PerlinNoise Class Reference

Class used to generate 2D perlin noise. More...

#include <PerlinNoise.h>

List of all members.

Public Member Functions

 PerlinNoise (int size_x, int size_y)
 ~PerlinNoise ()
void SetForegroundColor (float red, float green, float blue, float alpha)
void SetBackgroundColor (float red, float green, float blue, float alpha)
void SetParameters (float frequency, float persistence, int octaves, bool smooth)
void SetInterpolationMode (int mode)
void TransformXY (float(*processor)(float x, float y, float value))
void TransformAB (float a, float b, float(*processor)(float a, float b, float value))
void GenerateNoise ()
void ExportRGB256 (GLubyte *data_pointer)
void Export (float *data_pointer)
void GetDimensions (int &dim_x, int &dim_y)
void Push ()
void FlushToColorBuffer ()
void CombineColorBuffers (float(*combiner)(float val_1, float val_2))
void CombineColorBuffersAB (float a, float b, float(*combiner)(float a, float b, float val_1, float val_2))
void SmoothBorder (int border)

Static Public Member Functions

static float RangeTransform (float a, float b, float perlin_xy)
static float LinearTransform (float a, float b, float perlin_xy)
static float MarblyTransform (float x, float y, float perlin_xy)
static float GrainyTransform (float x, float y, float perlin_xy)
static float SumCombiner (float val_1, float val_2)
static float ProductCombiner (float val_1, float val_2)
static float MaximumCombiner (float val_1, float val_2)
static float WeightedSumCombiner (float weight_1, float weight_2, float val_1, float val_2)

Private Types

typedef PerlinNoise::StackElem PerlinStack

Private Member Functions

 PerlinNoise ()
void Pop ()
bool IsEmpty ()
float GetSmoothedValue (float x, float y)
float GetNoiseValue (float x, float y)
float GetNoise2D (int x, int y)
float Interpolate (float value_1, float value_2, float fraction)
float LinearInterpolation (float value_1, float value_2, float fraction)
float CosineInterpolation (float value_1, float value_2, float fraction)
float CubicInterpolation (float value_1, float value_2, float fraction)

Private Attributes

int size_x
int size_y
PerlinStacktop

Classes

struct  StackElem


Detailed Description

Class used to generate 2D perlin noise.

PerlinNoise is a class that can be used to generate 2 dimensional perlin noise. It contains some modified, rewritten code from the two internet sources listed below.

The whole class is stack based, which means that additional elements can be pushed onto the stack and any operation like generating the noise, transforming the noise or setting parameters is only applied to the top of the stack. Furthermore, there are some combination routines, which combine the top two elements of the stack into one single element and thereby reduce the stack size by one.

Every stack element contains two buffers, a buffer with the actual noise and a color buffer. The actual noise buffer can be filled by generating noise (with some parameters). This buffer can then be modified by applying certain transformation routines to each value of the buffer. This buffer can either be exported directly or be written into the color buffer of the stack element. This procedure will generate color values from the noise values by interpolating between the foreground and background color attributes. The color buffers of the stack elements can then either be exported to an array of color values or combined with each other by applying a certain combination routine to each of the color values.

For transforming the noise in the noise buffer or combining two color buffers of the stack, there are some basic routines implemented as static members of this class. But the transformations and combinations are in no way restriced to these routines, any routine can be used for transforming and combining the noise as long as it has the correct argument and return value types.

Sources: - http://www.animeimaging.com/asp/PerlinNoise.aspx

Definition at line 46 of file PerlinNoise.h.


Member Typedef Documentation

typedef struct PerlinNoise::StackElem PerlinNoise::PerlinStack [private]
 

Structure holding an element of the stack.


Constructor & Destructor Documentation

PerlinNoise::PerlinNoise int  size_x,
int  size_y
 

Constructor: Brings the object into a consistent state. The dimensions of the perlin noise has to be specified when the object is created.

Parameters:
size_x x dimension of the perlin noise.
size_y y dimension of the perlin noise.

Definition at line 3 of file PerlinNoise.cpp.

PerlinNoise::~PerlinNoise  ) 
 

Destructor.

Definition at line 14 of file PerlinNoise.cpp.

PerlinNoise::PerlinNoise  )  [private]
 

Hide the default constructor.


Member Function Documentation

void PerlinNoise::CombineColorBuffers float(*)(float val_1, float val_2)  combiner  ) 
 

