projection matrix question

Started by
3 comments, last by silvermace 19 years, 6 months ago
I was trying to create the projection matrix myself rather than using gluPerspective() or glFrustum(). I used the matrix in the back of the red book defined for perspective projection. I'm having trouble coming up with the same values for the matrix I'm calculating that gluPerspective() comes up with. I was hoping someone could spot and see if I'm missing something. For a camera with aspect ratio 1.33, a viewAngle of 30.0, and clipping planes at 0.1 and 300.0: gluPersp matrix: 2.79904 0 0 0 0 3.73205 0 0 0 0 -1.0 -1.0 0 0 -0.2 0 my matrix: 2.79904 0 0 0 3.73205 0 0 0 0 -1.0 -0.2 0 0 -1.0 0 The 11 and 14 positions seem to be swapped. I'm confused by position 14, as all my references say to load -1.0 here. Why would gluPerspective's position 14 be anything other than -1.0? Here is how I'm loading the matrix: tTop = nearClip * tan(PI/180.0 * (viewAngle/2.0)); tBot = -tTop; tRight = tTop * iAspect; tLeft = -tRight; projMatrix.m[0] = (2.0 * nearClip)/(tRight - tLeft); projMatrix.m[1] = 0.0; projMatrix.m[2] = (tRight + tLeft)/(tRight - tLeft); projMatrix.m[3] = 0.0; projMatrix.m[4] = 0.0; projMatrix.m[5] = (2.0 * nearClip)/(tTop - tBot); projMatrix.m[6] = (tTop + tBot)/(tTop - tBot); projMatrix.m[7] = 0.0; projMatrix.m[8] = 0.0; projMatrix.m[9] = 0.0; projMatrix.m[10] = -(farClip + nearClip)/(farClip - nearClip); projMatrix.m[11] = (-2.0 * (farClip * nearClip))/(farClip - nearClip); projMatrix.m[12] = 0.0; projMatrix.m[13] = 0.0; projMatrix.m[14] = -1.0; projMatrix.m[15] = 0.0; Thanks for any help! [Edited by - mjs1 on October 16, 2004 8:01:05 PM]
Advertisement
the matrix you generate is not a perspective matrix, its a plane jane glFrustrum matrix.

try comparing a glFrustrum generated matrix to your matrix, that should be the same, post your results back here.

cheers,
Danu
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Danu,

Thanks for your reply! I was under the impression that glFrustum() and gluPerspective() both set up the same matrix and the differences between the two were only in the inputs given them, which made glPerspective() more intuitive to use. In the end don't they do the same thing? They both create a perspective transformation matrix, right?
gluPerspective is a shortcut to glFrustrum, it simply provides a more intuitive interface for glFrustrum, but yes i do beleive the matricies they generate are the same.

here is how i calculate my perspective matrix (from my engine)
void Renderer::setPersepectiveMatrix(){   double left, right;    double bottom, top;   const double AspectRatio = (double) (m_width / m_height);   const double Fov = 90.0; // 90 degrees   top    = tan(Fov * math::Pi / 360.0) * zNear;   bottom = -top;   left   = AspectRatio * bottom;   right  = AspectRatio * top;   // make the OpenGL matrix   glFrustum (left, right, bottom, top, zNear, zFar);}
in your case you should be able to swap the "glFrustum" call with your manual calculations (they seem correct)

Cheers,
Danu

EDIT: just looking at your calculations, they seem ok .... ?
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
can you provide your sample code please?
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement