OpenGL Matrices

Don't do this lab until you've completed the previous Events lab.

Create a new directory for this lab and copy the cube program from the Events lab into it, with a new name. C programmers: copy the Makefile as well and update it. Java programmers: copy GlutWindow.java.

Projection matrix

In the setProjection code, the first parameter to gluPerspective is the field of view, here set to 60 degrees. Try reducing it to 30, and increasing it to 120.

There is a line which calculates the aspect ratio as the window width divided by the height. Replace this with a line that sets the aspect ratio to 1.0 every time. Run the program and change the window shape to tall and thin, and short and wide. (Restore the original code afterwards.)

The near and far parameters to gluPerspective determine how close or far away objects can be before they are not visible (clipped). Try setting the far clipping plane to a small value, say 5, and then moving backwards or forwards. What happens to the cube?

Modelview matrix

You can retrieve the current modelview transformation matrix with the C code

GLfloat mat[16];
...
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
or in Java
float[] mat = new float[16];
...
gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX, mat, 0);
(Note the extra ,0 at the end of the Java method arguments. Most of the JOGL _v functions require this. It's extremely annoying but Sun don't seem interested in fixing it.)

Get the current matrix immediately after the gluLookAt in setProjection and print the values out as a 4 x 4 grid. (Note: writing loops is much more work than just having four print statements, each printing four values on one line.) Print out ViewPoint as well and compare.

The code

glLoadMatrixf(mat);
replaces the current matrix (PROJECTION or MODELVIEW) by the parameter. Create a matrix with the identity values and use it to replace all glLoadIdentity calls in the program with glLoadMatrix. (For Java, gl.glLoadMatrix(mat, 0))