232 Learning Processing
e display( ) function above draws all of Zoog’s parts (body and head, etc.) relative to Zoog’s x , y location.
It requires that x and y be used in both rect( ) and ellipse( ). translate( ) allows us to simply set Processing’s
origin (0,0) at Zoog’s ( x , y ) location and therefore draw the shapes relative to (0,0).
void display() {
// Move origin (0,0) to (x,y)
translate(x,y);
// Draw Zoog's body
fill(150);
rect( 0,0 ,w/6,h*2);
// Draw Zoog's head
fill(255);
ellipse(0,-h/2,w,h);
}
14.2 P3D vs. OPENGL
If you look closely at Example 14-3, you will notice that we have added a third argument to the size( )
function. Traditionally, size( ) has served one purpose: to specify the width and height of our Processing
window. e size( ) function, however, also accepts a third parameter indicating a drawing mode.
e mode tells Processing what to do behind the scenes when rendering the display window. e default
mode (when none is specifi ed) is “ JAVA2D, ” which employs existing Java 2D libraries to draw shapes, set
colors, and so on. We do not have to worry about how this works. e creators of Processing took care of
the details.
If we want to employ 3D translation (or rotation as we will see later in this chapter), the JAVA2D mode
will no longer suffi ce. Running the example in the default mode results in the following error:
“ translate(x, y, z) can only be used with OPENGL or P3D, use translate(x, y) instead. ”
Instead of switching to translate(x,y) , we want to select a diff erent mode. ere are two options:
• P3D —P3D is a 3D renderer developed by the creators of Processing . It should also be noted that
anti-aliasing (enabled with the smooth( ) function) is not available with P3D.
• OPENGL —OPENGL is a 3D renderer that employs hardware acceleration. If you have an
OpenGL compatible graphics card installed on your computer (which is pretty much every
computer), you can use this mode. Although at the time of the writing of this book, there are still
a few, minor kinks to be worked out with this mode (you may find things look slightly different
between P3D and OPENGL), it may prove exceptionally useful in terms of speed. If you are
planning to display large numbers of shapes onscreen in a high-resolution window, this mode will
likely have the best performance.
To specify a mode, add a third argument in all caps to the size( ) function.
size(200,200); // using the default JAVA2D mode
size(200,200,P3D); // using P3D
size(200,200,OPENGL); // using OPENGL
translate() can be used to draw a collection
of shapes relative to a given point.