Sign in to follow this  

Transforming Points from World Space to Screen Coordinates

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

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[i],&plane[i],&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 this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 2848 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.

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