First steps in 3D Processing

Lesson 12 of 14 · Processing

So far in Processing we have drawn two-dimensional objects such as lines, rectangles, circles and polygons. In this lesson we take the first step towards three-dimensional graphics. We will learn how to turn on the P3D renderer, how to draw the first 3D objects and how to move and rotate them.

What will we learn in this lesson?
  • turning on the 3D mode (P3D)
  • drawing basic 3D shapes
  • using the translate() function
  • rotating objects
  • the basic differences between 2D and 3D graphics

The illustrations in this lesson have labels in Serbian; the text next to each picture explains it in English.

What is P3D?

By default, Processing uses a two-dimensional renderer. In that mode we can draw only objects in the plane of the screen. If we want to use three-dimensional graphics, we need to turn on the P3D renderer.

size(800, 600, P3D);

The third parameter of the size() function tells Processing that it will use a three-dimensional view. From that moment we can use functions such as:

  • box()
  • sphere()
  • rotateX()
  • rotateY()
  • rotateZ()
Important!

If you forget to write P3D, the functions for drawing three-dimensional objects will not work as you expect.
The P3D renderer

The P3D renderer

Processing uses a special renderer that makes it possible to draw three-dimensional objects.

The difference between 2D and 3D graphics

2D and 3D graphics

In 3D space objects have width, height and depth.

A Processing P3D window

The 3D workspace

P3D makes it possible to show objects in three-dimensional space.

The first 3D program

We will start with the simplest possible example. The program draws one cube in the centre of the window.

void setup() {
    size(800, 600, P3D);
}

void draw() {
    background(220);

    translate(width / 2, height / 2);

    box(150);
}

The box() function draws a cube whose side is equal to the given value. Since the origin of the coordinate system is in the top left corner of the window, we use the translate() function to move the coordinate system to the centre of the screen.

Experiment
  • Increase the size of the cube to 250.
  • Reduce the size to 50.
  • Move the cube to the left by changing the translate() function.

The coordinate system in 3D

In two-dimensional graphics we used two axes:

  • X — the horizontal axis
  • Y — the vertical axis

In three-dimensional graphics one more axis is added:

  • Z — the depth of space.

At the beginning you do not need to remember in which direction the Z axis grows. It is much more important to understand that every object now has a position given by three coordinates:

(x, y, z)
Note

In the next examples we will use the function translate(x,y,z) to move objects along all three axes.
The coordinate system in 3D

The coordinate system

The position of every object is given by the coordinates (x, y, z).

translate in 3D

translate()

We can move an object along all the axes of space.

A 3D cube

box()

The simplest three-dimensional object in Processing.

Moving an object in space

The translate() function can take three parameters.

translate(150, 100, -100);

box(120);

The first parameter sets the movement along the X axis. The second parameter sets the movement along the Y axis. The third parameter sets the movement along the Z axis. This way we can place an object wherever we want in space.

Try it yourself
  • Change the value of the X coordinate.
  • Change the value of the Y coordinate.
  • Change the Z coordinate and watch what changes.
In the next part

We will learn how to rotate objects with the functions:
  • rotateX()
  • rotateY()
  • rotateZ()
Then we will add lighting and draw several 3D objects in the same scene.

Rotating 3D objects

One of the biggest advantages of three-dimensional graphics is the ability to rotate objects around different axes. In 2D graphics we could rotate an object only in the plane of the screen, while in 3D space there are three independent axes of rotation.

  • rotateX() – rotation around the X axis
  • rotateY() – rotation around the Y axis
  • rotateZ() – rotation around the Z axis
Note

All the rotation functions take an angle in radians. As in earlier lessons, we can use the constant PI.

Example 1 – Rotating a cube around the Y axis

// The rotation angle of the cube
float angle = 0;

void setup() {
    // Creating a window of 800x600 pixels.
    // The third parameter, P3D, turns on the three-dimensional renderer.
    size(800, 600, P3D);
}

