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 from here and here and worked out the order in which matrices have to be treated in a certain way (see here) 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.
// ==================== Drawing scene here: ======================
// ...
// =================================== HUD: ==================================
glMatrixMode(GL_PROJECTION);
glPushMatrix();
//glPopMatrix();
glLoadIdentity();
gluOrtho2D(0, winW, 0, winH); //left,right,bottom,top
glMatrixMode(GL_MODELVIEW);
//glPushMatrix();
glPopMatrix();
glLoadIdentity();
// drawing the HUD rect here:
glBegin(GL_QUADS);
// ...
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.