Jump to content
  • Advertisement
Sign in to follow this  
bababooey

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.

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,&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 this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!