ModelMatrix + ViewMatrix vs ModelViewMatrix

时间:2016-09-07 08:27:20

标签: opengl 3d shader matrix-multiplication

我有一个代码可以将modelViewMatrix传递给着色器。我尝试分别传递modelMatrix和viewMatrix,但是我没有得到相同的结果,我真的不明白我错过了什么......

以下是我的代码的一部分:

  • 传递ModelViewmatrix(正常工作)

java代码:

        shaderProgram.loadProjectionMatrix(renderer.getProjectionMatrix());

        Matrix4f viewMatrix = TransformationMatrix.createViewMatrix(renderer.camera);
        Matrix4f modelMatrix = new Matrix4f();
        modelMatrix.setIdentity();

        Matrix4f layerTransformationMatrix = getTransformationMatrix();
        // set the translation
        Vector3f entityTranslation = shaderProgram.getTranslation();
        modelMatrix.m30 = entityTranslation.x;
        modelMatrix.m31 = entityTranslation.y;
        modelMatrix.m32 = entityTranslation.z;

        // mix the modelMatrix with the layer transformation
        modelMatrix.mul(layerTransformationMatrix);

        // reset the rotation
        modelMatrix.m00 = viewMatrix.m00;
        modelMatrix.m01 = viewMatrix.m10;
        modelMatrix.m02 = viewMatrix.m20;
        modelMatrix.m10 = viewMatrix.m01;
        modelMatrix.m11 = viewMatrix.m11;
        modelMatrix.m12 = viewMatrix.m21;
        modelMatrix.m20 = viewMatrix.m02;
        modelMatrix.m21 = viewMatrix.m12;
        modelMatrix.m22 = viewMatrix.m22;

        // compute modelViewMatrix
        Matrix4f modelViewMatrix = modelMatrix;
        modelViewMatrix.mul(viewMatrix);
        // inverse y axis
        modelViewMatrix.m11 = -1;

        shaderProgram.loadModelViewMatrix(modelViewMatrix);

着色器代码:

    void main(void)
    {
        gl_Position = projectionMatrix * modelViewMatrix * vec4(attribute_Position.xy, 0.0, 1.0);

        pass_textureCoords = attribute_TextureCoords;
        varying_Color = attribute_Color;
    }

屏幕截图:

enter image description here

  • 传递ModelMatrix和ViewMatrix(不工作)

java代码:

        shaderProgram.loadProjectionMatrix(renderer.getProjectionMatrix());

        Matrix4f viewMatrix = TransformationMatrix.createViewMatrix(renderer.camera);
        Matrix4f modelMatrix = new Matrix4f();
        modelMatrix.setIdentity();

        Matrix4f layerTransformationMatrix = getTransformationMatrix();
        // set the translation
        Vector3f entityTranslation = shaderProgram.getTranslation();
        modelMatrix.m30 = entityTranslation.x;
        modelMatrix.m31 = entityTranslation.y;
        modelMatrix.m32 = entityTranslation.z;

        // mix the modelMatrix with the layer transformation
        modelMatrix.mul(layerTransformationMatrix);

        // reset the rotation
        modelMatrix.m00 = viewMatrix.m00;
        modelMatrix.m01 = viewMatrix.m10;
        modelMatrix.m02 = viewMatrix.m20;
        modelMatrix.m10 = viewMatrix.m01;
        modelMatrix.m11 = viewMatrix.m11;
        modelMatrix.m12 = viewMatrix.m21;
        modelMatrix.m20 = viewMatrix.m02;
        modelMatrix.m21 = viewMatrix.m12;
        modelMatrix.m22 = viewMatrix.m22;

        shaderProgram.loadModelMatrix(modelMatrix);
        shaderProgram.loadViewMatrix(viewMatrix);

着色器代码:

void main(void)
{
    mat4 MVMatrix = modelMatrix * viewMatrix;
    MVMatrix[1][1] = -1;
    gl_Position = projectionMatrix * MVMatrix * vec4(attribute_Position.xy, 0.0, 1.0);

    pass_textureCoords = attribute_TextureCoords;
    varying_Color = attribute_Color;
}

屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:1)

在着色器中乘以矩阵的顺序是错误的。它必须是

mat4 MVMatrix = viewMatrix * modelMatrix;

有关说明,请参阅this post