Nice tip.
May I point few enhancements ?
1- don't mandate viewport[0] and viewport[1] are null. In the generic case, it's better to call that instead :
glOrtho(vPort[0], vPort[0]+vPort[2], vPort[1], vPort[1]+vPort[3], -1, 1);
2- for pixel-exact positioning, the usage of a slight translation is mandatory, as explained in the
opengl.org's FAQ :
glMatrixMode(GL_MODELVIEW);glPushMatrix();glLoadIdentity();glTranslatef(0.375, 0.375, 0.);
3- disable depth testing while rendering 2D. But this means 2D stuff has to be rendered AFTER all 3D stuff, in case 2D represents some kind of HUD (acronym of Heads Up Display).
So the new code becomes :
void glEnable2D(){ int vPort[4]; glGetIntegerv(GL_VIEWPORT, vPort); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(vPort[0], vPort[0]+vPort[2], vPort[1], vPort[1]+vPort[3], -1, 1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glTranslatef(0.375, 0.375, 0.); glPushAttrib(GL_DEPTH_BUFFER_BIT); glDisable(GL_DEPTH_TEST);}void glDisable2D(){ glPopAttrib(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); }
Keep up the good work =)