Primitive to Screen Coordinates

Started by
1 comment, last by fs1 10 years, 4 months ago

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; 
GetVertexShaderConstantF(0, (float *)&matrix, 4); 
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)); 

The matrix variable holds the World x View x Projection matrix retrieved from the Shader.

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

Advertisement

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.

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

This topic is closed to new replies.

Advertisement