Perspective Matrix Question

Started by
10 comments, last by kamaldeep40 13 years, 2 months ago
[ Removing your original post is a bit of a dick move. Here, I put it back for you. --Sneftel ] I am trying to understand the perspective matrix and I understand that the matrix is | 2-n/(r-l) 0 r+l/r-l 0| | 0 2n/(t-b) (t+b)/(t-b) 0| | 0 0 -(f+n)/(f+n) -2fn/(f-n)| | 0 0 -1 0| After performing the modeling matrix (scale, rotation, translation). I get the position to be: | 1.00 0.00 1.73 0.00 | | 0.77 1.79 -0.45 0.45 | | -1.55 0.89 0.89 -5.37 | | 0.00 0.00 0.00 1.00 | My question how does n, r, l, t, b, and f in the perspective matrix relate to the modeling matrix? [Edited by - Sneftel on October 13, 2009 1:29:59 PM]
Advertisement
They don't, because view matrix and projection matrix are two completely different things. OpenGL combines view and model transformation into a single modelview matrix, though (mostly because the view matrix is nothing but "move everything the opposite way" from a "virtual camera object point of view").

You should ask how this and gluPerspective are related.
f@dzhttp://festini.device-zero.de
how does the perspective relate to the camera coordinates?
It doesn't relate to the camera coordinates (if you are referring to the camera coordinates as the vectors used when constructing the modelview matrix)

the Projection matrix is post camera-transform, i.e. the camera/view has already had its effect.

if you are asking what those parameters actually are, they are:
near, right, left, top, bottom and far

what these actually do is define the frustum planes distances from the point that is exactly 1 unit in front of the camera. Apart from near and far being your clipping distances, typically:
left = -right
bottom = -top

a reason for specifying these seperately would be super-highresolution screenshot rendering, where you need to render the screen a small fraction at a time.

Ignoring the above reason, the top/bottom value is calculated by standard trig functions from the vertical-fov, and the left/right are the top/bottom but multiplied by the cameras' aspect ratio.

I am really confused. Im trying to compute the perspective matrix by hand and I don't know how to do this. In the post before I had what the computer computes the camera and perspective matrix to be. Now I'm trying to do this by hand but don't know where to start. If you could please help me that would be great.
Hi, this is my code to replace gluPerspective in my fps engine...
Please note the difference is that the aspect ratio is hidden from client.
void PerspectiveMatrix(   float matrix[16],   int width,   int height,   float fovy,   float znear,   float zfar){   float aspect = (float)width / (float)height;   float fovy2 = fovy * MATH_PIOVER360;   float cotan = cosf(fovy2) / sinf(fovy2);   matrix[0]  = cotan / aspect;   matrix[1]  = 0.0F;   matrix[2]  = 0.0F;   matrix[3]  = 0.0F;   matrix[4]  = 0.0F;   matrix[5]  = cotan;   matrix[6]  = 0.0F;   matrix[7]  = 0.0F;   matrix[8]  = 0.0F;   matrix[9]  = 0.0F;   matrix[10] = (znear + zfar) / (znear - zfar);   matrix[11] = -1.0F;   matrix[12] = 0.0F;   matrix[13] = 0.0F;   matrix[14] = (znear * zfar * 2.0F) / (znear - zfar);   matrix[15] = 0.0F;}
Quote:Original post by bitshifter
Hi, this is my code to replace gluPerspective in my fps engine...
Please note the difference is that the aspect ratio is hidden from client.
*** Source Snippet Removed ***


I have the code. What Im trying to do is figure out how the math behind the code. For example when executing the code how is the camera matrix computed or the perspective matrix computed. I understand how the model and camera matrix is computed, but I do not understand how the perspective matrix is computed.
Quote:Original post by bananasplitkids
but I do not understand how the perspective matrix is computed.


The documentation for gluPerspective says how it's computed.
Quote:Original post by Erik Rufelt
Quote:Original post by bananasplitkids
but I do not understand how the perspective matrix is computed.


The documentation for gluPerspective says how it's computed.


Thanks ok so I computed it by hand but my numbers don't match the computer generated numbers. For the perspective matrix the computer got

| 2.41 0.00 0.00 -0.00 |
| 0.00 2.41 0.00 -0.00 |
| 0.00 0.00 -1.00 -0.20 |
| 0.00 0.00 -1.00 -0.00 |

For the perspective matrix I got:
| 2.41 0.00 0.00 -0.00 |
| 0.00 2.41 0.00 -0.00 |
| 0.00 0.00 1.00 1 |
| 0.00 0.00 -.100 -0.00|

This was using the values fov =45 degrees, aspect ratio =1, near=0.1, and far=500
never mind i figured it out!

This topic is closed to new replies.

Advertisement