Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Beginners query on perspective

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2005
    Posts
    1

    Beginners query on perspective

    Hi, I need some help on getting the correct perspective when viewing a cube object.

    I'm trying to use the glLookAt function, but am having trouble implementing it when the following function is included:

    ---------
    Code :
    void myReshape(int w, int h)
    {
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
     
        aspectRatio=(GLfloat)w/(GLfloat)h;
     
        if (aspectRatio <= 1.0)
          glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
          2.0 * (GLfloat) h / (GLfloat) w, distance-nearPlane, distance-farPlane);
        else
          glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h,
          -2.0, 2.0, distance-nearPlane, distance-farPlane);
     
        glMatrixMode(GL_MODELVIEW); 
     
        glLoadIdentity();
     
    	gluPerspective(60.0, aspectRatio, 0.1, 40.0);
     
     
     
        glTranslatef(0.0, 0.0, -distance); 
     
    	//redraw the objects
        glutPostRedisplay();
    }
    I want to be positioned inside the cube where i can see the floor, ceiling and 3 walls. Thanks

  2. #2
    Junior Member Regular Contributor
    Join Date
    Jul 2005
    Location
    Berlin, Germany
    Posts
    188

    Re: Beginners query on perspective

    Don't combine glOrtho/gluOrtho2D with glFrustum/gluPerspective. Both set up a view frustum and their interaction is not very intuitive. Also, don't apply glFrustum/gluPerspective to the MODELVIEW stack. It can screw up your lighting (and some other things).

    Code :
    void myReshape(int w, int h)
    {
        aspectRatio=(GLfloat)w/(GLfloat)h;
     
        glViewport(0, 0, w, h);
     
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60.0, aspectRatio, 0.1, 40.0);
     
        //redraw the objects
        glutPostRedisplay();
    }
     
    void myDisplay(void) {
        glMatrixMode(GL_MODELVIEW); 
        glLoadIdentity();
        gluLookAt(
            0, 0, 0,
            0, 0, distance,
            0, 1, 0
        );
     
        // draw objects ...
     
        glutSwapBuffers();
    }
    355/113 -- Not the famous irrational number PI, but an incredible simulation!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •