如何正确计算正投影矩阵?

时间:2016-02-12 17:27:10

标签: java projection orthographic

我发现了许多不同的公式如何构建投影矩阵,它们都给出了不同的输出,但我觉得我误解了这个概念。目前我有这个:

//x = 10 | y = 10 | width = 800 | height = 600
        float left = x;
        float top = y;
        float right = x + width;
        float bottom = y + height;

        final Matrix4f pMatrix = new Matrix4f();
        pMatrix.m[0][0] = 2f / (right - left);
        pMatrix.m[1][1] = 2f / (top - bottom);
        pMatrix.m[2][2] = -2f / (farZ - nearZ);
        pMatrix.m[3][0] = -(right + left) / (right - left);
        pMatrix.m[3][1] = -(top + bottom) / (top - bottom);
        pMatrix.m[3][2] = -(farZ + nearZ) / (farZ - nearZ);
        pMatrix.m[3][3] = 1;

使用给定的输出:

0.0025, 0.0, 0.0, -1.025
0.0, -0.0033333334, 0.0, 1.0333333
0.0, 0.0, -0.0068965517, -2.724138
0.0, 0.0, 0.0, 1.0

但是,我不确定rightbottom常数是否为x +宽度& y +高度或简单宽度&高度?在网络上的一些文章中,人们使用不同的公式。

1 个答案:

答案 0 :(得分:1)

在此计算中,右和底部应该是绝对的 - 即right = x + width。您的计算看起来正确。主要提示是这样的行:

pMatrix.m[0][0] = 2f / (right - left);

x分量按屏幕宽度缩放。现在,根据您当前的设置,right - left(x + width) - x相同。这肯定会给width。相反,如果您使用了right = width,那么计算将变为width - x,这很有意义。

相关问题