Jump to content
  • Advertisement
Sign in to follow this  
Phy

OpenGL Vertex Coordinates

This topic is 4981 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

I'm having trouble getting the on screen coordinates for vertices input into OpenGL, I think that the problem is in my code however. Does anyone see anything wrong with this?
float tpos[4];
			tpos[0] = ptcl->pos[0];
			tpos[1] = ptcl->pos[1];
			tpos[2] = ptcl->pos[2];
			tpos[3] = 1.0f;

			float mtx[16];
			glGetFloatv( GL_MODELVIEW_MATRIX, mtx );
			tpos[0] = mtx[0]*tpos[0] + mtx[4]*tpos[1] + mtx[8]*tpos[2] + mtx[12]*tpos[3];
			tpos[1] = mtx[1]*tpos[0] + mtx[5]*tpos[1] + mtx[9]*tpos[2] + mtx[13]*tpos[3];
			tpos[2] = mtx[2]*tpos[0] + mtx[6]*tpos[1] + mtx[10]*tpos[2] + mtx[14]*tpos[3];
			tpos[3] = mtx[3]*tpos[0] + mtx[7]*tpos[1] + mtx[11]*tpos[2] + mtx[15]*tpos[3];
			glGetFloatv( GL_PROJECTION_MATRIX, mtx );
			tpos[0] = mtx[0]*tpos[0] + mtx[4]*tpos[1] + mtx[8]*tpos[2] + mtx[12]*tpos[3];
			tpos[1] = mtx[1]*tpos[0] + mtx[5]*tpos[1] + mtx[9]*tpos[2] + mtx[13]*tpos[3];
			tpos[2] = mtx[2]*tpos[0] + mtx[6]*tpos[1] + mtx[10]*tpos[2] + mtx[14]*tpos[3];
			tpos[3] = mtx[3]*tpos[0] + mtx[7]*tpos[1] + mtx[11]*tpos[2] + mtx[15]*tpos[3];

			float fBuffValue;
			glReadPixels( tpos[0], tpos[1], 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &fBuffValue );

			if ( fBuffValue < tpos[2] )
			{
				ptcl = ptcl->nextParticle;

				if ( ptcl )
					goto Render_while;
				else
					goto Render_wend;
			}

Share this post


Link to post
Share on other sites
Advertisement
Well, the matrix multiplications are slightly wrong. The multiplications itself is correct, but the way you handle the answer is not correct. Consider the first line of a multiplication where you calculate tpos[0]. Now consider the next line, where you calculate tpos[1]. In this second line you use the new value of tpos[0], when you should use the original value of tpos[0].

You must store the result in another array, and leave tpos alone for the entire matrix multiplication. And when done, you can copy the result from the temporary array to tpos (or use the temporary array as source for next multiplication, but you get the idea I hope).

However, to get the actual screen space coordinates, you're missing two steps after the modelview and projection matrix multiplication; perspective division and viewport transformation.

After the projection matrix, you need to perform the perspective division, which means you divide all values in tpos by tpos[3]. Then you apply the viewport transform, which means you transform the coordinate from normalized device coordinates to screen space coordinates.

screenpos[0] = viewport[0] + (1 + tpos[0]) * viewport[2] / 2;
screenpos[1] = viewport[1] + (1 + tpos[1]) * viewport[3] / 2;

viewport[] is an array with viewport parameters returned by glGetInteger and GL_VIEWPORT. screenpos[] is the X and Y coordinate in screen space.

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!