Transforming Points from World Space to Screen Coordinates

Started by
3 comments, last by Buckeye 14 years, 1 month ago
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,&world,&view); //start with world, multiply by view
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?
Advertisement
To convert a world space position to screen space, use D3DXVec3Project().
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
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.


I don't think you mean "view space". [grin]
View space? You said, view space?? I'm an idiot.. maybe.. projection? [sigh]

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement