PDA

View Full Version : building projection matrix from viewWidth, viewHeight, nearClip and farClip



soconne
01-29-2006, 02:32 PM
I'm trying to define a perspective matrix when only using viewWidth, viewHeight, nearClip and farClip. I don't know if it is correct, since I cannot seem to find ANY sources online except for MSDN DirectX, but their method does not work for OpenGL.


void Matrix4::setupProjectionMatrixWH(float w, float h, float zNear, float zFar)
{
m[0] = 2*zNear/w;
m[1] = 0;
m[2] = 0;
m[3] = 0;

m[4] = 0;
m[5] = 2*zNear/h;
m[6] = 0;
m[7] = 0;

m[8] = 0;
m[9] = 0;
m[10] = -(zFar + zNear) / (zFar - zNear);
m[11] = -1;

m[12] = 0;
m[13] = 0;
m[14] = -(2.0f * zFar * zNear) / (zFar - zNear);
m[15] = 0;
}I think m[10] and m[14] are calculated correctly, but I'm not sure about m[0] and m[5].

andras
01-29-2006, 03:59 PM
OpenGL 2.0 specification, page 45. (chapter 2.11.2)

soconne
01-29-2006, 04:15 PM
I just downloaded the specification: http://www.opengl.org/documentation/specs/version2.0/glspec20.pdf

And nowhere is your reference to my problem. What link did you look at?

andras
01-29-2006, 07:48 PM
Well, it's page 59 if you start counting from the cover..

Gernot Ziegler
01-31-2006, 01:37 AM
Just build a glFrustum out of it; left and right are +/-viewWidth/2.0, top and bottom are +/- viewHeight/2.0.