• Create Account

## Primitive to Screen Coordinates

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1fs1  Members

445
Like
0Likes
Like

Posted 08 December 2013 - 06:37 AM

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, 08 December 2013 - 06:41 AM.

### #2dooz  Members

271
Like
0Likes
Like

Posted 08 December 2013 - 11:10 AM

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.

### #3fs1  Members

445
Like
0Likes
Like

Posted 08 December 2013 - 12:46 PM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.