1

我需要在 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);

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

4

1 回答 1

4

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

gl_Position = uMVPMatrix*vPosition;

于 2013-03-29T15:24:51.390 回答