How to convert a 3d Vertex to Screen coordinates (any matrix gurus...)

Started by
4 comments, last by Vanz 20 years, 5 months ago
This seems like it should be a simple enough task, but it must be late because I am just pulling my hair out here trying to solve this. Basically all I want is: Given a world vertex at (x,y,z)... how do I get back a screen coodinate (x,y). Here''s what I am doing so far... I set up an object in my 3d world at say... (x,y,z) 800,0,500. Then positioned camera at 300,0,-1000. Taking a screen shot I grabbed the sx,sy (screen x,y) co-ordinates ~(550,300). Screen window is 800x600, with FOV=1.2, Aspect=1.33, NearPlane=1.0, FarPlane=5000. Step by Step... 1. Find World Matrix given 800,0,500: D3DXMatrixTranslation(&MatWorld, 800, 0, 500); W= [1 0 0 0] [0 1 0 0] [0 0 1 0] [800 0 500 1] 2. Get View Matrix (Camera): V= [1 0 0 0] [0 1 0 0] [0 0 1 0] [-300 0 1000 1] Thus, VW=VxW= [1 0 0 0] [0 1 0 0] [0 0 1 0] [500 0 0 1] , which is right object is 500 in x-dir from the camera. 3. Figure out Projection Matrix. I used call: Graphics->GetDeviceCOM()->GetTransform(D3DTS_PROJECTION, &matProj); or calculated using... w 0 0 0 0 h 0 0 0 0 Q s 0 0 -np 0 w = cot(fovw/2) h = cot(fovh/2) Q=fp/(fp-np) np = distance to near plane fp = distance to far plane Thus proj matrix is: P= [1.09 0 0 0] [0 1.46 0 0] [0 0 1.0 1.0] [0 0 -1.0 0] 4. Convert to Normalized Device (Screen) Co-ordinates (NDC) (-1.0 to 1.0) for x and y. 5. Convert NDC to screen sx, sy using projecyion matrix (#3). This I know how to do factoring in the FOV and Aspect ratio... Sooo where I really need help with is Step #4. How exactly do I get the NDC (screen x,y coordinates from -1.0 to 1.0). In other words how do I go from a 3d vertex (x,y,z) to normalized screen x,y?? Any help, links or suggestions would be huuugely appreciated... thanks in advance... rhuala
Advertisement
D3DXVec3Project does all you need.
Just insert the matrices and your viewport.

And when using GetTransform assure you don''t have a pure device.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

oh well look at that a function that already does what I''m trying to do, thanks Endurion...

What do you mean by "assure you don''t have a pure device".

I Usually read in the matrix as follows:

Graphics->GetDeviceCOM()->GetTransform(D3DTS_PROJECTION, &matProj);

How can I be sure I dont have a pure device??

rhuala
The device would only be pure if you created it as pure. If you are able to read the projection matrix with GetTransform you do not have a pure device. I guess you could do a simple test with a SetTransform followed by a GetTransform and see if you get back your matrix.

You may want to consider reading the projection matrix from a member variable that you set and don't use GetTransform; that way you can use or pure device or not without any problems.


______________________________________
Understanding is a three edged sword...


[edited by - Sean Doherty on November 11, 2003 9:30:25 AM]
_______________________________________Understanding is a three edged sword...Freelance Games - Home of XBLIG Starchonwww.FreelanceGames.com
Is this dependant on your video card? Is it a good idea to stay way from the command GetTransform?

Does this reduce the compatibility of your program...
The following is from the SDK documentation:

quote:
D3DCREATE_PUREDEVICE
Specifies that Direct3D does not support Get* calls for anything that can be stored in state blocks. It also tells Direct3D not to provide any emulation services for vertex processing. This means that if the device does not support vertex processing, then the application can use only post-transformed vertices.


Since a pure device does not support vertex processing; I can''t think of why you would want to use it? That said, unless you set the D3DCREATE_PUREDEVICE flag as part of CreateDevice you don''t have a pure device.

_______________________________________
Understanding is a three edged sword...
_______________________________________Understanding is a three edged sword...Freelance Games - Home of XBLIG Starchonwww.FreelanceGames.com

This topic is closed to new replies.

Advertisement