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 />

164<br />

P3D mode<br />

P3D mode gives you access to a custom 3D engine in <strong>Processing</strong>. A 3D engine is just software<br />

(no oil or gears) that allows you to deal with a z-axis, in addition to the st<strong>and</strong>ard<br />

x- <strong>and</strong> y-axes. In 3D, geometry is calculated in three dimensions, which is just a simple<br />

math problem for a computer. However, the darn computer screen is only 2D, so the<br />

engine has to translate the 3D data back to a 2D screen projection. In the real world, we<br />

perceive images decreasing in size as they move farther away from us. We can simulate this<br />

virtually (both with traditional materials <strong>and</strong> through software) by using a system of<br />

perspective. I’m sure some of you remember doing simple two <strong>and</strong> three-point perspective<br />

exercises in an intro art class. Perspective works similarly in P3D mode, but without the<br />

need to grab a ruler or get your h<strong>and</strong>s dirty. 3D-to-2D projections on the computer are<br />

generally tied to the concept of a virtual camera, which is capable of very wide-angle views<br />

(in which things are large, close, <strong>and</strong> often distorted—think fish-eye lens) as well as very<br />

distant views.<br />

Besides virtual camera capabilities, P3D has virtual lights <strong>and</strong> simple texture mapping<br />

(image mapping), allowing an image to be attached to the points (vertices) of 3D geometry.<br />

This capability, called u-v mapping, allows image maps on the geometry to move with<br />

the form. P3D mode is built for speed, a little at the expense of rendering quality.<br />

However, P3D mode is a great <strong>and</strong> relatively easy way to begin experimenting with coding<br />

3D with a minimal amount of code <strong>and</strong> complexity. The following short example rotates<br />

three concentric polyhedra (polyhedra are basically 3D polygons) <strong>and</strong> also responds to<br />

both the x <strong>and</strong> y mouse positions. Figure 5-13 shows the output of the sketch.<br />

/*<br />

Rotating Polyhedra<br />

Ira Greenberg, November 3, 2005<br />

*/<br />

//declare variables<br />

float rotY;<br />

float rotX;<br />

void setup(){<br />

size (400, 400, P3D);<br />

frameRate(30);<br />

}<br />

void draw(){<br />

background(255);<br />

/* ensures shapes are drawn at the<br />

center of the display window. In addition,<br />

mouse y postion controls the distance<br />

the shapes are drawn along the z-axis.<br />

As the shapes moves further into space,<br />

they will diminish in size*/<br />

translate(width/2, height/2, mouseY-100);

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

Saved successfully!

Ooh no, something went wrong!