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

696<br />

3D Primitives<br />

The 3D Primitives section only includes three functions:<br />

box()<br />

sphere()<br />

sphereDetail()<br />

The box() <strong>and</strong> sphere() functions generate simple 3D objects. These are not simulated 3D<br />

forms, but are actually based on 3D geometry. Thus, to use these functions, you need to<br />

specify either the P3D or OPENGL rendering mode in the size() function call. If you use<br />

OPENGL, you also need to import the OPENGL library (via the Sketch ➤ Import Library ➤<br />

opengl menu option).<br />

The SphereDetail() function controls the level of detail, or number of vertices, used to<br />

generate the sphere, which is constructed out of triangles that all fit together in a mosaic<br />

pattern (called tessellated triangles). Here’s a little example of some spinning stuff, using<br />

all three of the 3D primitive functions (see Figure A-7). The outer ring of spheres in the<br />

example illustrates the use of the sphereDetail() function. Each of the objects in the ring<br />

is technically a sphere, but by setting the sphereDetail value very low, you get a more<br />

rigid, less sphere-like form.<br />

/* Spinning 3D Stuff<br />

Ira Greenberg, October 9, 2006 */<br />

float px, py, angle;<br />

void setup(){<br />

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

noFill();<br />

stroke(255);<br />

}<br />

void draw(){<br />

background(0);<br />

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

pushMatrix();<br />

rotateY(frameCount*PI/150);<br />

rotateX(frameCount*PI/150);<br />

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

pushMatrix();<br />

rotateY(-frameCount*PI/10);<br />

rotateX(PI/4);<br />

box(45, 15, 65);<br />

popMatrix();<br />

popMatrix();

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

Saved successfully!

Ooh no, something went wrong!