Float to pixel

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

Recommended Posts

Hiya guys, In my program I have some point sprites which are used for my partical engine, and I need to know what the position of the sprite is on my screen in pixels. Is there anyway to convert the 3D position into pixels? I only need the X and Y values and not Z. Thanks.

Share on other sites
The same way you transform the 3d points into screen space for rendering, but with a twist.

 bool IsVisible = false; D3DXVECTOR3 ScreenPos; D3DVIEWPORT9 VP; D3DXMATRIX V; D3DXMATRIX P; D3DXMATRIX Ident; d3ddev->GetViewport(&VP); d3ddev->GetTransform(D3DTS_VIEW,&V); d3ddev->GetTransform(D3DTS_PROJECTION,&P); D3DXMatrixIdentity( &Ident ); D3DXVec3Project( &ScreenPos,                  &D3DXVECTOR3 (Position.x,Position.y,Position.z),                  &VP,                  &Ident,                  &V,                  &Ident                 ); if ( ScreenPos.z > 0)     IsVisible = true; else     IsVisible = false; D3DXVec3Project(                 &ScreenPos,		 &D3DXVECTOR3 (Position.x,Position.y,Position.z),                 &VP,                 &P,                 &V,                 &Ident                );

The first call transforms the the 3D point into the Viewport using ONLY the view matrix.

This output position's 'Z' value will tell you if the point is in front of the camera or behind it. Greater than 1 and the point is in front of the camera

This is important because the screen position returned by the projection will return a point on the screen even if the point is behind the camera, because it is working with the ray to the point.

The second call transforms the 3D point into the Viewport using the View AND Projection matrices, but NOT the world matrix. this will return the screen position of that point in pixels.

Now remember, the 0,0 point is in the upper left of the screen.

hope this was usefull

Share on other sites
Ah thanks a lot! Of course I have yet to test it :).

Would the same thing work for meshes?

Share on other sites
well it takes A point in 3d and transforms it into screen space. so for a mesh, if you want the position of each vertex, you would have to pass each position separately, if you wanted the center of the mesh, well that is just a single point to transform

Share on other sites
Thanks guys but I do have some questions :)

1) Why is it that the world matrix is never used?
2) Can the viewport parameter be NULL if you only have one?
3) Finally, how does the identity matrix work!?!? I just don't get it.

Thanks again!

Share on other sites
Quote:
 Original post by WinRadThanks guys but I do have some questions :)1) Why is it that the world matrix is never used?2) Can the viewport parameter be NULL if you only have one?3) Finally, how does the identity matrix work!?!? I just don't get it.Thanks again!

1. The world (identify) matrix is used as a "filler" matrix here since any matrix multiplied with the identify matrix will not be changed by the multiplication. The function still needs those arguments though.

2. I would guess not.

3. The identify matrix is the world matrix and is untransformed in the current coordinate system. It has each axis pointing along each of the world axis, is located at the center of the world and with no scaling applied.

Thanks for the info Ebola0001 btw!

Share on other sites
Ah thanks guys it worked out great! I do have a couple questions more though, I promise they are my last :)

1)If the world parameter ain't needed then can't it be set to NULL? Or would that mess up the multiplication?
2)If I don't care about weather or not my object is visible then should I just use the second occurence of the function?

Thanks!

Share on other sites
1) In testing, Yes the project function will return the same value with NULL as with an identity matrix.

2) Yes the first call is only to figure out if the point is in front of the camera, if you don't care then you can use the second call only

an identity matrix of "4x4" size is

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

what this means is

------Inputs------Output--
1x + 0y + 0z + 0w = 1x
0x + 1y + 0z + 0w = 1y
0x + 0y + 1z + 0w = 1z
0x + 0y + 0z + 1w = 1w

so no matter what the input it is just copied to the output

[Edited by - Ebola0001 on August 17, 2008 4:41:01 PM]