I’ve been through a number of tutorials but still haven’t got the hang of how to draw a 2D HUD with a 3D scene without some sort of a disaster happening. I’ve gathered some sample code and worked out the order in which matrices have to be treated in a certain way to achieve exactly what I’ve set out for. I’ve worked out a rendering code as shown below:
void render()
{
//Clear screen
glClearColor(0.2f, 0.0f, 0.2f, 1.0f); // Clear the background of our window to red
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
// Movement as response to input:
glTranslatef( cam.getEye().getX(), cam.getEye().getY(), cam.getEye().getZ() ); // Translate our object along the y axis
glRotatef( cam.getTheta(), 0.0f, 1.0f, 0.0f ); // Rotate our object around the y axis
// No pushing/popping a matrix and no gluLookAt() used here.
// ==================== Positional reference exercise: ======================
// - plane:
// ... plane-drawing code here ...
// pyramid:
// ... pyramid-drawing code here ...
// =================================== HUD: ==================================
glMatrixMode(GL_PROJECTION);
glPushMatrix();
//glPopMatrix();
glLoadIdentity();
gluOrtho2D(-0.5, 0.5, -0.5, 0.5);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//glPopMatrix();
glLoadIdentity();
glColor3f(0.0f, 0.2f, 0.2f);
// drawing the HUD rect here:
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(640, 0);
glVertex2f(640, 75);
glVertex2f(0, 75);
glVertex2f(0, 0);
glVertex2f(1, 0);
glVertex2f(1, 1);
glVertex2f(0, 1);
glEnd();
glPopMatrix();
glPopMatrix();
//glPushMatrix();
//glPushMatrix();
// ===============================================================================
glutSwapBuffers(); //Send scene to the screen to be shown
}
… but for some reason it only displays my dark-cyan HUD and the 3D scene simply disappeared. What am I doing wrong? What have I missed? I know I’m supposed to push the projection matrix, then the model-view matrix, then do glOrtho2D(), then pop the two matrices off of the stack. That didn’t work. I’m tired of fiddling with pushing and popping matrices in a random order.
Someone said that I’m underflowing the matrix stack with glPushMatrix()/glPopMatrix() calls. I did a little digging and found. The page suggests that I should use a PushStack object, but I don’t know how. So, I started experimenting:
…
glMatrixMode(GL_PROJECTION);
glutil::PushStack stackPusher ( glutil::MatrixStack() );
glLoadIdentity();
gluOrtho2D(-0.5, 0.5, -0.5, 0.5);
glMatrixMode(GL_MODELVIEW);
glutil::PushStack stackPusher ( glutil::MatrixStack() );
glLoadIdentity();
glColor3f(0.0f, 0.2f, 0.2f);
// draw quad
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(640, 0);
glVertex2f(640, 75);
glVertex2f(0, 75);
glVertex2f(0, 0);
glVertex2f(1, 0);
glVertex2f(1, 1);
glVertex2f(0, 1);
glEnd();
…
But that didn’t work because my glutil is missing boost/array.hpp .
Anyone cares to help?