Combines the values of the color buffers of the 2 topmost elements on the stack by applying a function to all the values in the color buffer. After the two color buffers have been combined, the top element of the stack is popped from the stack. The resulting color buffer will then be on top of the stack.

Parameters:
combiner Pointer to a function that takes to values as input and returns an output value. This function will be applied to all the values of the two color buffers.

Definition at line 195 of file PerlinNoise.cpp.

void PerlinNoise::CombineColorBuffersAB float  a,
float  b,
float(*)(float a, float b, float val_1, float val_2)  combiner
 

Combines the values of the color buffers of the 2 topmost elements on the stack by applying a function that takes two additional parameters to all the values in the color buffer. After the two color buffers have been combined, the top element of the stack is popped from the stack. The resulting color buffer will then be on top of the stack

Parameters:
a First parameter that shall be fed to the combiner function.
b Second parameter that shall be fed to the combiner function.
combiner Pointer to the combiner function. This function will be fed with both parameters as well as the two values of the color buffers and it must return the resulting color value.

Definition at line 214 of file PerlinNoise.cpp.

float PerlinNoise::CosineInterpolation float  value_1,
float  value_2,
float  fraction
[private]
 

Use cosine interpolation between two values.

Parameters:
value_1 First value for the interpolation.
value_2 Second value for the interpolation.
fraction Fraction between 0 and 1 used for the interpolation. (0: first value, 1: second value).

Definition at line 336 of file PerlinNoise.cpp.

float PerlinNoise::CubicInterpolation float  value_1,
float  value_2,
float  fraction
[private]
 

Use cubic interpolation between two values.

Parameters:
value_1 First value for the interpolation.
value_2 Second value for the interpolation.
fraction Fraction between 0 and 1 used for the interpolation. (0: first value, 1: second value).

Definition at line 342 of file PerlinNoise.cpp.

void PerlinNoise::Export float *  data_pointer  ) 
 

Exports the values of the generated perlin noise on top of the stack to an array of single precision floating point values.

Parameters:
data_pointer pointer to an array of single precision floating point values. This array will be filled with the perlin noise values and its size has to be of at least size_x * size_y elements.

Definition at line 187 of file PerlinNoise.cpp.

void PerlinNoise::ExportRGB256 GLubyte *  data_pointer  ) 
 

Exports the values of the color buffer on top of the stack to an array of unsigned bytes in RGB format. This routine does not produce any meaningful result if 'FlushToColorBuffer ()' has not been called on the element currently on top of the stack.

Parameters:
data_pointer pointer to an array of unsigned bytes. This array will be filled with color values from 0 to 255 in RGB format and its size has to be of at least size_x * size_y * 3 elements.

Definition at line 172 of file PerlinNoise.cpp.

void PerlinNoise::FlushToColorBuffer  ) 
 

Flushes the perlin noise to the color buffer by linearly interpolating the values of the perlin noise between the background and foreground color. Because the values of the perlin noise may be smaller than 0 or bigger than 1, there is no guarantee that the values written to the color buffer are between the background and foreground color. This can, however, be achieved by appying a range transform (0..1) on the perlin noise before flushing it to the color buffer. This routine does only affect the top of the stack.

Definition at line 67 of file PerlinNoise.cpp.

void PerlinNoise::GenerateNoise  ) 
 

Generates a two dimensional perlin noise using the actual parameters. Transformations, direct exports and flushed to the color buffer do not produce meaningful results unless this routine has been called first. This routine does only affect the top of the stack.

Definition at line 111 of file PerlinNoise.cpp.

void PerlinNoise::GetDimensions int &  dim_x,
int &  dim_y
 

Returns the x and y dimension of the perlin noise.

Parameters:
dim_x Variable passed by reference. After the routine, this value will contain the size of the perlin noise in the x dimension.
dim_y Variable passed by reference. After the routine, this value will contain the size of the perlin noise in the y dimension.

Definition at line 105 of file PerlinNoise.cpp.

float PerlinNoise::GetNoise2D int  x,
int  y
[private]
 

Gets the noise at a discrete location.

Parameters:
x Discrete x coordinate for which the noise value shall be retrieved.
y Discrete y coordinate for which the noise value shall be retrieved.

Definition at line 303 of file PerlinNoise.cpp.

float PerlinNoise::GetNoiseValue float  x,
float  y
[private]
 

Retrives noise at one specific point.

Parameters:
x X coordinate for which the noise value shall be retrieved.
y Y coordinate for which the noise value shall be retrieved.

Definition at line 278 of file PerlinNoise.cpp.

float PerlinNoise::GetSmoothedValue float  x,
float  y
[private]
 

Retrieves a smoothed noise value at one specific point. This is done by evaluating the noise at (x-1,y-1), (x,y-1), (x+1,y-1), (x-1,y), (x,y), (x+1,y-1), (x-1,y+1), (x,y+1), (x+1,y+1) and weighting these values in a meaningful way.

Parameters:
x X coordinate for which the smoothed noise value shall be retrieved.
y Y coordinate for which the smoothed noise value shall be retrieved.

Definition at line 265 of file PerlinNoise.cpp.

float PerlinNoise::GrainyTransform float  x,
float  y,
float  perlin_xy
[static]
 

Predefined transformation function that can be used with 'TransformXY (...)'. Makes the noise appear grainy.

Parameters:
x X coordinate at which the input value shall be transformed.
y Y coordinate at which the input value shall be transformed.
perlin_xy Input value.

Definition at line 367 of file PerlinNoise.cpp.

float PerlinNoise::Interpolate float  value_1,
float  value_2,
float  fraction
[private]
 

Interpolates between two values using the interpolation specified by the interpolation mode.

Parameters:
value_1 First value for the interpolation.
value_2 Second value for the interpolation.
fraction Fraction between 0 and 1 used for the interpolation. (0: first value, 1: second value).

Definition at line 315 of file PerlinNoise.cpp.

bool PerlinNoise::IsEmpty  )  [private]
 

Checks wheather there are elements on the stack or not.

Definition at line 62 of file PerlinNoise.cpp.

float PerlinNoise::LinearInterpolation float  value_1,
float  value_2,
float  fraction
[private]
 

Interpolate linearly between two values.

Parameters:
value_1 First value for the interpolation.
value_2 Second value for the interpolation.
fraction Fraction between 0 and 1 used for the interpolation.

Definition at line 331 of file PerlinNoise.cpp.

float PerlinNoise::LinearTransform float  a,
float  b,
float  perlin_xy
[static]
 

Predefined transformation function that can be used with 'TransformAB (...)'. Transforms each point independently with new_value = old_value * a + b.

Parameters:
a Input value will be multiplied by this value.
b Offset that will be added to the input value.
perlin_xy Input value.

Definition at line 357 of file PerlinNoise.cpp.

float PerlinNoise::MarblyTransform float  x,
float  y,
float  perlin_xy
[static]
 

Predefined transformation function that can be used with 'TransformXY (...)'. Makes the noise appear marbly.

Parameters:
x X coordinate at which the input value shall be transformed.
y Y coordinate at which the input value shall be transformed.
perlin_xy Input value.

Definition at line 362 of file PerlinNoise.cpp.

float PerlinNoise::MaximumCombiner float  val_1,
float  val_2
[static]
 

Predefined combiner function that can be used with 'CombineColorBuffers'. Returns the maximum of the two input values.

Parameters:
val_1 Value from the first buffer.
val_2 Value from the second buffer.
Returns:
The maximum of both values.

Definition at line 385 of file PerlinNoise.cpp.

void PerlinNoise::Pop  )  [private]
 

Pops the top element from the stack. Internally used after combining color buffers.

Definition at line 46 of file PerlinNoise.cpp.

float PerlinNoise::ProductCombiner float  val_1,
float  val_2
[static]
 

Predefined combiner function that can be used with 'CombineColorBuffers'. Returns the product of the two input values.

Parameters:
val_1 Value from the first buffer.
val_2 Value from the second buffer.
Returns:
The product of both values.

Definition at line 380 of file PerlinNoise.cpp.

void PerlinNoise::Push  ) 
 

Pushes a new element onto the stack.

Definition at line 20 of file PerlinNoise.cpp.

float PerlinNoise::RangeTransform float  a,
float  b,
float  perlin_xy
[static]
 

Predefined transformation function that can be used with 'TransformAB (...)'. Makes sure that the resulting value lies within a certain interval.

Parameters:
a Lower bound of the interval, all returned values will be at least as big as this parameter.
b Upper bound of the interval, all returned values will be smaller or equal to this parameter.
perlin_xy Input value.

Definition at line 349 of file PerlinNoise.cpp.

void PerlinNoise::SetBackgroundColor float  red,
float  green,
float  blue,
float  alpha
 

Sets the background color that will be used when the noise is transformed into color space. This routine does only affect the top of the stack.

