I’m trying to use gluOrtho2D with glutBitmapCharacter so that I can render text on the screen as well as my 3D objects. However, when I use glOrtho2D, my 3D objects dissapear; I assume this is because I’m not setting the projection back to the OpenGL/GLUT default, but I’m not really sure what that is.
Anyway, this is the function I’m using to render text:
void GlutApplication::RenderString(Point2f point, void* font, string s) { glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0.0, WindowWidth, 0.0, WindowHeight); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glDisable(GL_TEXTURE); glDisable(GL_TEXTURE_2D); glRasterPos2f(point.X, point.Y); for (string::iterator i = s.begin(); i != s.end(); ++i) { glutBitmapCharacter(font, *i); } glEnable(GL_TEXTURE); glEnable(GL_TEXTURE_2D); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); }
And, the render function is similar to this:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glLoadIdentity(); // Do some translation here. // Draw some 3D objects. glPopMatrix(); // For some reason, this stops the above from being rendered, // where the camera is facing (I assume they are still being rendered). Point2f statusPoint(10, 10); RenderString(statusPoint, GLUT_BITMAP_9_BY_15, 'Loading...');
Your code looks okay. Most likely you’ve just messed up the matrix stack somewhere.
I suggest that you check if you forgot a
glPopMatrixsomewhere. To do so you can get the stack depth viaglGet(GL_MODELVIEW_STACK_DEPTH). Getters for the other matrix-stacks are available as well.Also you can take a look at the current matrix. Call
glGetFloatv(GL_MODELVIEW_MATRIX, Pointer_To_Some_Floats)to get it. You can print out the floats each time you’ve set up a modelview or projection matrix. That way you should be able to to find out which matrix erratically ends up as the current matrix.That should give you enough clues to find the bug.