2012-05-11 67 views
3

我试图旋转移动物体,但它旋转坐标系统的中心。如何让它在移动时围绕自身旋转?代码是:OpenGL ES 2.0:在Android上围绕自身旋转物体

Matrix.translateM(mMMatrix, 0, 0, -y, 0); 
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f); 
y += speed; 
Matrix.translateM(mMMatrix, 0, 0, y, 0); 
+0

[如何在OpenGL中围绕本地轴旋转对象?](http://stackoverflow.com/questions/1671210/how-to-rotate-object-around-local-axis-in-opengl)问题(及其答案)可能会使用旧的GL固定功能,但其背后的数学与您所做的完全相同。 –

+0

我实际上找不出解决我的问题的方法 – nifuki

回答

0

你在哪里做对象绘图?

我想这是你在这里搭起代码,喜欢的东西后:

Matrix.translateM(mMMatrix, 0, 0, -y, 0); 
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f); 
y += speed; 
Matrix.translateM(mMMatrix, 0, 0, y, 0); 
drawHere();//<<<<<<<<<<<<<<<<<<< 

然后,第二翻译呼叫的问题。 您应该在第二次翻译之前移动您的绘画通话。 或 干净的方式来做到这一点是:

Matrix.setIdentityM(mMMatrix, 0);//<<<<<<<<added 
Matrix.translateM(mMMatrix, 0, 0, -y, 0); 
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f); 
y += speed; 
//Matrix.translateM(mMMatrix, 0, 0, y, 0); //<<<<<<<<<removed 
drawHere(); 
+0

创建表面时我设置了单位矩阵。为什么第二个翻译是问题?我只是移动到原点,旋转然后翻译成新的坐标。 – nifuki

+0

我已经测试过你的代码,并且它有你说的问题。我把它固定在上面的asin中。现在,你能告诉我你在制作物体的地方吗? – CuriousChettai

0

我只是用视图矩阵,而不是模型矩阵和一切工作。有关模型,视图和投影矩阵see的详细信息。

2

不要使用视图矩阵来旋转对象,这个矩阵被用作所有场景的相机,要转换一个对象,你应该使用模型矩阵。要旋转,如果绕自己的中心,你可以用下面的方法:

public void transform(float[] mModelMatrix) { 
    Matrix.setIdentityM(mModelMatrix, 0); 
    Matrix.translateM(mModelMatrix, 0, 0, y, 0); 
    Matrix.rotateM(mModelMatrix, 0, mAngle, 0.0f, 0.0f, 1.0f); 
} 

唐`忘记使用单位矩阵重置变换中的每一个回路。

我认为你的代码是worng。在应用任何转换之前,您应该更新'y'的值。

public void onDrawFrame(GL10 gl) { 
    ... 
    y += speed; 
    transform(mModelMatrix); 
    updateMVP(mModelMatrix, mViewMatrix, mProjectionMatrix, mMVPMatrix); 
    renderObject(mMVPMatrix); 
    ... 
} 

的updateMVP方法,将结合模型,视图和投影矩阵:

private void updateMVP( 
     float[] mModelMatrix, 
     float[] mViewMatrix, 
     float[] mProjectionMatrix, 
     float[] mMVPMatrix) { 

    // combine the model with the view matrix 
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); 

    // combine the model-view with the projection matrix 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 
} 

而在最后的渲染方法,将执行着色器来绘制对象:

public void renderObject(float[] mMVPMatrix) { 

    GLES20.glUseProgram(mProgram); 

    ... 

    // Pass the MVP data into the shader 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 

    // Draw the shape 
    GLES20.glDrawElements (...); 
} 

我希望这会帮助你。

+1

你可以添加一些代码来解释你的答案吗? – 2012-10-08 08:25:35