我试图从OpenGL C ++迁移到OpenGL ES 2.0,而且我不确定矩阵是如何工作的。
以前,我使用glOrtho在projectionView中设置一个网格,原点位于中心,如下所示:( y比率通常为1)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ASPECT_RATIO_X, ASPECT_RATIO_X, -ASPECT_RATIO_Y,
ASPECT_RATIO_Y, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
进入android我尝试做同样的事情,但看起来坐标平面设置不正确。两个轴上的坐标平面似乎都在-1到1之间。 x轴应该是-ratio比率。这是我在onSurfaceChanged()函数中尝试做的事情:
Matrix.setIdentityM(mtrxProjection, 0);
Matrix.setIdentityM(mtrxView, 0);
Matrix.setIdentityM(mtrxProjectionAndView, 0);
Matrix.orthoM(mtrxProjection, 0, -ratio, ratio, -1.0f, 1.0f, 0, 50);
Matrix.setLookAtM(mtrxView, 0, 0f, 0f, 1f, 0, 0, 0f, 0f, 1.0f, 0.0f);
Matrix.multiplyMM(mtrxProjectionAndView, 0, mtrxProjection, 0, mtrxView, 0);
onDrawFrame,我清除,重置矩阵,并绘制实体:
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
Matrix.setIdentityM(mtrxProjectionAndView, 0);
Matrix.multiplyMM(mtrxProjectionAndView, 0, mtrxProjection, 0, mtrxView, 0);
//draw entities here
以下是我为每个实体设置矩阵的方法(为每个渲染调用构建):
float[] scaleMatrix = new float[16];
scaleMatrix[0] = scale.x;
scaleMatrix[5] = scale.y;
scaleMatrix[10] = scale.z;
scaleMatrix[15] = 1;
translateMatrix[12] = position.x;
translateMatrix[13] = position.y;
translateMatrix[14] = position.z;
float[] rotationXMatrix = new float[16];
float[] rotationYMatrix = new float[16];
float[] rotationZMatrix = new float[16];
Matrix.setIdentityM(matrix, 0);
Matrix.multiplyMM(matrix, 0, scaleMatrix, 0, matrix, 0);
Matrix.setRotateM(rotationXMatrix, 0, rotation.x, 1, 0, 0);
Matrix.setRotateM(rotationYMatrix, 0, rotation.y, 0, 1, 0);
Matrix.setRotateM(rotationZMatrix, 0, rotation.z, 0, 0, 1);
Matrix.multiplyMM(matrix, 0, rotationXMatrix, 0, matrix, 0);
Matrix.multiplyMM(matrix, 0, rotationYMatrix, 0, matrix, 0);
Matrix.multiplyMM(matrix, 0, rotationZMatrix, 0, matrix, 0);
Matrix.multiplyMM(matrix, 0, translateMatrix, 0, matrix, 0);
Matrix.multiplyMM(GLRenderer.mtrxProjectionAndView, 0, GLRenderer.mtrxView, 0, matrix, 0);
Matrix.multiplyMM(GLRenderer.mtrxProjectionAndView, 0, GLRenderer.mtrxProjectionAndView, 0, GLRenderer.mtrxProjection, 0);
答案 0 :(得分:0)
更新:矩阵乘法的运算顺序在最后一行搞砸了:
Matrix.multiplyMM(GLRenderer.mtrxProjectionAndView, 0,
GLRenderer.mtrxProjection, 0, GLRenderer.mtrxProjectionAndView, 0);