View Full Version : How does openGL actually use the projection matrix

08-11-2008, 04:38 AM

Can someone tell me what maths openGL does internally with the projection matrix when rendering? I've tried drawing a wireframe version of the world using my own 2d draws on top the openGL 3d world. They are pretty much the same, expect my own projected 2d lines seem as if there's a slightly different FOV. This can't be the case I've checked the matrix that I pass to openGL as a GL_PROJECTION_MATRIX, and it's the same as what I'm using to draw when converting from 3d to 2d. So that must mean I'm not using the projection matrix correctly, or that openGL must be adding an extra step somewhere. My 3d to 2d function looks like this:

void Transform3dto2dScreenCoord( MATRIX *m, VECTOR *v, VECTOR *vout )

float w;

vout->x = v->x*m->m[MINDEX_00] + v->y*m->m[MINDEX_10] + v->z*m->m[MINDEX_20] + m->m[MINDEX_30];
vout->y = v->x*m->m[MINDEX_01] + v->y*m->m[MINDEX_11] + v->z*m->m[MINDEX_21] + m->m[MINDEX_31];
vout->z = v->x*m->m[MINDEX_02] + v->y*m->m[MINDEX_12] + v->z*m->m[MINDEX_22] + m->m[MINDEX_32];

w = v->x*m->m[MINDEX_03] + v->y*m->m[MINDEX_13] + v->z*m->m[MINDEX_23] + m->m[MINDEX_33];

vout->z = vout->z/w;
vout->y = vout->y/w;
vout->x = vout->x/w;


vout->x *= ScreenWidth;
vout->y *= ScreenWidth;

vout->x += ScreenWidth/2;
vout->y += ScreenHeight/2;


Any ideas how this compares to what openGL actually does?

Thanks in advance.

08-11-2008, 05:59 AM
you may need to read the paragraph "2.11 Coordinate transformations" p 40 in the gl specification.

08-11-2008, 06:32 AM
So from my understanding, I've done the viewport transformation incorrectly? Any idea what I'd need to add/change to my code to make it the same?

08-11-2008, 08:47 AM
Remove the division by Z and multiply by the screen dimensions / 2 (and use ScreenHeight for y).

08-11-2008, 09:39 AM
yep... that did it! Thanks!