• Create Account

# How are the coordinate axes drawn in this video?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

Posted 04 May 2011 - 03:37 PM

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?

### #2allingm  Members   -  Reputation: 535

Like
1Likes
Like

Posted 04 May 2011 - 04:47 PM

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.

### #3Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

Posted 04 May 2011 - 05:41 PM

Excellent explanation. Thank you.

### #4Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

Posted 05 May 2011 - 05:52 AM

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

// Y
vertices[2] = new VertexPositionColor(Vector3.Zero, Color.Green);
vertices[3] = new VertexPositionColor(Vector3.UnitY, Color.Green);

// Z
vertices[4] = new VertexPositionColor(Vector3.Zero, Color.Blue);
vertices[5] = new VertexPositionColor(-Vector3.UnitZ, Color.Blue);



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

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{

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?

### #5Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

Posted 06 May 2011 - 05:47 AM

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?

### #6szecs  Members   -  Reputation: 2783

Like
0Likes
Like

Posted 06 May 2011 - 06:39 AM

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

### #7Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

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.

### #8szecs  Members   -  Reputation: 2783

Like
1Likes
Like

Posted 06 May 2011 - 10:15 AM

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

• 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
code
void RenderCoordinateSystem()
{
int old_viewport[4];

glDisable(GL_DEPTH_TEST);

glGetIntegerv(GL_VIEWPORT,old_viewport);

glViewport(0,0,90,90);

glMatrixMode(GL_PROJECTION);

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;

}



### #9Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

Posted 06 May 2011 - 10:35 AM

Thank you very much szecs. Much appreciated

### #10Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

Posted 09 May 2011 - 06:24 AM

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?

### #11szecs  Members   -  Reputation: 2783

Like
0Likes
Like

Posted 09 May 2011 - 06:30 AM

Um, I used the built in text Rendering, that's always in screen space.

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!

### #12Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

Posted 09 May 2011 - 06:48 AM

I've been playing around and experimenting all weekend but with little success.

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"?

### #13szecs  Members   -  Reputation: 2783

Like
0Likes
Like

Posted 09 May 2011 - 07:12 AM

look into "world coordinates to screen coordinates"

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.

### #14Spa8nky  Members   -  Reputation: 230

Like
0Likes
Like

Posted 11 May 2011 - 02:56 PM

After a day away from this, I came back to it today and solved the problem.

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.

### #15szecs  Members   -  Reputation: 2783

Like
0Likes
Like

Posted 12 May 2011 - 12:32 AM

There are more ways to solve a problem, that's an important lesson. You can use billboarding, or conversion of coordinate systems, or maybe other stuff too.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS