**1**

# How are the coordinate axes drawn in this video?

###
#1
Members - Reputation: **230**

Posted 04 May 2011 - 03:37 PM

How are those axes drawn to the screen so they stay in the same place and still give a sense of depth? Is it some kind of orthographic projection?

###
#2
Members - Reputation: **471**

Posted 04 May 2011 - 04:47 PM

Line 0: <1,0,0>, <0,0,0>

Line 1: <0,1,0>, <0,0,0>

Line 2: <0,0,1>, <0,0,0>

Scale them to the appropriate size. Rotate them based on the camera rotation. Position them in front of the camera, but do not transform them from world to view space. Instead skip the world transform and assume they are in view space already. Also, turn off depth writes so it overlaps anything else that renders (make it essentially part of the UI).

You're are essentially rendering UI, but instead of using an orthographic projection you are using a perspective projection.

###
#4
Members - Reputation: **230**

Posted 05 May 2011 - 05:52 AM

// X vertices[0] = new VertexPositionColor(Vector3.Zero, Color.Red); vertices[1] = new VertexPositionColor(Vector3.UnitX, Color.Red); quadrangles[0] = new Quadrangle(vertices[1].Position, 0.25f, 0.25f, GlobalSettings.FONT_INDEXSTART + 33); // Y vertices[2] = new VertexPositionColor(Vector3.Zero, Color.Green); vertices[3] = new VertexPositionColor(Vector3.UnitY, Color.Green); quadrangles[1] = new Quadrangle(vertices[3].Position, 0.25f, 0.25f, GlobalSettings.FONT_INDEXSTART + 34); // Z vertices[4] = new VertexPositionColor(Vector3.Zero, Color.Blue); vertices[5] = new VertexPositionColor(-Vector3.UnitZ, Color.Blue); quadrangles[2] = new Quadrangle(vertices[5].Position, 0.25f, 0.25f, GlobalSettings.FONT_INDEXSTART + 35);

For the shader I am just passing through the vertex positions and not transforming them in any way:

VertexShaderOutput VertexShaderFunction(VertexShaderInput input) { VertexShaderOutput output; output.Position = input.Position; output.Colour = input.Colour; return output; }

The transformation is done in the Draw method instead. Here is what I am using to draw the lines:

VertexPositionColor[] v = new VertexPositionColor[6]; // Set the translation based on the current camera's position Vector3 translation = game.ActiveCamera.Position + game.ActiveCamera.Forward; Matrix transform = Matrix.CreateScale(0.25f); //transform.Right = game.ActiveCamera.Right; //transform.Up = game.ActiveCamera.Up; //transform.Forward = game.ActiveCamera.Forward; transform.M41 = translation.X; transform.M42 = translation.Y; transform.M43 = translation.Z; //transform *= game.ActiveCamera.ViewMatrix; //transform *= game.ActiveCamera.ProjectionMatrix; //transform *= game.ActiveCamera.ViewProjectionMatrix; // Transform all the vertices for (int i = 0; i < 6; ++i) { v[i].Color = vertices[i].Color; v[i].Position = Vector3.Transform(vertices[i].Position, transform); } Effect effect = game.Effects["PreTransformed"]; // Disable the z buffer so that the axes are drawn on top of all other objects game.GraphicsDevice.DepthStencilState = DepthStencilState.None; effect.CurrentTechnique.Passes[0].Apply(); game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>( PrimitiveType.LineList, v, 0, 6, indices, 0, 3 );

Scale them to the appropriate size

Done using Matrix.CreateScale()

Rotate them based on the camera rotation

This fixes the axes based on the current camera rotation and does not draw the lines down the cardinal axes as intended.

Position them in front of the camera, but do not transform them from world to view space

The translation vector puts them in front of the camera but they will not draw to the screen and be visible unless I transform them by the camera's ViewProjection matrix:

transform *= game.ActiveCamera.ProjectionMatrix;

Also, turn off depth writes so it overlaps anything else that renders

Depth write has been turned off using:

game.GraphicsDevice.DepthStencilState = DepthStencilState.None;

You're are essentially rendering UI, but instead of using an orthographic projection you are using a perspective projection.

I have tried transforming the vertices by just the camera's projection matrix but they will not draw to the screen in the correct position unless I move the camera to the origin.Can you please explain why this is the case as I thought I had followed your instructions precisely?

###
#5
Members - Reputation: **230**

Posted 06 May 2011 - 05:47 AM

{ Axes }

