# OpenGL drawing 3d camera axis fixed location on screen

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

## Recommended Posts

Hi, The effect I want is similar to that seen in some 3D modeling applications such as 3D Studio. Essentially, three colored GL lines representing the X, Y, and Z axis of the rotation of the camera that stay in a corner of the screen. Conceptually I have a pretty clear idea of what I need to do, but being a relative OpenGL newbie I'm not sure how to accomplish the effect. Can someone post some pseudo-code of what I'd need to do or anything that would help get me started? The issue for me isn't how to get the orientation of the camera or the drawing the lines, it's how to make sure they stay to scale in one corner of the screen. Thanks!

##### Share on other sites
Hmm for not having tried this I will take a stab.
glLoadIdentity();glPushMatrix();glTranslatef(//move to where you want the xyz lines to be);glBegin(GL_LINES);//draw lines based on camera positionglEnd();glPopMatrix();

thats off the top of my head without thinking about it to much...

##### Share on other sites
well, that would just draw the axis at a given location. with your snippet, you'd still have to specify glVertex3*(x,y,z) somewhere, and once the camera moves you'd have to take its position into account.

i'm thinking i either need something like render to texture, then map the texture to a 2d point on screen using orthographic projection. or i need a way to draw the axis at a point always ahead and down of the camera, but i'm not sure if that would really work very well.

any ideas?

##### Share on other sites
Quote:
 Original post by Silexwell, that would just draw the axis at a given location. with your snippet, you'd still have to specify glVertex3*(x,y,z) somewhere, and once the camera moves you'd have to take its position into account.

Why? You just care about it's orientation. What's your camera looking like? You should know at least it's forward and up vectors (at least a rough up so two crossproducts will give you the rest). The position doesn't matter, that's why you load the identity first. Absolutely no reason to panic over "where is my camera, where will I have to draw the lines to make them appear in the corner".

But as you said you're new to OpenGL I will try to elaborate (an ugly thing, if I would be good at explaining I'd be a teacher).
I assume you use some kind of helper like gluLookAt or maybe a bunch of rotate/translate calls before drawing. The former sets up the modelview matrix with the inverse of your "camera matrix" (ie. it's right,up,fwd,position vectors that form it's local coordinate system). The latter would apply the translations and rotations to the modelview. However, whenever you want something in a fixed position on the screen you don't care about any of that. You wipe the modelview and load the identity and after that the point x,y,z will always be at the same screen position (well, viewport position technically, but only important if it's not filling the whole window). There are always two ways. The somewhat pointless way is to transform and align your menus/hud/whatever with the camera and the other is to set the camera back to it's default position.

And to throw in some math: the modelview is containing the inverse of the camera matrix (to transform everything "the other way"). To align your stuff with the camera you would have to transform it with the camera matrix. What happens when you apply a matrix and then it's inverse? Nothing, because the result is the identity. So obviously instead of multiplying two matrices of which you already know will result in the identity you can just go and set it in the first place. Which is exactly what the code above is doing. Except that you should push (save) the current matrix before loading the identity unless these lines are the last thing you will be drawing each frame anyway.

And if you want to be REALLY lazy, you could do this inside:

  translate a bit to side/down/fwd (ie. the corner)  glMultMatrix with camera matrix (but position part -idx 12-14- set to 0, ie only apply rotation)  glLines    (-1,0,0)(1,0,0)    (0,-1,0)(0,1,0)    (0,0,-1)(0,0,1)

Technically it doesn't really matter if you have the axes in the matrix or the glVertex calls

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 16
• 15
• 28
• 12
• 14
• ### Forum Statistics

• Total Topics
634805
• Total Posts
3019366
×