Parameters:
red Red part of the background color.
green Green part of the background color.
blue Blue part of the background color.
alpha Alpha value of the background color.

Definition at line 76 of file PerlinNoise.cpp.

void PerlinNoise::SetForegroundColor float  red,
float  green,
float  blue,
float  alpha
 

Sets the foreground color that will be used when the noise is transformed into color space. This routine does only affect the top of the stack.

Parameters:
red Red part of the foreground color.
green Green part of the foreground color.
blue Blue part of the foreground color.
alpha Alpha value of the foreground color.

Definition at line 84 of file PerlinNoise.cpp.

void PerlinNoise::SetInterpolationMode int  mode  ) 
 

Sets the mode of interpolation used between the bare random noise values. This routine does only affect the top of the stack.

Parameters:
mode Interpolation mode (LINEAR_INTERPOLATION, COSINE_INTERPOLATION or CUBIC_INTERPOLATION).

Definition at line 100 of file PerlinNoise.cpp.

void PerlinNoise::SetParameters float  frequency,
float  persistence,
int  octaves,
bool  smooth
 

Sets the parameters used for the noise generation. This routine does only affect the top of the stack.

Parameters:
frequency Lowest frequency of the noise. This frequency is independent of the size of the noise.
persistence Persistence defines how much higher frequencies are weighted with respect to the lower frequencies.
octaves Number of noise patterns that are added to the final noise.
smooth Flag indicating whether the noise shall be smoothed or not.

Definition at line 92 of file PerlinNoise.cpp.

void PerlinNoise::SmoothBorder int  border  ) 
 

Smoothes the noise at the border such by linearly interpolating the values that are not farther away from the border that a certain distance. All values on the border are will be zero afterwards.

Parameters:
border Width of border in which the values are interpolated linearly.

Definition at line 233 of file PerlinNoise.cpp.

float PerlinNoise::SumCombiner float  val_1,
float  val_2
[static]
 

Predefined combiner function that can be used with 'CombineColorBuffers'. Returns the sum of the two input values.

Parameters:
val_1 Value from the first buffer.
val_2 Value from the second buffer.
Returns:
The sum of both values.

Definition at line 375 of file PerlinNoise.cpp.

void PerlinNoise::TransformAB float  a,
float  b,
float(*)(float a, float b, float value)  processor
 

Transforms the generated noise with a function that takes two parameters a and b. Each value of the noise is transformed by calling the function with both parameters on each value of the perlin noise. This routine does only affect the top of the stack.

Parameters:
a First parameter that will be fed to the function specified as the third argument of this routine.
b Second parameter that will be fed to the function specified as the third argument of this routine.
processor Pointer to a function which takes two parameter as well as an input value and returns a transformed value.

Definition at line 164 of file PerlinNoise.cpp.

void PerlinNoise::TransformXY float(*)(float x, float y, float value)  processor  ) 
 

Transforms the generated noise with a function that depends on the x and y coordinated of the noise. x and y, which are fed to this function, are not the dimensions of the noise. They merely depend on the frequency times coordinate values between 0 and 1 (0 at top and left/ 1 at bottom and right). This routine does only affect the top of the stack.

Parameters:
processor Pointer to a function which takes an x and y coordinate as well as an input value and returns a transformed value.

Definition at line 149 of file PerlinNoise.cpp.

float PerlinNoise::WeightedSumCombiner float  weight_1,
float  weight_2,
float  val_1,
float  val_2
[static]
 

Predefined combiner function that can be used with 'CombineColorBuffersAB'. Returns the weighted sum of the two input values.

Parameters:
weight_1 The weight for the value from the first buffer.
weight_2 The weight for the value from the second buffer.
val_1 Value from the first buffer.
val_2 Value from the second buffer.
Returns:
The weighted sum of both values (weight_1 * val_1 + weight_2 * val_2).

Definition at line 390 of file PerlinNoise.cpp.


Member Data Documentation

int PerlinNoise::size_x [private]
 

Size of the noise along the x dimension.

Definition at line 300 of file PerlinNoise.h.

int PerlinNoise::size_y [private]
 

Size of the noise along the y dimention.

Definition at line 303 of file PerlinNoise.h.

PerlinStack* PerlinNoise::top [private]
 

Top element of the stack.

Definition at line 322 of file PerlinNoise.h.


Generated on Sun Jul 2 13:20:41 2006 for Demo by  doxygen 1.4.6-NO