// Set the translation based on the current camera's position and an x, y, z offset float xOffset = 0.4f; float yOffset = 0.2f; float zOffset = 1f; Vector3 translation = camera.Position; translation += camera.Right * xOffset; translation += camera.Up * yOffset; translation += camera.Forward * zOffset; Matrix transform = new Matrix(); transform.M11 = 0.125f; transform.M22 = 0.125f; transform.M33 = 0.125f; transform.M41 = translation.X; transform.M42 = translation.Y; transform.M43 = translation.Z; transform.M44 = 1f;

{ Axis Labels }

// Transforming by camera rotation will make the fonts always face the camera transform.Right = game.ActiveCamera.Right * 0.125f; transform.Up = game.ActiveCamera.Up * 0.125f; transform.Forward = game.ActiveCamera.Forward * 0.125f;

Which produces a final result shown in this video.

The white quadrangles always face the camera but they have no depth and do not follow the end points of the axes.

Why is this occurring?

###
#6
Members - Reputation: **2185**

Posted 06 May 2011 - 06:39 AM

I took a closer look at 3DS MAX, I'm positive that it uses orthogonal projection and not perspective. You can see that the axes are somewhat off and not parallel with 3D axis oriented lines in the view.

You really have to learn (you in general) that you can set whatever projection/modelview/viewport whenever you like! Just reset then set everything after drawing the scene. Then reset again to draw the HUD if you have one. Don't be possessed by the idea that if you have set up a projection, you have to stick with it through all the lifespan of the application! This really should be made a sticky. We have this king of question every day.

BTW I implemented the same thing in my paper modeller, so I know what I'm talking about

###
#7
Members - Reputation: **230**

Posted 06 May 2011 - 09:48 AM

BTW I implemented the same thing in my paper modeller, so I know what I'm talking about

I looked at your paper modeler and those axes are ideal. Would you be willing to share that area of code with me/others on the board? If not could you outline that area of the code in pseudo code perhaps?

I've tried an orthogonal projection/camera but it remains 2D with no perspective, and no sense of depth, so I'm struggling a little.

###
#8
Members - Reputation: **2185**

Posted 06 May 2011 - 10:15 AM

Hmm, or maybe not, the logic itself is quite clear and nice, only the drawing code is "deprecated".

So the logic (to be honest, that should be pretty obvious):

- set viewport to the bottom left corner
- disable depth buffer
- reset projection matrix (load identity)
- set an orthogonal projection: a cube with the center at 0,0,0
- get the camera/modelview matrix
- set it's last column (the translate part) to 0,0,0,1
- load that as the modelview matrix
- draw axes:1,0,0; 0,1,0; 0,0,1
- draw texts

void RenderCoordinateSystem() { int old_viewport[4]; glDisable(GL_DEPTH_TEST); glGetIntegerv(GL_VIEWPORT,old_viewport); glViewport(0,0,90,90); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-45,45,-45,45,-45,45); glMatrixMode(GL_MODELVIEW); TransformWithCameraRotation(); glBegin(GL_LINES); glColor4f(1,0,0,1); glVertex3f(0,0,0); glVertex3f(38,0,0); glColor4f(0,1,0,1); glVertex3f(0,0,0); glVertex3f(0,38,0); glColor4f(0,0,1,1); glVertex3f(0,0,0); glVertex3f(0,0,38); glEnd(); glColor4f(1,1,1,1); glPrint(40,0,0,"x"); glPrint(0,40,0,"y"); glPrint(0,0,40,"z"); glViewport(old_viewport[0],old_viewport[1],old_viewport[2],old_viewport[3]); glEnable(GL_DEPTH_TEST); } void TransformWithCameraRotation() { double mvm[16]; mvm[ 0] = MainCamera.mv_matrix[ 0]; mvm[ 1] = MainCamera.mv_matrix[ 1]; mvm[ 2] = MainCamera.mv_matrix[ 2]; mvm[ 3] = MainCamera.mv_matrix[ 3]; mvm[ 4] = MainCamera.mv_matrix[ 4]; mvm[ 5] = MainCamera.mv_matrix[ 5]; mvm[ 6] = MainCamera.mv_matrix[ 6]; mvm[ 7] = MainCamera.mv_matrix[ 7]; mvm[ 8] = MainCamera.mv_matrix[ 8]; mvm[ 9] = MainCamera.mv_matrix[ 9]; mvm[10] = MainCamera.mv_matrix[10]; mvm[11] = MainCamera.mv_matrix[11]; mvm[12] = 0; mvm[13] = 0; mvm[14] = 0; mvm[15] = 1; glLoadMatrixd(mvm); }

###
#10
Members - Reputation: **230**

Posted 09 May 2011 - 06:24 AM

