我正在尝试创建一个简单的Android OpenGL 2.0游戏来让我的脚湿透。我对OpenGL的Androids教程进行了调整并开始运行,将我的方块移动到我想要的位置,现在我正试图通过触摸来翻译它。
我读过我必须取消现有方格的投射...但不理解这一点。如果在广场上进行翻译有任何帮助,下面是我的代码......
private float mPreviousY;
@Override
public boolean onTouchEvent(MotionEvent e) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dy = y - mPreviousY;
// reverse direction of rotation to left of the mid-line
if (y < getHeight() / 2) {
dy = dy * -1 ;
}
mRenderer.mOffSet += dy;
requestRender();
}
mPreviousY = y;
return true;
}
我的onDrawFrame:
@Override
public void onDrawFrame(GL10 unused) {
// Draw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -50, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
Matrix.translateM(mModleViewProjMatrix, 0, 0, mOffSet, 0);
// Calculate the projection and view transformation
Matrix.multiplyMM( mModleViewProjMatrix, 0, mProjMatrix, 0, mViewMatrix, 0);
// Draw square
mPaddle.draw(mModleViewProjMatrix);
}
答案 0 :(得分:1)
Unprojecting意味着,逆转顶点在转换时经历的过程。正向变换是
v_eye = Modelview · v
v_clip = Projection · v_eye
v_ndc = v_clip / v_clip.w
现在你要做的就是扭转这个过程。我建议你看一下Mesa的GLU函数gluUnProject的源代码,可以在这里找到http://cgit.freedesktop.org/mesa/glu/tree/src/libutil/project.c
取消投影实质上是在扭转这一过程。
让我们看一下Mesa的GLU gluUnProject代码:
GLint GLAPIENTRY
gluUnProject(GLdouble winx, GLdouble winy, GLdouble winz,
const GLdouble modelMatrix[16],
const GLdouble projMatrix[16],
const GLint viewport[4],
GLdouble *objx, GLdouble *objy, GLdouble *objz)
{
double finalMatrix[16];
double in[4];
double out[4];
首先评估compund转换Projection · Modelview
......
__gluMultMatricesd(modelMatrix, projMatrix, finalMatrix);
......倒置,即颠倒;
if (!__gluInvertMatrixd(finalMatrix, finalMatrix)) return(GL_FALSE);
in[0]=winx;
in[1]=winy;
in[2]=winz;
in[3]=1.0;
然后将窗口/视口坐标映射回NDC坐标
/* Map x and y from window coordinates */
in[0] = (in[0] - viewport[0]) / viewport[2];
in[1] = (in[1] - viewport[1]) / viewport[3];
/* Map to range -1 to 1 */
in[0] = in[0] * 2 - 1;
in[1] = in[1] * 2 - 1;
in[2] = in[2] * 2 - 1;
乘以复合投影模型视图的倒数
__gluMultMatrixVecd(finalMatrix, in, out);
最后检查,所谓的同质成分是非零的
if (out[3] == 0.0) return(GL_FALSE);
同质的分裂倒转。
out[0] /= out[3];
out[1] /= out[3];
out[2] /= out[3];
在投影过程之前产生原始顶点位置
*objx = out[0];
*objy = out[1];
*objz = out[2];
return(GL_TRUE);
}