void draw() {
    // Background colour (light grey)
    background(220);

    // Moving the coordinate system to the centre of the window.
    // If we did not do this, the cube would be drawn in the top left corner.
    translate(width / 2, height / 2);

    // Rotating the cube around the Y axis.
    // The angle is in radians.
    rotateY(angle);

    // Drawing a cube whose side is 150 pixels.
    box(150);

    // Increasing the rotation angle.
    // Since the draw() function runs many times a second,
    // the cube will look as if it is turning all the time.
    angle += 0.02;
}

In this example the cube turns around the Y axis. The variable angle stores the current rotation angle, and in each pass through the draw() function it increases by a small value. Since the screen is refreshed dozens of times a second, the viewer gets the impression that the cube is turning continuously.

In each pass through the draw() function the angle increases a little, so the cube keeps turning around the Y axis.

Experiment
  • Change the speed of rotation.
  • Use rotateX() instead of rotateY().
  • Try the rotateZ() function.
Rotating a cube around the Y axis

Rotation around the Y axis

The object turns around the vertical axis.

Rotation around the X axis

rotateX()

Rotation around the horizontal axis.

Rotation around the Z axis

rotateZ()

Rotation in the plane of the screen.

Drawing a sphere

Besides a cube, Processing can draw a sphere with the sphere() function.

void setup() {
    size(800, 600, P3D);
}

void draw() {
    background(220);

    translate(width / 2, height / 2);

    sphere(90);
}

The argument of the function is the radius of the sphere.

Important!

If you do not turn on lighting, the sphere may look completely flat. That is why the lights() function is used very often in 3D graphics.

Adding lighting

Unlike in 2D graphics, in three-dimensional scenes it is very important to decide how objects are lit. The simplest way is to use the function:

lights();

It turns on the default lighting and makes the edges and curved surfaces of objects much easier to see.

void draw() {
    background(220);

    lights();

    translate(width / 2, height / 2);

    rotateY(frameCount * 0.02);

    sphere(90);
}
Try it yourself
  • Delete the lights() function.
  • Look at how the sphere looks.
  • Then turn the lighting on again.
A 3D sphere

The sphere() function

A sphere is one of the basic 3D objects in Processing.

A sphere without lighting

Without lighting

The object looks flatter and is harder to see.

A sphere with lighting

With lighting

Light emphasises the shape and depth of the object.

What have we learnt?
  • how 3D objects are rotated,
  • the difference between the functions rotateX(), rotateY() and rotateZ(),
  • how a sphere is drawn,
  • why the lights() function is important in 3D graphics.

In the last part of the lesson we will learn how to draw several objects in the same scene using the functions pushMatrix() and popMatrix(). These functions are the basis for making more complex 3D scenes and will be very important in the next lessons, when we make the solar system and other three-dimensional simulations.

Several 3D objects in the same scene

So far we have drawn one object in the centre of the screen. In real 3D scenes we often have several objects that are in different positions and have different rotations.

To achieve this, we have to manage the transformations correctly.

The problem: why does everything rotate together?

Let us look at the following example:

translate(width / 2, height / 2);

rotateY(angle);

box(100);

translate(200, 0, 0);

box(50);

At first glance we expect the second object to be separate, but it still behaves as part of the same transformation.

The problem:

All transformations "stick" to one another. That is why we need to protect the transformations.

The solution: pushMatrix() and popMatrix()

The functions pushMatrix() and popMatrix() let us save and restore the state of the coordinate system.

  • pushMatrix() – saves the current state
  • popMatrix() – restores the previous state

An example of drawing several objects correctly

void draw() {
    // Background colour of the window.
    background(220);

    // Moving the coordinate system to the centre of the window.
    // This way the objects will be drawn around the centre of the screen.
    translate(width / 2, height / 2);

    // Rotating the coordinate system around the Y axis.
    // All objects drawn after this statement will be rotated.
    rotateY(angle);

    // ==================================================
    // The first object - a cube
    // ==================================================

    // Save the current state of the coordinate system.
    pushMatrix();

    // Drawing a cube with a side of 120 pixels.
    // Since we have not changed the position of the coordinate system,
    // the cube will be drawn in the centre of the scene.
    box(120);

    // Restore the previous state of the coordinate system.
    popMatrix();

    // ==================================================
    // The second object - a sphere
    // ==================================================

    // Save the state of the coordinate system again.
    pushMatrix();

    // Move the coordinate system 200 pixels along the X axis.
    // This sets the position of the sphere relative to the cube.
    translate(200, 0, 0);

    // Rotating the sphere around the X axis.
    rotateX(angle);

    // Drawing a sphere with a radius of 60 pixels.
    sphere(60);

    // Restore the coordinate system to its state before the move.
    // The next objects will not be affected by these transformations.
    popMatrix();

    // Increasing the rotation angle.
    // In each pass through draw() the objects will be
    // in a new position.
    angle += 0.02;
}

Each object now has its own "local space" and does not affect the others.

Mini project – a 3D scene with several objects

In the next example we create a simple 3D scene that consists of:

  • a rotating cube in the centre
  • a sphere that circles around it
  • small helper axes of space
// The angle that sets the rotation of the objects
float angle = 0;

void setup() {
    // Creating a window of 800x600 pixels.
    // The P3D parameter turns on the three-dimensional renderer.
    size(800, 600, P3D);
}

void draw() {
    // A dark background makes 3D objects stand out better.
    background(30);

    // Turning on the default lighting of the scene.
    // Without lighting, objects can look flat.
    lights();

    // Moving the coordinate system to the centre of the window.
    translate(width / 2, height / 2);

    // ==================================================
    // Drawing the coordinate axes
    // ==================================================

    // X axis (red)
    stroke(255, 0, 0);
    line(0, 0, 0, 200, 0, 0);

    // Y axis (green)
    stroke(0, 255, 0);
    line(0, 0, 0, 0, 200, 0);

    // Z axis (blue)
    stroke(0, 0, 255);
    line(0, 0, 0, 0, 0, 200);

    // Turn off drawing the edges of objects.
    noStroke();

    // ==================================================
    // The central rotating cube
    // ==================================================

    // Save the current state of the coordinate system.
    pushMatrix();

    // Rotating the cube around the Y axis.
    rotateY(angle);

    // The colour of the cube.
    fill(200, 100, 255);

    // Drawing the cube.
    box(120);

    // Restore the previous state of the coordinate system.
    // All transformations done between pushMatrix()
    // and popMatrix() apply only to this cube.
    popMatrix();

    // ==================================================
    // A sphere circling around the cube
    // ==================================================

    // Save the state of the coordinate system again.
    pushMatrix();

    // Rotate the coordinate system around the Y axis.
    rotateY(angle);

    // Move the coordinate system 200 pixels along the X axis.
    // Since the coordinate system has already been rotated,
    // the sphere will circle around the central cube.
    translate(200, 0, 0);

    // The colour of the sphere.
    fill(255, 200, 0);

    // Drawing a sphere with a radius of 40 pixels.
    sphere(40);

    // Restore the coordinate system to its previous state.
    popMatrix();

    // Increasing the rotation angle.
    // This way, in the next pass through draw()
    // the objects are in a new position.
    angle += 0.02;
}
A 3D scene with several objects

Several objects

Each object has its own transformation and position in space.

The 3D axes of space

The axes of space

The X, Y and Z axes help you find your way in 3D space.

An orbiting object

Orbital motion

Objects can move around other objects.

Mini quiz

Questions:
  • 1. What is P3D used for in Processing?
  • 2. Which function lets an object move in space?
  • 3. Which function stops transformations from "mixing"?
  • 4. Which axis represents the depth of space?

Tasks for independent work

  • Add one more object that circles around the centre.
  • Change the colours of all the objects in the scene.
  • Increase the radius of the sphere's orbit.
  • Add a third rotating cube.

What next?

In the next lesson we move on to a very important concept:

  • the camera in 3D space (camera())
  • perspective (perspective())
  • controlling the view of the scene

Then we will apply everything we have learnt and make: the solar system in 3D

Final note

If you understand how transformations work in this lesson, you have already mastered 80% of the basics of 3D graphics in Processing.

Check your knowledge

Open the quiz

Quiz: First steps in 3D

1. How do you turn on the 3D mode?




2. What does box(100) draw?




3. In which direction does the z axis go in Processing?




4. What is lights() used for?