Understanding 3D space
In Chapter 1, Getting Started with Processing 2, you learned about the coordinate system in Processing. In this recipe, we'll take a look at the third dimension, and draw objects in a 3D space.
How to do it...
The first thing you'll need to do is to import the OpenGL library. This will enable you to use the third dimension. You've done this before in the Drawing curves recipe, in Chapter 2, Drawing Text, Curves, and Shapes in 2D. Go to Sketch | Import Library | OpenGL to import the library using the following code:
import processing.opengl.*;
The next thing we need to do is declare two variables, right before the setup()
function, and give them some values. Note that the size()
function is a little different from the examples in Chapter 2.
float depth; float zSpeed; void setup() { size( 640, 480, OPENGL ); depth = 0; zSpeed = -1; }
The first thing we'll do inside draw()
is change the value of the depth
variable, so we can use it for animation. The following piece...