Homework 4

Write a program that draws two or more 3D objects.

One of the objects should spin (around an axis other than the Z axis).

Another object should move (translate) in 3 dimensions.

All of the objects can rotate and translate if desired.

The objects should remain fully visible as they move - i.e. not be clipped, including in the Z direction.




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



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