# OpenGL Perspective matrix + coordinate spaces + debugging

This topic is 3333 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I'm trying to write a geometry projection class and had some questions about coordinate spaces. 1) After applying the ModelView & Projection matrices, I should be in clip space? So, x,y,z coords whould be between -w and w? 2) After the homogeneous divide ( divide x,y,z by w ), all coords should be in normalized device coords? So between -1 and 1? First, I checked my ModelView matrix stuff against OpenGL, and output matches, so that works fine For the perspective projection matrix, when I use this:
void setPerspectiveProjection(float fov, float aspect, float n, float f) {

float t = n * tan( fov/2.0 );
float r = t * aspect;

float proj[4][4] = {	{ n/r,	0.0,	0.0,	0.0 },
{ 0.0,	n/t,	0.0,	0.0 },
{ 0.0,	0.0,	-1*(f+n)/(f-n),	(-2*f*n)/(f-n) },
{ 0.0,	0.0,	-1.0,	0.0 }  };

^ I get something different from the OpenGL's call to gluPerspective(), but (if my assumptions about spaces above are correct), my vertices end up between -w and w. But when I do this:
void setPerspectiveProjection(float fov, float aspect, float n, float f) {

float c = 1.0/tan( fov*DEG_TO_RAD/2.0 );
float proj[4][4] = {	{ c/aspect,	0.0,	0.0,		0.0 },
{ 0.0,		c,	0.0,		0.0 },
{ 0.0,		0.0,	(f+n)/(n-f),	(2*f*n)/(n-f) },
{ 0.0,		0.0,	-1.0,		0.0 }	};


^ I get the same as OpenGL's call to gluPerspective(), but my vertices are way out of the range of -w to w, so when I do the homogeneous divide, they're not between -1 to 1 ... I have no idea what I'm doing wrong....

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013434
×