PDA

View Full Version : 2 different view frustums



dvm
09-12-2004, 01:30 PM
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!

09-12-2004, 02:00 PM
try a reshape like this:


void reshape(int w, int h)
{
glViewport( 0, 0, w, h );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLdouble)w / (GLdouble)h, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}and a display like this:


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);

// projection already set in reshape
// could just set that here...

// camera transform
glLoadIdentity();
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

// object transform next
glRotatef(angle, 0, 0, 1);

// now draw object
glColor3f(0,0,0);
glRectf(-0.5, -0.5, 0.5, 0.5);

// get ready for 2D overlay projection
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho( 0, w, 0, h, -1, 1 );

// camera identity
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// draw text overlays
drawBitmapString("This is only a test.", 0, 0, 0);

// clean up
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

// swap
glutSwapBuffers();
glutPostRedisplay();
}i hope this helps! (im half out of my mind on coffee right now) :eek:

dvm
09-12-2004, 05:35 PM
Thanks for the help. It really worked. And boy was I relieved to see that my solution didn't work...after finding out that I had hardcoded the color for the text in white (on a white background). So fortunately your solution was not in vain! Really apreciate it! Thanks a bunch!