2016-02-12 203 views
0

我遇到过很多不同的公式,如何构建投影矩阵,它们都有不同的输出,但我觉得我误解了这个概念。目前,我有这样的:如何正确计算正射投影矩阵?

//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 +高度或宽度只是高度&?在网络上的一些文章中,人们使用两种不同的公式。

+0

看起来不错,但你可以仔细检查这个[很好的教程](http://www.songho.ca/opengl/gl_projectionmatrix.html)。你在使用OpenGL还是DirectX? (它对Z分量稍有不同)。 –

回答

1

在此计算中,右下角应该是绝对的 - 即right = x + width。你的计算看起来正确。主要提示是这样的线:

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

x组件正在通过屏幕的宽度进行缩放。现在,使用您当前的设置,right - left(x + width) - x相同。这绝对会给width。相反,如果您使用的是right = width,那么计算结果将变为width - x,这意义不大。

+0

感谢您解决这个问题。 – MircoProgram