应用变换矩阵后不旋转

时间:2012-10-01 18:17:31

标签: android opengl-es-2.0

  

可能重复:
  Is Google’s Android OpenGL tutorial teaching incorrect linear algebra?

在Android上学习OpenGL ES 2.0。使用模拟器,运行Android 4.1。

Android Developer Site / OpenGL

复制并粘贴代码段

更新了onDrawFrame方法。贴在下面。 添加了Matrix.setIdentityM(mRotationMatrix, 0),因为它是 Null 矩阵。 将角度更改为角度(第16行)。

public void onDrawFrame(GL10 unused) {
    // Redraw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

    // Create a rotation transformation for the triangle
    long time = SystemClock.uptimeMillis() % 4000L;
    float angle = 0.090f * ((int) time);

    Matrix.setIdentityM(mRotationMatrix, 0); //added
    Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, 1.0f); //changed

    // Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

    // Draw shape
    mTriangle.draw(mMVPMatrix);
}

并注释掉setRenderMode(RENDERMODE_WHEN_DIRTY);

然而绘制的三角形没有旋转。我哪里出错了?

1 个答案:

答案 0 :(得分:0)

感谢this question here。我学会了解决这个问题。

只需编辑“顶点着色器代码”即可。 uMVPMatrix非常重要,如果没有这个,则不会应用投影。

private final String vertexShaderCode = "attribute vec4 vPosition;"
        +"uniform mat4 uMVPMatrix;"
        + "void main() {" + "  gl_Position = uMVPMatrix * vPosition;" + "}";