I'm drawing the axes as instructed:

// Set viewport to the bottom left corner Viewport viewport = game.GraphicsDevice.Viewport; // Disable the z buffer so that the axes are drawn on top of all other objects game.GraphicsDevice.DepthStencilState = DepthStencilState.None; Matrix orthogonal = Matrix.CreateOrthographic( viewport.Width, viewport.Height, -viewport.Width, viewport.Width ); Camera camera = game.ActiveCamera; Effect effect = game.Effects["PreTransformed"]; // Get the camera/modelview matrix Matrix viewMatrix = camera.ViewMatrix; // Set it's last column (the translate part) to 0,0,0,1 viewMatrix.M41 = 0f; viewMatrix.M42 = 0f; viewMatrix.M43 = 0f; viewMatrix.M44 = 1f; effect.Parameters["ViewProjection"].SetValue(viewMatrix * orthogonal); effect.CurrentTechnique.Passes[0].Apply(); game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>( PrimitiveType.LineList, vertices, 0, 6, indices, 0, 3 );

If I use 'viewMatrix * orthogonal' for drawing the labels then they never rotate to face the camera (like a spherical billboard). How do I draw the labels so they rotate correctly?

###
#11
Members - Reputation: **2185**

Posted 09 May 2011 - 06:30 AM

Anyway: Project the coordinates of the labels (I assume you know what I mean by that), reset the modelview matrix, and render the labels just like you would render a HUD. I can't be more specific, play around and experiment!

###
#12
Members - Reputation: **230**

Posted 09 May 2011 - 06:48 AM

Project the coordinates of the labels (I assume you know what I mean by that)

This is where I think I am going wrong:

- If I draw the labels just using an orthographic projection they do not move but will always face the camera (like a GUI) when the camera rotates.

- If I transform them by the camera's view matrix with 0,0,0 for the translation they will be in the correct location but they will not face the camera.

Can you please explain what you mean by "project the coordinates of the labels"?

###
#13
Members - Reputation: **2185**

Posted 09 May 2011 - 07:12 AM

To be honest, you should learn how to solve these problems. I could do it, no one showed me. All the pieces of the puzzle (or terms to look into) are here in the thread. You just need to put together these few pieces.

###
#14
Members - Reputation: **230**

Posted 11 May 2011 - 02:56 PM

look into "world coordinates to screen coordinates"

Can you please explain why I should use this method? I've seen it used as a Unity add on here but I have solved it by doing the following:

// Always use the active camera to get the label to face the current camera Camera camera = game.ActiveCamera; Viewport viewport = game.GraphicsDevice.Viewport; // Scale //Matrix worldMatrix = Matrix.CreateScale(0.5f); Matrix worldMatrix = Matrix.CreateScale(50f); // Rotate worldMatrix *= camera.ViewMatrix; // Set its last column (the translation part) to (0, 0, 0, 1) worldMatrix.M41 = 0f; worldMatrix.M42 = 0f; worldMatrix.M43 = 0f; worldMatrix.M44 = 1f; // The transpose of view matrix to make the vertices face the current camera Matrix.Transpose(ref worldMatrix, out worldMatrix); // Translate worldMatrix.M41 = ObjectPosition.X; worldMatrix.M42 = ObjectPosition.Y; worldMatrix.M43 = ObjectPosition.Z; vertices[0] = Vector3.Transform(corners[0], worldMatrix); vertices[1] = Vector3.Transform(corners[1], worldMatrix); vertices[2] = Vector3.Transform(corners[2], worldMatrix); vertices[3] = Vector3.Transform(corners[3], worldMatrix); Matrix orthogonal = Matrix.CreateOrthographic( viewport.Width, viewport.Height, -viewport.Width, viewport.Width ); Matrix viewMatrix = camera.ViewMatrix; // Set its last column (the translation part) to (0, 0, 0, 1) viewMatrix.M41 = 0f; viewMatrix.M42 = 0f; viewMatrix.M43 = 0f; viewMatrix.M44 = 1f; game.WireShapeDrawer.Begin(viewMatrix, orthogonal); game.WireShapeDrawer.DrawLine(vertices[0], vertices[1], Color.White); game.WireShapeDrawer.DrawLine(vertices[1], vertices[3], Color.White); game.WireShapeDrawer.DrawLine(vertices[3], vertices[2], Color.White); game.WireShapeDrawer.DrawLine(vertices[2], vertices[0], Color.White); game.WireShapeDrawer.End();

The video of the results are shown here

I hope that someone can enlighten me why conversion to screen space coordinates are often used instead.