• Advertisement

Archived

This topic is now archived and is closed to further replies.

Converting a point in 3D space to a 2D point on screen

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

Is anyone aware of exactly how Direct3D converts a point in 3D space to a 2D screen position? I''m trying to write some code that can produce the same 2D screen coordinates that Direct3D''s geometry pipeline outputs given a 3D vertex. At the moment what I do is first multiply the 3D point by the view matrix (I dont use the world matrix so I don''t need to worry about it) and then multiply the result by the projection matrix. And then I multiply this result with the following matrix w 0 0 0 0 -h 0 0 0 0 1 0 0 h 0 1 where w and h are the width and height of the screen. Say the result I get from this is (x, y, z, w). Then the final 2D screen point should be ( x/w, y/w ) BUT THIS DOES NOT WORK!! I used http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndir3d/html/d3dxfrm6.asp as a guide. Somebody please help!

Share this post


Link to post
Share on other sites
Advertisement
without looking too close and double checking. if the projection matrix is returning a value between -1,1...

anyway, the easiest way i found to convert screen/world coords requires keeping your own "camera" matrix (ie. the inverse of the view matrix). the dot product of (point-camera.position) with each vector (right/up/forward) results in view space coords. dividing the new x and y by z return the positions on a plane 1 unit in front of the viewer. the size of this plane is:

height=2*tan(fov/2), width=2*height*aspect

so x'=x/(width*z) and y'=y/(height*z) are between -.5,.5 and
xs=(x'+.5)*(resolutionX-1) and ys=(y'+.5)*(resolutionY-1) should be the screen coordinates of the point.

of course this requires a matrix-camera and calculating height and width when the projection matrix changes.

so all together:
vec3 p=Point-Cam.Pos;
z=dot(p,Cam.Fwd);
x=.5f + dot(p,Cam.Right)/(z*width);
x*=(resX-1);
y=.5f + dot(p,Cam.Up)/(z*height);
y*=(resY-1);

opengl has gluproject and gluunproject to do this, so im more or less sure d3d should have similar functions too.
(D3DXVec3Project, D3DXVec3Unproject)

[edited by - Trienco on March 4, 2004 2:24:42 PM]

Share this post


Link to post
Share on other sites

  • Advertisement