Primitive to Screen Coordinates

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

Recommended Posts

Dear All

It is my first post in here, and may be this has been discussed several times.

I am having no success finding the screen coordinates fron a 3D Primitive Point/Coordinate

I am using the following code:

D3DMATRIX matrix;
D3DXVECTOR4 position;
D3DXVECTOR4 input;
D3DVIEWPORT9 viewport;
GetViewport(&viewport);
input.x = 0.0f; // ==> I replace the 0.0 with my x coordinate
input.y = 0.0f; // ==> I replace the 0.0 with my y coordinate
input.z = 0.0f; // ==> I replace the 0.0 with my z coodinate
input.w = 1.0f;
D3DXMatrixTranspose((D3DXMATRIX *)&matrix, (D3DXMATRIX *)&matrix);
position.x = input.x * matrix._11 + input.y * matrix._21 + input.z * matrix._31 + matrix._41;
position.y = input.x * matrix._12 + input.y * matrix._22 + input.z * matrix._32 + matrix._42;
position.z = input.x * matrix._13 + input.y * matrix._23 + input.z * matrix._33 + matrix._43;
position.w = input.x * matrix._14 + input.y * matrix._24 + input.z * matrix._34 + matrix._44;
float x = ((position.x / position.w) * (viewport.Width / 2)) + viewport.X + (viewport.Width / 2);
float y = viewport.Y + (viewport.Height / 2) - ((position.y / position.w) * (viewport.Height / 2));


I also tried the D3DXVec3TransformCoord and D3DXVec3Project code snippets but without success.

I tested drawing these points in screen and they not nearly match the sprites/objects which they represent after going through the DirectX drawing Pipeline.

I appreciate any help!

Thanks

fs1

Edited by fs1

Share on other sites

It looks like you are missing a perspective divide between the matrix multiplication and the viewport transformation.

The projection matrix will transform a point from eye coordinates to clip space, and the perspective divide (divide x,y,z by w) will transform from clip space to NDC (-1..1 in x/y, 0..1 in z). From NDC you apply your viewport scaling (and flip of y-coordinate) to get to screen coordinates.

Share on other sites
Dooz, thank you so much.

I am dividing the position vector elements by position.w, in the following lines:

float x = ((position.x / position.w) * (viewport.Width / 2)) + viewport.X + (viewport.Width / 2);
float y = viewport.Y + (viewport.Height / 2) - ((position.y / position.w) * (viewport.Height / 2));

Is this what you are referring to?

Thanks for your help, I appreciate it.

EDIT: Code is working Perfect

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634112
• Total Posts
3015580
×

Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!