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

Using the ofNoise() function


openFrameworks has a built-in implementation of simplex noise, implemented in the ofNoise( t ) function. For example, the following code draws the Perlin noise function, ofNoise( t ), for t ranging from 0 to 10 on the screen:

ofSetColor( 0, 0, 0 );	
for (int x=0; x<1000; x++) {
  float t = x * 0.01;
  float y = ofNoise( t );
  ofLine( x, 300, x, 300 - y * 300 );
}

Note

This is the example 13-PerlinNoise/01-PerlinGraph.

Run the code and you will see the following graph:

Now replace the line float y = ofNoise( t ); with the following line:

float y = ofNoise( t + 493.0 );

This code renders the noise function in the range [443, 453].

Considering the preceding graphs, you can note the following properties:

  • The function values are from 0 to 1, and the mean value is clearly about 0.5. Hence you can think about the noise function as describing the fluctuation of some random parameter near its center value, equal to 0.5.

  • These two graphs depict different ranges of t – [0, 10] and [443, 493]. They look different but the scales of fluctuations in t are roughly the same for both these (and actually, any other) ranges of the same width. This property is called statistical homogeneity and means that the statistical properties of the function in the range [t, t+Q] for any fixed and big constant Q does not depend on any particular value of t. So you can get as many random functions as you want by just considering the noise function shifted by some constant in t. For example, ofNoise( t + 293.4 ) and ofNoise( t + 3996.4 ) will generate two distinct and uncorrelated random values for a given t.

Note

An interesting feature of this noise is that its value, ofNoise( t ), is equal to 0.5 for all the integer values of t. So do not wonder when you obtain a constant output from ofNoise( t )—just check, maybe you are accidentally using integer values for t.

You can use this function for randomly changing some parameters of the objects in your project; for example, position, size, or angle. Normally, we don't use the pure ofNoise( t ) function, but a more complex formula.

float value = amplitude * ofNoise( timePosition + position0 );

Here, amplitude defines the output range of the value, namely [0, amplitude]; position0 is a constant (for example, random); and timePosition is a value that increases with time.

See a simple example of the implementation of this idea for controlling sound volumes in The singing voices example section in Chapter 6, Working with Sounds. Using Perlin noise for creating a knot curve and changing its color is shown in The twisting knot example section in Chapter 7, Drawing in 3D.

Another interesting example is drawing a cloud of a hundred randomly flying points. It can be done with the following code:

ofSetColor( 0, 0, 0 );
float time = ofGetElapsedTimef();
for (int i=0; i<100; i++) {
  float ampX = ofGetWidth();
  float ampY = ofGetHeight();
  float speed = 0.1;
  float posX0 = i * 104.3 + 14.6;
  float posY0 = i * 53.3 + 35.2;
  float x = ampX * ofNoise( time * speed + posX0 );
  float y = ampY * ofNoise( time * speed + posY0 );
  ofCircle( x, y, 10 );
}

Note

This is example 13-PerlinNoise/02-PerlinPoints.

Here, constants 104.3, 14.6, 53.3, and 35.2 were chosen quite arbitrarily—just to obtain distinct values for posX0 and posY0. On running this, you will see a cloud of slowly flying points:

See the further evolution of this example in the Dancing cloud example section in Chapter 6, Working with Sounds.

By summing up several Perlin noises with different scales, it is possible to obtain more interesting noises. See the openFrameworks example in the openFrameworks' folder examples/math/noise1dOctaveExample. It sums up several noises and you can see the resultant function.

Note

There is a signed version of the ofNoise( t ) function. It's a function called ofSignedNoise( t ) that returns a noise value in the range [-1, 1]. The mean value of the function is zero. Actually, ofSignedNoise( t ) is just equal to 2.0*ofNoise( t ) – 1.0.

Now we will see how to use multidimensional Perlin noise for generating textures and other fields.

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