# Converting world space coordinate to screen space coordinate and getting incorrect range of values

I'm attempting to convert from world space coordinates to screen space coordinates.

I have the following code to transform my object position

Vector3 screenSpacePoint = Vector3.Transform(object.WorldPosition, camera.ViewProjectionMatrix);

The value does not appear to be in screen space coordinates and is not limited to a [-1, 1] range.

What step have I missed out in the conversion process?

EDIT:

Projection Matrix

Perspective(game.GraphicsDevice.Viewport.AspectRatio, nearClipPlaneZ, farClipPlaneZ);

private void Perspective(float aspect_Ratio, float z_NearClipPlane, float z_FarClipPlane)
{
nearClipPlaneZ = z_NearClipPlane;
farClipPlaneZ = z_FarClipPlane;

float yZoom = 1f / (float)Math.Tan(fov * 0.5f);
float xZoom = yZoom / aspect_Ratio;

matrix_Projection.M11 = xZoom;
matrix_Projection.M12 = 0f;
matrix_Projection.M13 = 0f;
matrix_Projection.M14 = 0f;

matrix_Projection.M21 = 0f;
matrix_Projection.M22 = yZoom;
matrix_Projection.M23 = 0f;
matrix_Projection.M24 = 0f;

matrix_Projection.M31 = 0f;
matrix_Projection.M32 = 0f;
matrix_Projection.M33 = z_FarClipPlane / (nearClipPlaneZ - farClipPlaneZ);
matrix_Projection.M34 = -1f;

matrix_Projection.M41 = 0f;
matrix_Projection.M42 = 0f;
matrix_Projection.M43 = (nearClipPlaneZ * farClipPlaneZ) / (nearClipPlaneZ - farClipPlaneZ);
matrix_Projection.M44 = 0f;
}


View Matrix

        // Make our view matrix
Matrix.CreateFromQuaternion(ref orientation, out matrix_View);

matrix_View.M41 = -Vector3.Dot(Right, position);
matrix_View.M42 = -Vector3.Dot(Up, position);
matrix_View.M43 = Vector3.Dot(Forward, position);
matrix_View.M44 = 1f;

// Create the combined view-projection matrix
Matrix.Multiply(ref matrix_View, ref matrix_Projection, out matrix_ViewProj);

// Update the bounding frustum
boundingFrustum.SetMatrix(matrix_ViewProj);

• you should specify the rendering technology that you are using – user827992 Aug 30 '12 at 16:13
• I'm using OpenGL with C++ and DirectX for C#. I am looking to understand why the projection is not working as expected. In this case it is not producing the range of values I would be expect while the object in question is moved around inside the view frustum. – user1423893 Aug 30 '12 at 18:49

XNA has a built-in function for calculating world-to-screen space coordinates called Viewport.Project. It also has the opposite function (Unproject) for turning screen coordinates back into world coordinates (minus the depth value).

As far as why your implementation isn't working, I'm not 100% sure but here's a few ideas:

• You're using Vector3.Transform, but your ViewProjectionMatrix is 4x4, so you may be missing the homogenous W divide. I'm not sure whether Vector3.Transform takes this into account.
• If your object's WorldPosition is outside of the view frustum, its post-projective position won't be in the -1 to 1 range. This is how the GPU knows how to clip polygons that lie partially or fully outside the view frustum.
• The range of -1 to 1 is for the unit cube conversion; it's not actually screen space coordinates in the sense of texturing (0.0 to 1.0) or resolution (0.0 to screen width or height).

EDIT: It definitely looks like you're missing the W divide. Vector3.Transform drops the W component in the value it returns. Transform it like this and see if it works:

Vector4 objectPos4 = new Vector4( object.WorldPosition, 1.0f );
Vector4 postProjectivePosition = Vector4.Transform(objectPos4, camera.ViewProjectionMatrix);
float clipSpaceX = postProjectivePosition.X / postProjectivePosition.W;
float clipSpaceY = postProjectivePosition.Y / postProjectivePosition.W;


That should get it into the -1 to 1 range you desire.

• I've seen the XNA version via reflection and it pretty much does the same thing. It doesn't work either with my view * projection matrix. – user1423893 Aug 30 '12 at 18:45
• If your object's WorldPosition is outside of the view frustum, its post-projective position won't be in the -1 to 1 range. I should state that I am moving the object around inside the view frustum and the values are not in range. – user1423893 Aug 30 '12 at 18:46