Android Studio NDK-OpenGL ES 觸屏坐標轉游㱆坐標

Android Studio NDK-OpenGL ES 觸屏坐標轉游㱆坐標
Android Studio NDK-OpenGL ES 觸屏坐標轉游㱆坐標

觸屏坐標』『x,y』坐標轉『正交投影』坐標, 『視錐體解像』寬高, 比例需手機解像寬高比壹致.

計屏幕寬高比

float aspect_ratio = (float)cam->real_width / (float)cam->real_height;

『視錐體解像』寬高,此時定義『高』800pix

float frustum_width    = 800 *aspect_ratio;
float frustum_height   = 800 ;

 

『正交投影』代碼

重置視區尺寸, 值係手機解像寬高

::glViewport(0,0,real_width,real_height);

設定投影矩陣

::glMatrixMode(GL_PROJECTION);

載入單位矩陣

::glLoadIdentity();

正交投影, 游戲坐標原點(0,0,0)為於屏幕中心

glOrthof(frustum_width / 2, frustum_width / 2, -frustum_height / 2, frustum_height / 2, pos.y – 10, far_clip_z);

設定模型視圖矩陣

::glMatrixMode(GL_MODELVIEW);

載入單位矩陣

::glLoadIdentity();

 

手指触摸手機屏幕onTouch() 所得坐標需轉游戲世界坐標,正交投影OpenGL游㱆+Z軸指向屏幕深處.

float touch3Dx = (touch2Dx / real_width) * frustum_width  ;
float touch3Dz = (touch2Dy /real_height) * frustum_height  ;

計3D相機位置

touch3Dx = touch3Dx + camPosX;
touch3Dz = touch3Dz + camPosX;

游戲坐標原點(0,0,0)為於屏幕中心

touch3Dx = touch3Dx – (frustum_width / 2.0f);
touch3Dz = touch3Dz – (frustum_height / 2.0f);

 

發表評論