Results 1 to 3 of 3

Thread: 2 different view frustums

  1. #1
    Senior Member Regular Contributor
    Join Date
    Aug 2003
    Posts
    369

    2 different view frustums

    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:
    Code :
    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:
    Code :
    // 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:
    Code :
    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:
    Code :
    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!

  2. #2
    Guest

    Re: 2 different view frustums

    try a reshape like this:

    Code :
    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:

    Code :
    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)

  3. #3
    Senior Member Regular Contributor
    Join Date
    Aug 2003
    Posts
    369

    Re: 2 different view frustums

    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!

Similar Threads

  1. newbie view location / view direction in textfield
    By newsb in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-12-2011, 02:15 PM
  2. Problems with frustums, etc...
    By Hiram in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-16-2011, 03:09 AM
  3. Creating a Bounding Volume for Camera Frustums
    By Mattagus in forum OpenGL: General
    Replies: 5
    Last Post: 12-25-2010, 01:30 AM
  4. OT- Intersection of two Cones/ Frustums
    By Sundy in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 07-16-2002, 10:51 AM
  5. how to create 2 Frustums for stereo
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-22-2001, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean