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

166<br />

OPENGL mode<br />

P3D mode utilizes a software-based 3D engine, meaning that all the 3D calculations are<br />

h<strong>and</strong>led by Java, just as normal 2D calculations are; the 3D math is crunched <strong>and</strong> converted<br />

to 2D data before being sent to the graphics hardware to draw the image to the<br />

screen. The hardware isn’t even aware the data began life as 3D geometry. However, your<br />

graphics hardware has the capability to crunch numbers, <strong>and</strong> in fact can do it much faster<br />

than Java. The trick, though, is in communicating directly with the graphics hardware to<br />

speed things up.<br />

OpenGL is a platform-independent library that functions as an interface between your<br />

code <strong>and</strong> the graphics hardware. OpenGL was developed in 1992 by Silicon Graphics, but<br />

is now overseen by a large group of organizations, under the heading OpenGL<br />

Architecture Review Board. Designed as a set of specifications, OpenGL is platform agnostic<br />

<strong>and</strong> can be utilized by practically all systems that have OpenGL-accelerated hardware.<br />

<strong>Processing</strong> <strong>and</strong> Java interface with OpenGL through JOGL. JOGL, developed by the Game<br />

Technology Group at Sun, is technically considered a Java binding (as in “the ties that<br />

bind”) to OpenGL, <strong>and</strong> gives <strong>Processing</strong> sketches that use the OPENGL rendering mode the<br />

ability to render more stuff faster <strong>and</strong> at larger sizes. However, to benefit from this mode,<br />

you need an OpenGL-accelerated graphics card. Fortunately, most modern machines have<br />

one. To learn more about OpenGL, see www.opengl.org/; <strong>and</strong> to learn more about JOGL,<br />

see https://jogl.dev.java.net/.<br />

Utilizing the OpenGL library couldn’t be simpler in <strong>Processing</strong>. Simply select Sketch ➤<br />

Import Library ➤ opengl, which adds an import line to your sketch, <strong>and</strong> then add the<br />

OPENGL string as a third argument to the size function call: size (800, 600, OPENGL);. The<br />

following 3D example (shown in Figure 5-14) can be run in either P3D or OPENGL rendering<br />

mode; simply change the third size argument from OPENGL to P3D. It is worth trying<br />

both 3D modes to get a sense of how hardware acceleration dramatically impacts performance.<br />

One word of caution to beginners though: 3D programming is difficult—even<br />

<strong>Processing</strong>’s greatly simplified version. If you’re feeling overwhelmed from information<br />

overload, don’t worry about the 3D renderers. I won’t cover them until the last two chapters<br />

in the book, at which time you’ll be ready. I do recommend running the example to<br />

see what it does. It’s a little complicated, though, so unless you have prior coding experience,<br />

I wouldn’t spend too much time dwelling on it. 3D can be tough, but also pretty<br />

cool.<br />

/*<br />

Space Junk<br />

Ira Greenberg, November 4, 2005<br />

*/<br />

/*need to import opengl library to use OPENGL<br />

rendering mode for hardware acceleration*/<br />

import processing.opengl.*;<br />

//used for oveall rotation<br />

float ang;

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

Saved successfully!

Ooh no, something went wrong!