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.

Figure 13-14. Extrusion Example sketch<br />

In reality, my example is a pretty cheap version of extrusion, since I used <strong>Processing</strong>’s<br />

box() comm<strong>and</strong>, which created the rectangle in 3D space, allowing me to just interactively<br />

change the value of the depth argument in the box() call. If I had started with a “real” rectangle,<br />

I would have had to deal with adding <strong>and</strong> attaching new geometry to have enough<br />

vertices (at least eight) to make a box. Notice in the example that when you drag the<br />

mouse up, the rectangle becomes a solid box, but when you drag it down, the box exterior<br />

seems to become strangely transparent. This occurrence isn’t a quantum paradox, but one<br />

of the clever ways 3D is optimized for our still slow computers. Since we normally aren’t<br />

interested in the inside of a form (the part we can’t see), 3D engines tend not to waste<br />

processing power rendering that information. Thus, in 3D, polygons are usually singlesided.<br />

So, when the mouse is dragged down <strong>and</strong> the box collapses in on itself, the inside<br />

surfaces become the outside surfaces; however, since the inside faces are invisible, you can<br />

see through them to the original outside surfaces, which are now inside the box—oy vey!<br />

Lest you think I got off too easy borrowing <strong>Processing</strong>’s box() function in the last extrusion<br />

example, I’ll follow it up with a better one. Using the keyboard, you can control the length<br />

of the extrusion <strong>and</strong> the number of segments. You can also set the rendering as wireframe,<br />

hollow shaded, or filled solid. The left <strong>and</strong> right arrows control extrusion length, the up<br />

<strong>and</strong> down arrows control the addition/deletion of segments, the S key toggles between<br />

solid shaded <strong>and</strong> hollow shaded, <strong>and</strong> the W key toggles between wireframe <strong>and</strong> shaded.<br />

All the surfaces are double-sided, allowing you to see both the inside <strong>and</strong> outside of the<br />

box when in hollow shaded mode. I included three screenshots generated by the sketch,<br />

shown in Figures 13-15 through 13-17.<br />

/* Better Extrusion Example<br />

Arrow keys control segments (up/down)<br />

<strong>and</strong> extrusion length (left/right)<br />

's' key controls solid rendering<br />

'w' key controls wireframe rendering<br />

*/<br />

// begin as a flat cube<br />

int segments = 1;<br />

int pts = 8;<br />

float rectWidth = 150, rectHeight = 100;<br />

float extrudeLength;<br />

3D<br />

651<br />

13

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

Saved successfully!

Ooh no, something went wrong!