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:


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

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

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