Hi to everyone, it’s been a while since my last post here.
I’m trying to create a window with 2 view frustums or 2 projection matrices. In the window I want to display a rotating rectangle with say a perspective frustum of:
gluPerspective(45.0f, (GLdouble)w / (GLdouble)h, 0.1f, 100.0f);
Also I’d like to use glut to display some text in the screen. As I think I know (anybody who thinks I’m wrong please correct me) is that for the text to display easily and properly, one should have an orthographic projection equal to the width and height of the window, like:
// let w, h be the window width and height respectively.
glOrtho(- (w / 2.0), w / 2.0, -(h / 2.0), (h / 2.0), -1.0, 1.0);
That should put the raster center right in the center of the window and go from there.
But what I’m trying to do is change the frustum for writing the font inside the glut “display” function. So my “reshape” is:
void reshape(int w, int h)
{
glViewport(g_vVprCoordinates[0],
g_vVprCoordinates[1],
g_vVprCoordinates[2],
g_vVprCoordinates[3]); //define the viewport
glMatrixMode(GL_PROJECTION); // change the matrix mode to projection
glLoadIdentity(); // load the identity matrix in the trasnformation matrix
gluPerspective(45.0f, (GLdouble)w / (GLdouble)h, 0.1f, 100.0f); //define the viewing volume to be
// 45 degrees wide, have an aspect ratio of w/h, while the
// near drawing plane is 0.1 units from the center of projection
// and the far drawing plane is 100 units from the
// center of projection
glMatrixMode(GL_MODELVIEW); // change back to the modelview matrix
glLoadIdentity(); // same as above
}
and the display function is:
void display()
{
static float angle = 0.0;
angle += 0.5;
if (angle >= 360.0)
angle -= 360.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color and depth buffers
glLoadIdentity(); // load the identity matrix in the trasnformation matrix
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0); // position the camera in the world (makes a series of rotations and translations
glColor3f(0, 0, 0);
glRotatef(angle, 0, 0, 1);
glRectf(-0.5, -0.5, 0.5, 0.5); // create a simple white rectangle in the center of the screen
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(-(g_vWinCoordinates[3] - g_vWinCoordinates[0]) / 2.0,
(g_vWinCoordinates[2] - g_vWinCoordinates[0]) / 2.0,
-(g_vWinCoordinates[3] - g_vWinCoordinates[1]) / 2.0,
(g_vWinCoordinates[3] - g_vWinCoordinates[1]) / 2.0,
-1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawBitmapString("This is only a test.", 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glutSwapBuffers(); // tell glut to swap the front and back buffers
glutPostRedisplay(); // ask glut to re-execute the display callback
}
What I’m trying to do is first draw the rectangle, push the current modelview into the stack, change to projection (which is the perspective projection), push this matrix as well, change into an orthographic projection, change back to the modelview matrix, clear it and draw the string. Then change back to the projection matrix, pop the matrix to get the old perspective back and finally change back to the modelview and pop that as well.
Of course all this is based in the assumption that OpenGL would hold 2 different matrices for the projection and modelview but it doesn’t work. I can see the rectangle but can’t see the text.
Can someone point out to me why? Is there a link you know where I can see how I can do it? Thanks for your time!