How are the coordinate axes drawn in this video?

Started by
13 comments, last by szecs 12 years, 11 months ago
In 3DS Max there is a coordinate cross that allows you to see the current orientation of the camera. See the bottom left hand corner of this video.

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?
Advertisement
Draw three lines in model space as the axis you would expect.

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.
Excellent explanation. Thank you. :)
I've been trying to implement this based on your instructions but it's not working out as intended.I have defined my 3 lines that create the coordinates axes along with a quadrangle to label each axis:


// 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.Color = vertices.Color;
v.Position = Vector3.Transform(vertices.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[/quote]

Done using Matrix.CreateScale()

Rotate them based on the camera rotation [/quote]

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[/quote]

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[/quote]

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.[/quote]

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?
I'm now using the followings transforms:

{ 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?
Well, AFAIK the little coordinate system is just an ORTHOGONAL projection there, with the appropriate viewport settings (little square at the bottom-left). And only the rotation is applied to the axes.

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 :P

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



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.
Well, it's not really educational. I just threw together the whole thing, so immediate mode etc, all the horrible deprecated thing you can imagine.

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):

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

code
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);
}

Thank you very much szecs. Much appreciated :)
I've followed your instructions but I can't get the text/quadrangles that label each axis to face the camera. The problem is shown in this video.

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?

This topic is closed to new replies.

Advertisement