2
votes

Can someone enlighten me what the correct parameters to gluPerspective would be, for the wire cube to be visible similar to how glOrtho renders it in this example? (glOrtho works if you swap it for gluPerspective). As is, this displays a blank window.

I tried cargo-culting (a) many different values to gluPerspective, (b) different sized cubes, (c) different camera angles, (d) translating to different spots on Z; none seem to work. I didn't try adjusting glViewport, my understanding is that it's not needed for a simple example like this.

Also do I understand correctly that glOrtho, gluPerspective and glFrustum are normally mutually exclusive (they're all different ways of specifying a viewing volume) and all are equally usable with gluLookAt?

void display() {
    glClearColor (1, 1, 1, 0);
    glClear (GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //glOrtho (0, 500, 0, 500, -100, 100);
    gluPerspective (20, 1, 1, 100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (0, 0, 0, 0, 0, -1, 0, 1, 0); // these are the defaults
    glColor3f(1, 0, 0);
    glTranslatef (250, 250, 0);
    glScalef (2, 2, 2);
    glutWireCube (100.0);
    glFlush();
}

void main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH) ;
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    int windowHandle = glutCreateWindow ("COMP 390 TME  2 Question 1");
    glutSetWindow (windowHandle);
    glutDisplayFunc (display);
    glutMainLoop();
}
1

1 Answers

2
votes

It is simply impossible to visualize the full cube with only modifying the perspetive parameters. What you have set up is the following, in top view, where +x is going right, +z is going down:

      viewing direction   ^
                          |                 +-+
                          +                 | |
                                            +-+
                       camera at        cube, centered
                        origin         around (250,250,0) 

Now with an orthogonal view, you can actually include object behind the camera. But with a perspecitve one, this is not possible (at least not in a useful sense, you of course can shift the frustum around, but the projection center will then shift with it, so you basically would move the camera). All you can do is using an insanely wide field of view angle (near to 180 degree) to get part of the cube visble. But you cannot set up anything with gluPerspecitve() that making you see the front half of that cube.

I suggest you use some more normal projection and just look at the direction of the cube. If you change that lookat to

gluLookAt (0, 0, 0,  1, 1, 0, 0, 1, 0);

so that you look to the cube, you can use some usual perspective like

gluPerspective (90, aspect, 10, 1000);

Note that aspect should be the width of your viewport divided by the height. This is required so that the image is undistorted, and a square is acutally shown as a square.

The cube might appear very small, since it is only 4 units big, but 250 apart. YOu could of course use a smaller field of view angle ("zooming" with a camera), or just move the camera closer to the cube, or the other way around.