Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering openFrameworks: Creative Coding Demystified

You're reading from   Mastering openFrameworks: Creative Coding Demystified openFrameworks is the doorway to so many creative multimedia possibilities and this book will tell you everything you need to know to undertake your own projects. You'll find creative coding is simpler than you think.

Arrow left icon
Product type Paperback
Published in Sep 2013
Publisher Packt
ISBN-13 9781849518048
Length 364 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Denis Perevalov Denis Perevalov
Author Profile Icon Denis Perevalov
Denis Perevalov
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Mastering openFrameworks: Creative Coding Demystified
Credits
Foreword
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. openFrameworks Basics FREE CHAPTER 2. Drawing in 2D 3. Building a Simple Particle System 4. Images and Textures 5. Working with Videos 6. Working with Sounds 7. Drawing in 3D 8. Using Shaders 9. Computer Vision with OpenCV 10. Using Depth Cameras 11. Networking Working with Addons Perlin Noise Index

Space-coherent noise


There are a number of overloaded ofNoise() functions that depend on two, three, or four parameters: ofNoise( x, y ), ofNoise( x, y, z ), and ofNoise( x, y, z, t ). They have behavior like ofNoise( t ) but use several input parameters. Their coordinates may be scaled as 2D or 3D space coordinates of some point and may even include time. So such functions give a way for generating 2D, 3D, and 4D fields with coherently changing values that are static or evolving with time. For example, the ofNoise( x, y ) function can be used for drawing a smooth random texture in the following way:

float scaleX = 0.007;    //1.0 / scaleX is coherence in x
float scaleY = 0.008;    //1.0 / scaleY is coherence in y
float posX0 = 593.2;
float posY0 = 43.7;
for (int y=0; y<500; y++) {
  for (int x=0; x<500; x++) {
    float value = ofNoise( x*scaleX+posX0, y*scaleY+posY0 );
    ofSetColor( value*255, value*255, value*255 );
    ofRect( x, y, 1, 1 );
  }
}

Note

This is example 13-PerlinNoise/03-PerlinTexture.

On running the code, you will see the following texture:

Now add the third parameter in the calling function ofNoise(), which increases with time, and you will obtain a texture that slowly evolves with time.

float value = ofNoise( x*scaleX+posX0, y*scaleY+posY0,
                      time*0.1 + 445.6 );

This code is simple and great for demonstrating the idea. But it works extremely slowly because we render each texture's pixel as a separate rectangle with size 1 × 1. To obtain real-time performance, prepare the texture colors in the ofImage image object and draw it in one step using image.draw() (see the Creating images section in Chapter 4, Images and Textures).

Tip

The fastest way of generating textures is by using shaders; see the A liquify distortion example section in Chapter 8, Using Shaders. Though the example is about texture distortion with Perlin noise, it is simple to change it for generating textures. Using shaders is especially useful while generating really big textures in real time.

A similar technique can be used for generating values for evolving height maps of 3D surfaces (see The oscillating plane example section in Chapter 7, Drawing in 3D).

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image