04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

618<br />

If you compare the code in the preceding two sketches carefully, you’ll notice all I had to<br />

do to get the second rotation was add a single line of code: rotateX(frameCount*PI/60);.<br />

The sketch is very simple, but it does introduce a few new concepts. First, I included an<br />

extra argument in the size(400, 400, P3D) function call. In reality, the size() function<br />

always includes a third argument that tells <strong>Processing</strong> what renderer to use. If you don’t<br />

specify a renderer, then the default renderer (JAVA2D) is used. P3D is a 3D renderer, also<br />

referred to as a 3D engine, built within in <strong>Processing</strong>. A 3D engine is just software that calculates<br />

the math of 3D coordinate space (x, y, <strong>and</strong> z), <strong>and</strong> then remaps the 3D space back<br />

to a 2D projection so that you can view it on a monitor. Since our monitors still rely on a<br />

flat, illuminated 2D surface, they can’t really plot 3D information. In a sense, what you see<br />

on your monitor is sort of a photograph of 3D that has been flattened to 2D coordinate<br />

space. 3D engines typically allow you to work with 3D geometry, light sources, textures,<br />

<strong>and</strong> even virtual cameras. Besides that, 3D works very similarly to 2D, allowing you to set<br />

properties like fills, strokes, position, <strong>and</strong> scale, <strong>and</strong> animate these properties as well.<br />

3D transformation<br />

In the last two examples, <strong>Processing</strong>’s box() function took three arguments (width, height,<br />

<strong>and</strong> depth), <strong>and</strong> placed the box, centered, at (0, 0, 0). Since you’re using three dimensions<br />

now, you use three values (x, y, <strong>and</strong> z) to identify a point in space. To move the box to the<br />

center of the display window, you can use <strong>Processing</strong>’s translate() function. Try running<br />

the last sketch again, but commenting out the translate(width/2, height/2); call. You<br />

should see the box spinning around the top-left corner of the display window, centered at<br />

(0, 0, 0).<br />

You can also translate along the z-axis. In the last sketch, change translate(width/2,<br />

height/2); to translate(width/2, height/2, -400); <strong>and</strong> run the sketch. You should<br />

now see a smaller cube spinning in the display window. You can also try using a larger positive<br />

value for the third argument; for example, translate(width/2, height/2, 130);<br />

will generate a cube that fills the entire display window.<br />

I referred to the cube getting smaller <strong>and</strong> larger when translated along the z-axis. More<br />

accurately, the cube remains the same size (with regard to its internal width/height/depth<br />

properties), but its translation along the z-axis moves it within a virtual space modeled<br />

after space in the physical world, in which objects appear to decrease <strong>and</strong> increase in size<br />

as they move away from or toward a viewer, according to the rules of perspective. 3D<br />

engines attempt to simulate this phenomenon by coding perspective projection, usually<br />

through a virtual camera that can be moved, rotated, <strong>and</strong> even zoomed. I’ll discuss virtual<br />

cameras in <strong>Processing</strong> in Chapter 14.<br />

The process of translating the contents of the display window is a little confusing <strong>and</strong> takes<br />

some time to get used to. This shouldn’t be a completely new concept, though, as it was<br />

covered earlier in the book in the discussion of 2D space, <strong>and</strong> the same principles apply.<br />

The benefit of translation becomes apparent when you try to rotate a shape drawn in the<br />

middle of the display window. The next sketch draws a 2D rectangle in the center of the<br />

screen <strong>and</strong> then rotates it. I used the rect() comm<strong>and</strong>, since it takes x- <strong>and</strong> y-coordinate<br />

<strong>and</strong> dimension arguments (as opposed to box() <strong>and</strong> sphere(), which only take dimension<br />

arguments).

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

Saved successfully!

Ooh no, something went wrong!