Book Image

Processing 2: Creative Programming Cookbook

Book Image

Processing 2: Creative Programming Cookbook

Overview of this book

Processing is probably the best known creative coding environment that helps you bridge the gap between programming and art. It enables designers, artists, architects, students and many others to explore graphics programming and computational art in an easy way, thus helping you boost your creativity. "Processing 2: Creative Programming Cookbook" will guide you to explore and experience the open source Processing language and environment, helping you discover advanced features and exciting possibilities with this programming environment like never before. You'll learn the basics of 2D and 3D graphics programming, and then quickly move up to advanced topics such as audio and video visualization, computer vision, and much more with this comprehensive guide. Since its birth in 2001, Processing has grown a lot. What started out as a project by Ben Fry and Casey Reas has now become a widely used graphics programming language. Processing 2 has a lot of new and exciting features. This cookbook will guide you to explore the completely new and cool graphics engine and video library. Using the recipes in this cookbook, you will be able to build interactive art for desktop computers, Internet, and even Android devices! You don't even have to use a keyboard or mouse to interact with the art you make. The book's next-gen technologies will teach you how to design interactions with a webcam or a microphone! Isn't that amazing? "Processing 2: Creative Programming Cookbook" will guide you to explore the Processing language and environment using practical and useful recipes.
Table of Contents (18 chapters)
Processing 2: Creative Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Making polygon soup


In the Drawing 3D primitives recipe, you've learned that Processing comes with two 3D primitives: the box and the sphere. Although these two shapes can be used to do great things, you might have more fun creating your own 3D shapes. We'll take a look at how you can create a flexible function to draw a cylinder in this recipe.

How to do it...

Just like in the previous recipes, you should start by importing the OpenGL library and setting up a window with a size of 640 x 480 pixels. The next thing we'll do is to write a function that will draw a cylinder to the screen. The code for the top and bottom of the cylinder should look familiar; we've used something similar in the Drawing custom shapes recipe, in the previous chapter. The code for the side of the cylinder is a little different.

void cylinder( int numSegments, float h, float r )
{
  float angle = 360.0 / (float)numSegments;
    
  // top
  beginShape();
  for ( int i = 0; i < numSegments; i++ ) {
    float x = cos...