If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

What are 3D shapes?

This tutorial is from Peter Collingridge and was originally posted on his website.
If you've been programming here on Khan Academy, you've probably drawn a whole lot of 2D shapes, like with rectangles and ellipses, and maybe you've wondered to yourself how to make 3D shapes, like cubes and spheres.
Well, the ProcessingJS library that we use here isn't really designed for 3D graphics, but as it turns out, with some trigonometry, we can write our own 3D graphics engine  and, in doing so, learn a bit how 3D graphics work (and why there is a reason to learn trigonometry besides making math teachers happy!).
Here's an example of the sort of program you can make - use your mouse to rotate the donut around:

What are 3D graphics?

Since computer screens are essentially two-dimensional, 3D graphics are just 2D optical illusions that trick your brain into thinking it is seeing a 3D object. Here's a simple example:
3D cube
Three parallelograms pretending to be a cube
A 3D graphics engine works by calculating what 2D shapes a 3D object would project on to the screen. So to write our own 3D engine, we need to know how to do these calculations. Our program won't be as quick as most 3D engines but it should help us understand the principles of how they work.

Representing shapes

A 3D graphics engine takes a 3D object and converts into 2D graphics, but how do we represent a 3D object in code?
A single point in 3D space is easy to represent using an array of three numbers. For example, we can use [30, 80, 55] to represent a point 30 pixels along the horizontal (x) axis, 80 pixels along the vertical (y) axis, and 55 pixels along the axis that goes into and out of the screen. Play around with the point below, rotating with the mouse and tinkering with the numbers:
Representing a line is also easy: you just connect two points. One way to represent an object in 3D, therefore, is by converting it into a group of lines. This is called a wireframe, as it looks like the object is made from wire. It's obviously not ideal for representing solid objects, but it's a good place to start.

Terms

Below are some terms that we will use when referring to 3D shapes. Other terms might be used elsewhere, but these ones are pretty popular.
  • Node: a point represented by three coordinates, x, y and z (can also be called a vertex).
  • Edge: a line connecting two points.
  • Face: a surface defined by at least three points.
  • Wireframe: a shape consisting of just nodes and edges.

Want to join the conversation?