PDA

View Full Version : Implementing a Camera + HUD



h3st14
05-09-2012, 02:27 PM
Dear community, I'm glad to write my first post today!

Unfortunately, this is about a struggle between me and opengl, which has lasted a few weeks now, and, even though I could have accomplished some results, I'm still far from my objectives.

I have implemented a basic opengl world, using the glfw libraries, which ( till now ) draws a simple ground plane ( a grid ), and a few squares around. A few days ago, I added support for the "camera", using a vector to store its position, and a quaternion for its rotation. It's taken a while, but it works pretty good.

Can you please confirm me that this is how things should be implemented?

1) INIT ( enabling options, lighting, perspective, etc )
2) CAMERA TRANSFORMS ( translation, rotation )
3) WORLD OBJECTS

Once applied the transformations to the current MODELVIEW matrix to set the camera, I have to use glPushMatrix and glPopMatrix to draw things with respect to the default view, and not the current one ( the one of the camera ), am I right?

So, things should look like:
------------------------
...
//Camera
glTranslated();
glRotatef();

//Object1
glPushMatrix();
glLoadIdentity();
//DRAW
glPopMatrix();

//Object2
glPushMatrix();
glLoadIdentity();
//DRAW
glPopMatrix();

...

---------------

This way everything seems to work. Now, I want to implement a simple HUD, which tells me the current camera orientation with respect to the initial ( default ) one. This can just be represented by three lines ( axis ) which are placed in a corner of my screen, and are rotated accordingly to the quaternion of my camera.

Now, I'm having a few problems trying to implement this. To draw the hud, shouldn't it be enough to start drawing, without using glPushMatrix() and glPopMatrix()? It's not working these way, objects are still drawn with respect to the identity matrix.

Thank you for your suggestions.

BionicBytes
05-10-2012, 04:49 AM
//Object1
glPushMatrix();
glLoadIdentity();
//DRAW
glPopMatrix();

Err, you don't want to reset the ModelView matrix when drawing something you want to leave it alone. Resetting the camera with glLoadIdentity is like not having a camera, so remove this line. When you do, all objects drawn will then be transformed onto the camera space as defined by the ModelView matrix in effect at the time of the draw call.

h3st14
05-10-2012, 11:57 AM
So, basically, the difference between



// draw obj


and



glPushMatrix();
// draw obj
glPopMatrix();


is that, in the first case, I draw with respect to the camera coordinates, and in the latter, to the world coordinates ( default ones )?

Moreover, doesn't glLoadIdentity just affect the "new matrix" I have, after pushing the old one? Afaik, when you call glPushMatrix, you just put your current ( camera ) matrix apart, and start drawing in a completely new one. ut maybe I didn't get it right :/

BionicBytes
05-11-2012, 02:32 AM
pushing the model matrix does not change anything. If the ModelView matrix has been setup with your camera transform then you will be drawing your models and transforming into camera space. If you use glLoadIdentity then you have reset the camera view matrix and reset the model matrix to identity. This means that camera space is the exact same a model space which, in this case, is the same as world space. Just be clear the camera matrix (gluLookat) affects the ModelView matrix - this is a matrix which is the result of two individual matricies that have been pre-multiplied. The View matrix is the camera, and the Model part of ModelView is your model space to world space conversion matrix.