06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

232 <strong>Learning</strong> <strong>Processing</strong><br />

Th e display( ) function above draws all of Zoog’s parts (body and head, etc.) relative <strong>to</strong> Zoog’s x , y location.<br />

It requires that x and y be used in both rect( ) and ellipse( ). translate( ) allows us <strong>to</strong> simply set <strong>Processing</strong>’s<br />

origin (0,0) at Zoog’s ( x , y ) location and therefore draw the shapes relative <strong>to</strong> (0,0).<br />

14.2<br />

void display() {<br />

// Move origin (0,0) <strong>to</strong> (x,y)<br />

translate(x,y);<br />

}<br />

// Draw Zoog's body<br />

fill(150);<br />

rect( 0,0 ,w/6,h*2);<br />

// Draw Zoog's head<br />

fill(255);<br />

ellipse(0,-h/2,w,h);<br />

P3D vs. OPENGL<br />

If you look closely at Example 14-3, you will notice that we have added a third argument <strong>to</strong> the size( )<br />

function. Traditionally, size( ) has served one purpose: <strong>to</strong> specify the width and height of our <strong>Processing</strong><br />

window. Th e size( ) function, however, also accepts a third parameter indicating a drawing mode.<br />

Th e mode tells <strong>Processing</strong> what <strong>to</strong> do behind the scenes when rendering the display window. Th e default<br />

mode (when none is specifi ed) is “ JAVA2D, ” which employs existing Java 2D libraries <strong>to</strong> draw shapes, set<br />

colors, and so on. We do not have <strong>to</strong> worry about how this works. Th e crea<strong>to</strong>rs of <strong>Processing</strong> <strong>to</strong>ok care of<br />

the details.<br />

If we want <strong>to</strong> employ 3D translation (or rotation as we will see later in this chapter), the JAVA2D mode<br />

will no longer suffi ce. Running the example in the default mode results in the following error:<br />

“ translate(x, y, z) can only be used with OPENGL or P3D, use translate(x, y) instead. ”<br />

Instead of switching <strong>to</strong> translate(x,y) , we want <strong>to</strong> select a diff erent mode. Th ere are two options:<br />

• P3D —P3D is a 3D renderer developed by the crea<strong>to</strong>rs of <strong>Processing</strong> . It should also be noted that<br />

anti-aliasing (enabled with the smooth( ) function) is not available with P3D.<br />

• OPENGL —OPENGL is a 3D renderer that employs hardware acceleration. If you have an<br />

OpenGL compatible graphics card installed on your computer (which is pretty much every<br />

computer), you can use this mode. Although at the time of the writing of this book, there are still<br />

a few, minor kinks <strong>to</strong> be worked out with this mode (you may find things look slightly different<br />

between P3D and OPENGL), it may prove exceptionally useful in terms of speed. If you are<br />

planning <strong>to</strong> display large numbers of shapes onscreen in a high-resolution window, this mode will<br />

likely have the best performance.<br />

To specify a mode, add a third argument in all caps <strong>to</strong> the size( ) function.<br />

size(200,200); // using the default JAVA2D mode<br />

size(200,200,P3D); // using P3D<br />

size(200,200,OPENGL); // using OPENGL<br />

translate() can be used <strong>to</strong> draw a collection<br />

of shapes relative <strong>to</strong> a given point.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!