Skip to main content

Chapter 6 3D Shapes

We will be working with the following 3D shapes: Box, Sphere, Torus. There are many other 3D shapes that could be easily generated programmatically, these specific three allow us to work with and test many advanced features, especially when it comes to lighting & shading. If you want to add more shapes, keep that idea as a feature to be added to your final project. For each shape, we want to start by considering 2 important questions:
  1. What given information should we require?
  2. What are the points that our engine will need to generate in order to draw the shape?
The goal of our engine will be to create the necessary points from the given information (this is how we created circles & splines as well).

Section 6.1 Box

To make a box, we need the folloing:
  • A vertex: for consistency, we will be using the left-top-front vertex.
  • Width: Size in the x dimension.
  • Height: Size in the y dimension.
  • Depth: Size in the z dimension.
The defining points of a box are its 8 vertices, which can be straightfporwardly calculated from the given information above. Once you have the 8 vertices, you can directly add the 12 edges that make up the box. You don’t need to do anything fancy here.

Section 6.2 Sphere

To make a sphere, we need the following:
  • Center point.
  • Radius.
The defining points are any points along the surface of the sphere. in theory, there are an infinite number of points on the surface, but we can use a subset of all those to help draw something that looks like a sphere.

Subsection 6.2.1 Sphere Point Generation

The points on the surface of a sphere can be generated by taking a circle and rotating it. Each circle rotation creates a new "slice" of the sphere. The circle can be rotated about the x or y axes, but not z. The only difference will be the location of the "poles" of the sphere. An x-rotated sphere will have its poles on the left and right ends, while a y-rotated sphere will have its poles on the top and bottom.
\begin{equation*} \begin{array}{c} x rotation \\ \left[ \begin{matrix} 1 \amp 0 \amp 0 \\\ 0 \amp cos(\phi) \amp -sin(\phi) \\\ 0 \amp sin(\phi) \amp cos(phi) \end{matrix} \right] \end{array} \begin{array}{c} circle \\ \left[ \begin{matrix} rcos(\theta) \\\ rsin(\theta) \\\ 0 \end{matrix} \right] \end{array} = \begin{array}{c} sphere \\ \left[ \begin{matrix} rcos(\theta) \\\ rsin(\theta)cos(\phi) \\\ rsin(\theta)sin(\phi) \end{matrix} \right] \end{array} \end{equation*}
The matrix multiplication above leaves us with equations that we can use to find the points of a sphere:
\begin{equation*} \begin{aligned} x \amp= rcos(\theta) + C_x \\ y \amp= rsin(\theta)cos(\phi) + C_y \\ z \amp= rsin(\theta)sin(\phi) + C_z \end{aligned} \end{equation*}
Here \(\phi\) is used as the angle of rotation and \(\theta\) is used as the angle of circle creation. If you only use one angle, then you would generate one point per circle each rotation. The resulting shape would be a sprial going along the sphere (pleasing, but not our goal). You can generate a sphere either by creating a full circle and rotating it π radians, or by creating a semi-circle and rotating it 2π radians. For reasons that will become clear later, it is to our advantage to use the semi-circle method. With these equations we can create a nested-loop parametric function to generate all the points on a circle:
        for rot: 0 -> 1
          for cir: 0 -> 1
            x = r * cos(π * cir) + Cx
            y = r * sin(π * cir) * cos(2π * rot) + Cy
            z = r * sin(π * cir) * sin(2π * rot) + Cz
        

Section 6.3 Torus

A torus is a doughnut (or bagel, if that’s your thing)-like shape, that is simlar in some repsects to a sphere. To generate a torus, we will need:
  • Center point of the entire torus.
  • Radius of circular cross-section \(r\)
  • Distance from the torus center to center of the cross-section \(R\)
Like a sphere, the defining points are any points along the surface of the torus.

Subsection 6.3.1 Torus Point Generation

A torus, can be generated in a similar way to a sphere, except you move the circle away from the torus center before rotating. You have to be careful about how you move the circle and then rotate it. Only specific combinations will work:
  • Move a circle horizontally (x translation) and y rotation.
  • Move a circle vertically (y translation) and x rotation.
\begin{equation*} \begin{array}{c} y rotation \\ \left[ \begin{matrix} cos(\phi) \amp 0 \amp sin(\phi) \\\ 0 \amp 1 \amp 0 \\\ -sin(\phi) \amp 0 \amp cos(\phi) \end{matrix} \right] \end{array} \begin{array}{c} circle \\ \left[ \begin{matrix} rcos(\theta) + R \\\ rsin(\theta) \\\ 0 \end{matrix} \right] \end{array} = \begin{array}{c} sphere \\ \left[ \begin{matrix} cos(\phi)(rcos(\theta) + R) \\\ rsin(\theta) \\\ -sin(\phi)(rcos(\theta) + R) \end{matrix} \right] \end{array} \end{equation*}
This leaves us with the following Torus equations:
\begin{equation*} \begin{aligned} x \amp= cos(p) * (rcos(t) + R) + C_x \\ y \amp= rsin(t) + C_y \\ z \amp= -sin(p) * (rcos(t) + R) + C_z \end{aligned} \end{equation*}
Here \(\phi\) is used as the angle of rotation and \(\theta\) is used as the angle of circle creation. Unlike a sphere, both \(\phi\) and \(\theta\) must go from 0 to 2π in order to generate a full Torus. Limiting one of the angles to π would result in odd variations of half-tori.

Section 6.4 Implementing 3D Shapes

The algorithms desribed above generate the points for our 3D shapes, but they don’t cover how we should use those points in order to
Currently, the basic drawing unit for our graphics engine is a line. This works quite well for representing