Sign in to follow this  

OpenGL Project a point from 3D to 2D

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this