Transforming Points from World Space to Screen Coordinates

This topic is 3096 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, I'm trying to simply convert a unit's model-space vertices to screen-space in order to use its screen coordinates. I have a rectangle representing its boundary (plane[4]) and figured all you would need to do is the following:
int i;
D3DXMATRIX matr,proj,view,world;
D3DXVECTOR3 plane[4]={//set up unit's boundary plane (index runs clockwise from top-left):
D3DXVECTOR3(-32,-64,-1),//top left
D3DXVECTOR3( 32,-64,-1),//top right
D3DXVECTOR3( 32, 64,-1),//bottom right
D3DXVECTOR3(-32, 64,-1),//bottom left
};

graphDevice->GetTransform(D3DTS_PROJECTION,&proj); //retrieve currently-used projection matrix
graphDevice->GetTransform(D3DTS_VIEW,&view); //and view matrix
setupUnitWorldMatrix(this_unit,&world); //the same world matrix I use for drawing

D3DXMatrixMultiply(&matr,&matr,&proj); //then multiply that by projection last
//the above is the order that it's done in the graphics pipeline, right?
//(I tried with and without D3DXMatrixTranspose(&matr,&matr) here)

for(i=0;i<4;i++)D3DXVec3TransformCoord(&plane,&plane,&matr);//transform them all by our combined world/view/projection matrix.


I would expect it to make the transformed points' x and y components into screen space, with the z components all 0, right? I centered the model at about the middle of the screen and zoomed so that the points should vary by a few dozen pixels at least. It isn't working, and I suspect it's a problem with the matrix - it's making the plane[4] corners all small numbers near to 1, for example {0.82106942,0.98830092,0.99237508}, with each corner's point varying only by like 0.0000001 or so. I've tried transposing the matrix too (which I've needed in some cases for drawing though don't understand why), also with off results. What does the matrix need?

Share on other sites
To convert a world space position to screen space, use D3DXVec3Project().

Share on other sites
You got really close. But you converted your world coordinates into view space, not screen space. View space goes from -1 to 1. You then ratio that to your current screen width/height.

Using D3DXProject is probably easiest overall.

Share on other sites
Quote:
 Original post by BuckeyeYou got really close. But you converted your world coordinates into view space, not screen space. View space goes from -1 to 1. You then ratio that to your current screen width/height.

I don't think you mean "view space". [grin]

Share on other sites
View space? You said, view space?? I'm an idiot.. maybe.. projection? [sigh]

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 17
• Forum Statistics

• Total Topics
632155
• Total Posts
3004485

×