3D


Assassin's Creed, by UbiSoft





3D Coordinates

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






3D Viewing

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


glOrtho(-5, 8, -4, 4, -1, 3)






3D Transformations

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.






Complex Geometry

Simple objects can be created by just writing code - "hard coding" the vertex positions, colors, etc, in your program

This doesn't work well as things get more complex

Some solutions:






Geometry Functions

GLUT shapes
GLU quadrics GLUT text





GLUT Shapes

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






GLUT Shapes

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)





GLU Quadrics

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






GLU Quadrics

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)





Model Files

As with images, there are many file formats for storing 3D models.


Object formats can contain:






Wavefront OBJ Files


cow.obj





OBJ Format

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.

v x y z
vertex position
vn x y z
vertex normal
vt u v
texture coordinate
f v1 v2 v3 ...
face (list of vertex numbers)
mtllib file.mtl
file containing material descriptions
usemtl name
current material to apply to geometry






Hidden Surfaces

Hidden surfaces provide occlusion depth cue






Hidden Surface Algorithms

Many different algorithms developed over the years






Ray Tracing


CC-BY-SA diagram by Henrik





Depth Buffering

Rendering a polygon means filling pixels

Color buffer contains RGB color of each pixel drawn

Depth buffer contains depth of each pixel drawn

ColorDepth





Depth Buffer






Depth Buffer

When drawing a new pixel, compare new depth to what's stored in depth buffer

ColorDepth

Polygons can be drawn in any order
Polygons can intersect






OpenGL Depth Buffering

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 Fighting

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 Projection


Splinter Cell (Ubisoft)

Perspective-related depht cues are created by a perspective projection transformation

Perspective + movement yields motion parallax






Orthographic Projection

Orthographic projection : projects rectilinear box onto display
Objects will not appear to change size with distance






Perspective Projection

Perspective projection : projects frustum (truncated pyramid) onto disp lay
Near objects appear larger, distant objects appear smaller






Perspective Projection

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






OpenGL Perspective

    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)


Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.