Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

Projecting a shape into screen space coordinates gives odd results.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to project a shape from its object coordinates into screen space so it will always face a camera.

I start by drawing a yellow triangle in world space using a perspective transform:



Vector3 a = new Vector3(0, 0, 0);
Vector3 b = new Vector3(0, 2, 0);
Vector3 c = new Vector3(1, 0, 0);

game.WireShapeDrawer.Begin(game.ActiveCamera.ViewMatrix, game.ActiveCamera.ProjectionMatrix);

game.WireShapeDrawer.DrawWireTriangle(a, b, c, Color.Yellow);

game.WireShapeDrawer.End();


I now use the built in XNA function to project the vertices a, b and c to screen space:



a = viewport.Project(a, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);
b = viewport.Project(b, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);
c = viewport.Project(c, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);


Each coordinate requires dividing by the width and height in order to be in the [-1, 1] range and the y axis is flipped:



a.X /= viewport.Width;
a.Y /= -viewport.Height;
b.X /= viewport.Width;
b.Y /= -viewport.Height;
c.X /= viewport.Width;
c.Y /= -viewport.Height;


The projected triangle is now drawn to the screen using no transformation (Matrix.Identity):



game.WireShapeDrawer.Begin(Matrix.Identity, Matrix.Identity);

game.WireShapeDrawer.DrawWireTriangle(a, b, c, Color.Purple);

game.WireShapeDrawer.End();


The final result produces a triangle that doesn't face the camera and doesn't appear to project onto 2D correctly:

IncorrectScreenProjection.png


My projection matrix is created as follows:



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

float yZoom = 1f / (float)Math.Tan(MathHelper.ToRadians(GlobalSettings.FOV_X) * 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;
}


The view matrix is as follows:



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;


Where might this be going wrong?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!