OpenGL ES 2.0:在Android上围绕自身旋转对象

OpenGL ES 2.0:在Android上围绕自身旋转对象,android,opengl-es,Android,Opengl Es,我试图旋转移动的物体,但它绕着坐标系的中心旋转。如何使它在移动时围绕自身旋转?代码是: 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); 您在哪里绘制对象图形 我想是在你在这里写的代码之后,比如: Matrix.translateM(mMMatrix,

我试图旋转移动的物体,但它绕着坐标系的中心旋转。如何使它在移动时围绕自身旋转?代码是:

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); 

您在哪里绘制对象图形

我想是在你在这里写的代码之后,比如:

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.translateM(mMMatrix,0,0,-y,0);
setRotateM(mMMatrix,0,mAngle,0,0,1.0f);
y+=速度;
矩阵.translateM(mMMatrix,0,0,y,0);

drawHere()// 我只是用视图矩阵而不是模型矩阵,一切都解决了。有关模型、视图和投影矩阵的详细信息。

不要使用视图矩阵旋转对象,该矩阵用作所有场景的摄影机,要变换对象,应使用模型矩阵。要围绕其自身中心旋转,可以使用以下方法:

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); 
}
别忘了使用单位矩阵来重置每个循环中的变换

我想你的密码正在运行。在应用任何转换之前,应该更新“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);
}
最后,render方法将执行着色器来绘制对象:

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 (...);
}

我希望这能对你有所帮助。

这个问题(及其答案)的可能重复可能使用旧的GL固定函数,但其背后的数学与你所做的完全相同。实际上,我无法从这个问题中找出问题的解决方案。我在创建曲面时设置了单位矩阵。为什么第二个翻译是问题?我只是移动到原点,旋转,然后转换到新坐标。我已经测试了你的代码,正如你所说的,它有问题。我在上面的asnwer中修复了它。现在,你能告诉我你在哪里画物体吗?
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 (...);
}