3D Space in Processing
By default, Processing draws using the
JAVA2D renderer that is very precise, but slow. A sometimes faster but lower-quality version is
P2D (the Processing 2D renderer). By adding a third parameter to the
size() function you can specify which renderer to use. There are two ways to draw in 3D with Processing: Processing 3D(
P3D), or
OpenGL.
To use P3D, you specify the renderer as the third parameter in the
size() method:
void setup(){
size(200,200,P3D);
}
The
OPENGL renderer uses your graphics card to render your sketch and there is an
The OpenGl library that is designed for high-performance graphics. Using
OPENGL allows you to run your programs more quickly than P3D does when there is a lot of geometry. While the
P3D renderer is built-in, the
OpenGL renderer is a library and requires the
import statement as well as specifying the third parameter in the
size() function:
import processing.opengl.*;
void setup(){
size(200,200,OPENGL);
}
Basic methods
3D Primitives
box()
A box is an extruded rectangle. A box with equal dimension on all sides is a cube.
sphere()
A sphere is a hollow ball made from tessellated triangles.
sphereDetail()
Controls the detail used to render a sphere by adjusting the number of vertices of the sphere mesh. The default resolution is 30, which creates a fairly detailed sphere definition with vertices every 360/30 = 12 degrees. If you're going to render a great number of spheres per frame, it is advised to reduce the level of detail using this function. The setting stays active until
sphereDetail() is called again with a new parameter and so should not be called prior to every
sphere() statement, unless you wish to render spheres with different settings, e.g. using less detail for smaller spheres or ones further away from the camera. To control the detail of the horizontal and vertical resolution independently, use the version of the functions with two parameters.
3D transforms
rotateX()
rotateY()
rotateZ()
Rotates a shape around the x,y or z-axis the amount specified by the angle parameter. Angles should be specified in
radians (values from 0 to PI*2) or converted to radians with the
radians() function. Objects are always rotated around their relative position to the origin and positive numbers rotate objects in a counterclockwise direction. Transformations apply to everything that happens after and subsequent calls to the function accumulates the effect. For example, calling
rotateY(PI/2) and then
rotateY(PI/2) is the same as
rotateY(PI). If
rotateY() is called within the
draw(), the transformation is reset when the loop begins again.
scale()
Increases or decreases the size of a shape by expanding and contracting vertices. Objects always scale from their relative origin to the coordinate system. Scale values are specified as decimal percentages. For example, the function call
scale(2.0) increases the dimension of a shape by 200%. Transformations apply to everything that happens after and subsequent calls to the function multiply the effect. For example, calling
scale(2.0) and then
scale(1.5) is the same as
scale(3.0). If
scale() is called within
draw(), the transformation is reset when the loop begins again.
translate()
Specifies an amount to displace objects within the display window. The x parameter specifies left/right translation, the y parameter specifies up/down translation, and the z parameter specifies translations toward/away from the screen. Using this function with the z parameter requires using the
P3D or
OPENGL parameter in combination with size as shown in the above example. Transformations apply to everything that happens after and subsequent calls to the function accumulates the effect.
pushMatrix()
popMatrix()The
pushMatrix() function saves the current coordinate system to the stack and
popMatrix() restores the prior coordinate system.
pushMatrix() and
popMatrix() are used in conjuction with the other transformation methods and control the scope of the transformations. If you didn't use these functions, transformations would apply to all objects, which is not always what you want to do. Using them allows you to use different transformations on different objects-like in a solar system model.
Lighting
There are four types of lights in Processing:
- spot
- point
- directional
- ambient
Spot lights radiate in a cone shape. This light has a direction, location, and a color.
Point lights radiate from a single point like a lightbulb of any color.
Directional lights project in one direction to create strong lights and darks.
Ambient lights create an even light of any color over the entire scene and are almost always used with other lights.
The
lights() function creates the default lighting setup with both an ambient and directional light. You need to update lights so you need to put the call to
lights() in the
draw() function and if you are updating the background, after the call to
background().
Camera
Working in 3D introduces the idea of a camera that is pointed at the three-dimensional scene being constructed. Moving the camera changes the way Processing maps the 3D coordinates of your drawing onto the 2D screen.
Processing's default camera points at the center of the screen. Because of this, shapes away from the center are seen in perspective. The
camera() function offers control over the camera location (the location at which the camera is pointed) and the orientation (up, down, tilted). Here is how you would use the mouse to control the camera:
import processing.opengl.*;
void setup() {
size(420, 220, OPENGL);
noStroke();
}
void draw() {
lights();
background(0);
float camZ = (height/2.0) / tan(PI*60.0 / 360.0);
camera(mouseX, mouseY, camZ, // Camera location
width/2.0, height/2.0, 0, // Camera target
0, 1, 0); // Camera orientation
translate(width/2, height/2, -20);
int dim = 50;
pushMatrix();
box(dim, dim, dim);
popMatrix();
}