Android OpenGL ES 2.0:无法执行简单旋转
我在OpenGL es2中使用了以下几行来旋转对象。 平移和缩放工作正常,但不会发生旋转Android OpenGL ES 2.0:无法执行简单旋转,android,ios,opengl-es,Android,Ios,Opengl Es,我在OpenGL es2中使用了以下几行来旋转对象。 平移和缩放工作正常,但不会发生旋转 //modelViewMatrix is float[16]; Matrix.setIdentity(modelViewMatrix,0); Matrix.translateM(modelViewMatrix, 0, x, y, z); Matrix.rotateM(modelViewMatrix, 0, ax, 1, 0, 0); //shader concatentation //gl_Positi
//modelViewMatrix is float[16];
Matrix.setIdentity(modelViewMatrix,0);
Matrix.translateM(modelViewMatrix, 0, x, y, z);
Matrix.rotateM(modelViewMatrix, 0, ax, 1, 0, 0);
//shader concatentation
//gl_Position = projection*modelview*position;
我试着制作一个用于旋转的自定义矩阵,但结果仍然相同
如果我移除旋转线,那么平移效果会很好,但是当我添加旋转线时,平移也会停止
令人惊讶的是,同样的方法在iOS上的OpenGLES2中也能正常工作
我怎样才能解决这个问题?在android上的OpenGL es2中,旋转对象的正确方式是什么。由于还没有限定的答案,我将加入我从openGLES2中“学到”的内容,这可能是准确的,也可能不是准确的,甚至是正确的 我的代码似乎与ClayMontogmery所说的相似——我转换模型矩阵,然后使用一个单独的浮点[16]矩阵来保持旋转(mCurrentRotation)。一旦这样做了,我将矩阵相乘,并将结果放入一个临时矩阵,然后用于投影(这里我的术语可能有点散乱),下面显示了我是如何平移和旋转的。我把前一段时间的例子拼凑起来:
Matrix.translateM(mModelMatrix, 0, 0.0f, 0.8f, -3.5f);
// Set a matrix that contains the current rotation.
Matrix.setIdentityM(mCurrentRotation, 0);
Matrix.rotateM(mCurrentRotation, 0, mDeltaX, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mCurrentRotation, 0, mDeltaY, 1.0f, 0.0f, 0.0f);
// Multiply the current rotation by the accumulated rotation, and then set the accumulated rotation to the result.
Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
// Rotate the model taking the overall rotation into account.
Matrix.multiplyMM(mTemporaryMatrix, 0, mModelMatrix, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mModelMatrix, 0, 16);
然后在drawModel中我有
/* this multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix (which now contains model * view * projection). */
Matrix.multiplyMM(mTemporaryMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
最后一行onSurfaceCreated:
Matrix.setIdentityM(mAccumulatedRotation, 0);
这可能无法完全回答您的问题,但这段代码符合我的需要,因此您可以从中获取一些信息。如果其中任何一个是“不成熟的”,我提前道歉,我对这方面的知识充其量也不多,我只是回答,因为没有其他人给出任何有用的答案。矩阵是安卓类还是其他类?要进行平移和旋转,必须将这两个矩阵相乘,然后将它们相乘到投影矩阵上。矩阵是android类的。谢谢,我将用这种方法为您尝试。但是,我没有在代码中连接modelView和投影矩阵,而是在着色器中连接。@CodenameLambda1我更新了答案,因为onSurfaceCreated()中缺少一行代码,仅供参考。我希望这能帮助您解决这个问题。好的,我会考虑到这一点。我尝试集成您的代码,但没有成功。我的ModelView矩阵已包含LookAt.的值。。我将其传递给对象,在转换中,对象被连接。。这有什么问题吗?@CodenameLambda1我也有。你可能需要重构。我使用Matrix.setLookAtM(…),然后创建程序并将其与着色器(顶点和片段)链接,然后在onSurfaceCreated()中执行setidentitym(mAccumulated rotation)。上面发布的另一个代码是onDrawFrame(),它非常适合我的代码,这是一个用于结构的3D模型查看器-不幸的是,我不确定还能发布什么。-经过编辑以修改正确的方法名称,它们一下子就不正确了