Jump to content
  • Advertisement
Sign in to follow this  
meteorstorm42

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.

If you intended to correct an error in the post then please contact us.

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) {

	fov *= DEG_TO_RAD;
	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....

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!