Jump to content
  • Advertisement
Sign in to follow this  
nickf

OpenGL Project a point from 3D to 2D

This topic is 4256 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, I'll apologise in advance as I'm sure this has been answered somewhere before, I just can't seem to find it. I've got the coordinates of an object in 3D and I'd like the corresponding 2D coordinates so I could draw some text on the screen next to the object. I noticed when searching that OpenGL has gluProject() for this kind of thing. Is there an equivalent D3DX call? My current attempt looks like this which looks nearly right until I start moving the camera around.
Vector2 projectPoint(Vector3* point)
{
	Matrix v,p;
	Vector3 temp;

	device->GetTransform(D3DTS_VIEW, &v);
	device->GetTransform(D3DTS_PROJECTION, &p);

	// Project point
	D3DXVec3TransformCoord(&temp, point, &v);
	D3DXVec3TransformCoord(&temp, &temp, &p);

	// Temp now stores point in normalised coords (-1 to 1)

	Vector2 r;
	r.x = (temp.x + 1) * getWidth()/2;
	r.y = (temp.y + 1) * getHeight()/2;
	return r;
}
Could someone tell me what I'm missing, it's driving me nuts :-) Thanks in advance

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.

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!