ModelMatrix + ViewMatrix 与 ModelViewMatrix

ModelMatrix + ViewMatrix vs ModelViewMatrix

我有一个代码可以将 modelViewMatrix 传递给着色器。我尝试分别传递 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;

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

截图:

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

截图:

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

mat4 MVMatrix = viewMatrix * modelMatrix;

有关说明,请参阅