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.

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:

*
x ^{2} +
y^{2} +
z^{2}
= r^{2}
*

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.

- Wavefront (.obj)
- Inventor (.iv)
- VRML / X3D
- 3D Studio (.3ds)
- OpenFlight (.flt)
- ...

Object formats can contain:

- Geometry data - vertex positions, faces
- Colors / material properties
- Textures
- Transformations

cow.obj

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

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