- The first thing you have to do is set up the environment for 3D
viewing.
To do this you have to:
- In resizeGL():
- We draw inside an area of the screen known as the
viewing volume. In the last assignment the viewing
volume was more like a viewing plane (or like a sheet of
paper).
- Change the projection mode from gluOrtho2D(). Notice
the '2D' bit.
- We can change this to a 3D orthographic projection by
using:
- glOrtho( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
- The parameters are (left, right, top, bottom,
near, far).
- An orthographics projection is much like a
technical drawing where no account for perspective
is taken into the drawing.
- With an Orthographic projection the viewing volume
is a parallelpiped (a box).
- Objects are drawn the same size regardless of their
distance from the viewer.
- The cube would appear end on in the top right corner
of the screen as such:
Or if it is rotated slightly:
- We usually like to use a persepctive type drawing for
a more realistic view of the object.
- Only objects inside the viewing volume are drawn.
- Objects closer to the viewer are drawn larger.
- In a perspective projection the viewing volume is
a frustum of a pyramid, ie a truncated
pyramid. You can specifuy this in one of two ways:
- glFrustum()
- gluPerspective()
- In this example I use glFrustum( -1.0, 1.0, -1.0, 1.0,
5.0, 15.0 );
The parameters are (left, right, top, bottom, near,
far).
NB. the near and far are not z-coordinates as the
left, right, near, far parameters are. Near and far are
distances from the eye to the front and back clipping
planes of the scene.
- If an objects, or part of an objbect, moves
outside of this volume it is clipped (that part of
the object is not rendered).Observe
what happens when the corner of the cube is closer
to the eye/camera than the near clipping plane.
- setXRotation() & setYRotation()
- I have also added setXRotation() and setYRotation()
routines that are almost exactly the same as the
setZRotation() from the 2D lab exercise.
- The mouseMoveEvent() function has changed to allow
rotations on the 3 axes (rememeber our discussion in lab
about sensible rotations for a 2D scene).
The changes are:
- using the left mouse button for rotations, intead of
changing the calues for the zRot variable they now
change the values for xRot and yRot.
- the middle moouse button now can perform a zoom
operation, ie a translation on z by changing the value
of zTrans.
- The bulk of the changes are made to the paintGL()
function. These are:
- First we need to draw a cube. Instead of using
glBegin(GL_POLYGON); to draw a square, use 6 polygons or
quads (or some variation) to draw a cube. Hint: draw it on
paper first and think about what the vertex coordintes
will be. Think about inside and outside facing polygons
and the anti-clockwise ordering scheme.
- Remember our discussion in lab about buffers and what
glClear( GL_COLOR_BUFFER_BIT); does.
- In 3D we also need to use the depth buffer. The
depth buffer is required for doing 3D models. This
enables drawing in the correct order so that
polygons/objects/etc closer to the eye/screen are
drawn on top of the things in the distance.
- Without depth buffering the image may look like this:
- We need to add the depth buffer information. We do
this by changing the above glClear() function call
to include the depth buffer:
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- Then, we need to enable the depth test in the
drawing routine so that it tests to see if a
polygon, or indeed part of a polygon should be
drawn. This is accomplished by:
glEnable(GL_DEPTH_TEST);
- We add in the code to make sure the
transformations are applied. This means the mouse
movements and the new sliders for the y and z
rotations. This should be straight forward enough,
for you to do on your own.
Remember to check the documentation.
Read chapter 3 of the OpenGL Programming Guide: Viewing. Pay special
attention to Matrices and how OpenGL deals with them.
Here is an updated and re-commented version of the files.
www.cpsc.ucalgary.ca/~pj/453/box/3Dcube/
Useful References
OpenGL Programming Guide
(The Red Book).
OpenGL Index in Alphabetic Order
(This is pretty long)
The OpenGL Website
The OpenGL Website - tutorials
Contact me
email: pj@cpsc.ucalgary.ca
Tel: (403) 220 7041.