OpenGL ES 2.0 Scaling无法正常工作

时间:2013-03-26 12:01:31

标签: android opengl-es-2.0

我需要在OpenGL | ES 2.0中缩放对象。着色器:

    private final String vertexShaderCode =
        "uniform mat4 uMVPMatrix;" +
        "attribute vec4 vPosition;" +
        "void main() {" +
        //the matrix must be included as a modifier of gl_Position
            "  gl_Position = vPosition * uMVPMatrix;" +
        "}";

    private final String fragmentShaderCode =
        "precision mediump float;" +
                "uniform vec4 vColor;" +
                "void main() {" +                   
                "  gl_FragColor = vColor;" +
                "}";

投影:

        Matrix.orthoM(mProjMatrix,0,
              -1.0f,           // Left
               1.0f,           // Right
              -1.0f / ratio,   // Bottom
               1.0f / ratio,   // Top
               0.01f,          // Near
               10000.0f);

绘图设置:

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

实际渲染:

    float[] scale = {5f,5f,1f};
    Matrix.scaleM(scale_matrix, 0, scale[0], scale[1], scale[2]);

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

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, r_matrix, 0);

它不会扩展。我可以看到三角形,我可以旋转它。但缩放不起作用。

1 个答案:

答案 0 :(得分:4)

由于向量是OpenGL中的列向量,您必须更改顶点着色器中矩阵乘法的顺序:

gl_Position = uMVPMatrix * vPosition;