3D graphics adds a Z coordinate
OpenGL coordinate system is right-handed - +X to the right, +Y up, +Z out of screen
Other software or application domains may use other coordinate systems
The 3D viewing volume is controlled by glOrtho.
This is an axis-aligned box containing the region to be drawn.
glOrtho(left, right, bottom, top, near, far)
Note that near and far can be confusing - they are the negatives of the Z values for the corresponding planes.
glOrtho(-5, 8, -4, 4, -1, 3)
All OpenGL transformation functions are 3D; we can now use the Z coordinate
glTranslatef(x, y, z)
Moves objects by (x, y, z) units.
glRotatef(angle, x, y, z)
Rotates objects around the axis (x, y, z), by angle degrees.
Note that "the axis (x, y, z)" means a line that from the origin (0, 0, 0) through the point (x, y, z)
glScalef(x, y, z)
Resizes objects by the factor x in the X direction, y in the Y direction, and z in the Z direction.
This doesn't work well as things get more complex
Some solutions:
GLUT shapes | ![]() | ||
GLU quadrics | ![]() | GLUT text | ![]() |
Glut provides functions to draw several basic shapes - Platonic solids, simple curves, and teapots.
They can be drawn either with solid polygons, or in wireframe.
glutSolidSphere(1.5, 16, 8) glutWireDodecahedron()
Example code: glutGeometry.py
Sphere | glutSolidSphere(radius, slices, stacks) | ![]() |
Cone | glutSolidCone(baseRadius, height, slices, stacks) | ![]() |
Torus | glutSolidTorus(innerRadius, outerRadis, sides, rings) | ![]() |
Tetrahedron | glutSolidTetrahedron() | ![]() |
Cube | glutSolidCube(size) | ![]() |
Octahedron | glutSolidOctahedron() | ![]() |
Dodecahedron | glutSolidDodecahedron() | ![]() |
Icosahedron | glutSolidIcosahedron() | ![]() |
Teapot | glutSolidTeapot(size) | ![]() |
Quadrics are various smooth surfaces described by functions like:
x2 + y2 + z2 = r2
The basic GLU quadrics are spheres, cylinders, cones, and disks.
To draw one, create a "quadric object" and pass it to the appropriate GLU function. There are functions to control how a quadric is drawn - with points, lines, or polygons; with or without lighting; with or without texturing.
quadric = gluNewQuadric() gluQuadricDrawStyle(quadric, GLU_LINE) gluSphere(quadric, 2.5, 32, 24)
Example code: gluQuadrics.py
Sphere | gluSphere(quadric, radius, slices, stacks) | ![]() |
Cylinder | gluCylinder(quadric, baseRadius, topRadius, height, slices, stacks) | ![]() |
(a cone is a cylinder with one radius = 0) | ||
Disk | gluDisk(quadric, innerRadius, outerRadius, slices, rings) | ![]() |
Partial Disk | gluPartialDisk(quadric, innerRadius, outerRadius, slices, rings, startAngle , sweepAngle) | ![]() |
As with images, there are many file formats for storing 3D models.
Object formats can contain:
An OBJ file is a plain text file, containing vertices, polygon faces, and other information. Each vertex, face, etc, is given on a separate line.
Each line begins with a token to identify what sort of line it is - 'v' for vertex, 'f' for face, etc.
![]() | ![]() |
Hidden surfaces provide occlusion depth cue
Many different algorithms developed over the years
Rendering a polygon means filling pixels
Color buffer contains RGB color of each pixel drawn
Depth buffer contains depth of each pixel drawn
Color | Depth |
---|---|
![]() | ![]() |
When drawing a new pixel, compare new depth to what's stored in depth buffer
Color | Depth |
---|---|
![]() | ![]() |
Polygons can be drawn in any order
Polygons can intersect
Space must be allocated for the depth buffer:
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH)
Depth buffering must be enabled:
glEnable(GL_DEPTH_TEST)
Depth buffer must be cleared each frame:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Depth values have limited resolution
i.e. the numbers are not infinitely precise
Rounding errors occur when polygons are filled
Polygon overlap can cause "depth-fighting"
Perspective-related depht cues are created by a perspective projection transformation
Orthographic projection : projects rectilinear box onto display
Objects will not appear to change size with distance
Perspective projection : projects frustum (truncated pyramid) onto disp
lay
Near objects appear larger, distant objects appear smaller
GL's perspective projection is, conceptually, a pin-hole camera, located at origin, looking down -Z axis
Camera has a field-of-view - angle representing (horizontal) extent of region viewed
Small angle = narrow field-of-view = telephoto lens
Large angle = wide field-of-view = wide-angle lens
gluPerspective( fovy, aspect, zNear, zFar )
fovy = field of view, in Y direction, in degrees
aspect = aspect ratio (X:Y) of window
zNear = distance to near clipping plane
zFar = distance to far clipping plane
The "eye-point" of the perspective projection is at (0,0,0)
Example:
gluPerspective(45, 1.333, 0.1, 100)