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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

650<br />

Hopefully, the rotating castle tower didn’t bring your computer to a st<strong>and</strong>still. Calculating<br />

all that 3D data in real time is dem<strong>and</strong>ing. There are some advanced <strong>and</strong> frighteningly lowlevel<br />

approaches to optimizing 3D that I won’t go into. However, <strong>Processing</strong> includes a<br />

really simple way to speed things up, which I’ll look at in the next chapter (sorry to be a<br />

tease). For now, if the code is executing really slowly, try lowering the number of brick layers<br />

<strong>and</strong>/or increasing the speed of the rotation.<br />

Extrusion<br />

To begin to generate forms such as cylinders, cones, spheres, <strong>and</strong> toroids (also sometimes<br />

referred to as toruses), it helps to underst<strong>and</strong> the concept of lathing. However, to underst<strong>and</strong><br />

lathing, you need to underst<strong>and</strong> extrusion (lots of annoying terms, I know). Extrusion<br />

simply pushes 2D geometry into 3D space by adding a depth component to the 2D shape.<br />

For example, a rectangle on the xy plane is extruded into a cube that now exists in xyz<br />

space. Next is a simple interactive extrusion example beginning with a rectangle (shown in<br />

Figure 13-14). Move your mouse right <strong>and</strong> left to rotate the rectangle, <strong>and</strong> drag the mouse<br />

up <strong>and</strong> down to extrude it (remember, dragging requires the mouse button to be pressed).<br />

/* Extrusion Example<br />

move right to left to rotate<br />

drag up <strong>and</strong> down to extrude<br />

*/<br />

float depth = 0, boxDepth = 0;<br />

float mousePt_Y = 0;<br />

void setup(){<br />

size(500, 300, P3D);<br />

}<br />

void draw(){<br />

background(25);<br />

lights();<br />

fill(100, 100, 175);<br />

stroke(200);<br />

translate(width/2, height/2);<br />

rotateY(radians(mouseX));<br />

box(100, 50, boxDepth);<br />

}<br />

void mouseDragged(){<br />

boxDepth = depth + (mousePt_Y-mouseY);<br />

}<br />

void mousePressed(){<br />

mousePt_Y = mouseY;<br />

}<br />

void mouseReleased(){<br />

depth = boxDepth;<br />

}

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

Saved successfully!

Ooh no, something went wrong!