1
votes

I've been studying Computer Graphics and I'm very confused about the role of the viewport, gluortho and when to use GL_MatrixMode and GL_Projection. Here is a sample code I wrote that confuses me.

void init()
{
    glClearColor(1.0,1.0,1.0,1.0);//Background Color of Viewport
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-200,200,-200,200,-50,50);
    glMatrixMode(GL_MODELVIEW);
}

void wheel()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1,0.2,0.2);   
    glLoadIdentity();
    glViewport(0,0,200,200);
    glutSolidCube(100); 
    glFlush();
}
void main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(400,400);//Position from the top left corner
    glutCreateWindow("Car");
    init();

    glutDisplayFunc(wheel);//Shape to draw
    glutMainLoop();
}

When I change the Cube's size to 200 it disappears, why? Is that because it's larger than the z clipping? When I remove glMatrixMode(GL_MODELVIEW) the cube disappears why? If I don't flush at the end of the display function the cube disappears as well,why? When I make the viewport smaller the object get smaller does that mean the object coordinates are relative to the viewport and not the world coordinates?

1

1 Answers

4
votes

When you change the cubes size to 200, its faces extend beyond the near and far clipping planes, which you've set in your glOrtho call to -50 and 50. Technically you'd then be viewing the inside of the cube, but the far side of the cube is also outside of the far clipping plane, so you can't see its backface.

Removing the call to set the matrix mode to GL_MODELVIEW means your glLoadIdentity call operates on the fixed functionality projection matrix (I'm pretty sure), and so the cube is directly translated into Normalized Device Coordinates, and it once again extends beyond all the clipping planes.

Finally, glViewport defines the size of the buffer you should be rendering to, and therefore usually matches your screen size. Making it smaller effectively makes your screen size smaller, but does not change the actual GLUT window size. In mathematical terms, it changes the way fragments are projected from normalized device coordinates into screen coordinates.