Archived

This topic is now archived and is closed to further replies.

CoderTCD

how does projection matrix work?

Recommended Posts

The PROJECTION_MATRIX (PM) matrix is used to multiply your MODELVIEW_MATRIX(MM)

point = MM * MP * original_point

After this OpenGL apply z division, clipping,...to project point in the viewport.

You know that you set up PM using directly glFrustum() or gluPerspective()...gluPerspective() fill the PM with this algorithm I found

here is gluPerspective source code (Mesa 3.0):


void APIENTRY gluPerspective( GLdouble fovy, GLdouble aspect,
GLdouble zNear, GLdouble zFar )
{
GLdouble xmin, xmax, ymin, ymax;

ymax = zNear * tan( fovy * M_PI / 360.0 );
ymin = -ymax;

xmin = ymin * aspect;
xmax = ymax * aspect;

glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
}



To read PM you use glGetFloatv(GL_PROJECTION_MATRIX, float16)

To read info from this matrix I have resolved the (simple) system

   

// I've used different notation...sorry!

//

// However this is the matrix

// A 0 E 0

// 0 B F 0

// 0 0 C D

// 0 0 -1 0


// We must transpose


// frustumMatrix is the PM as it is from glGet

REAL A = frustumMatrix[0];
REAL B = frustumMatrix[5];
REAL C = frustumMatrix[10];
REAL D = frustumMatrix[14];
REAL E = frustumMatrix[8];
REAL F = frustumMatrix[9];

// The params


REAL n = D/(C-1);
REAL f = D/(C+1);
REAL l = n * (E - 1) / A;
REAL r = n * (E + 1) / A;
REAL b = n * (F - 1) / B;
REAL t = n * (F + 1) / B;



So you get l,r,b,t,n,f that describe your projection: n and f are znear and zfar; l,r,b,t describe the rectangle on znear that is mapped exactly on the viewport.


[edited by - blizzard999 on October 22, 2003 6:51:54 AM]

[edited by - blizzard999 on October 22, 2003 6:53:15 AM]

Share this post


Link to post
Share on other sites