core | glapi | vbo | math | shader | swrast | swrast_setup | tnl | tnl_dd

prog_noise.c File Reference


Detailed Description

C implementation of Perlin Simplex Noise over 1, 2, 3 and 4 dims.

Author:
Stefan Gustavson (stegu@itn.liu.se)
This implementation is "Simplex Noise" as presented by Ken Perlin at a relatively obscure and not often cited course session "Real-Time Shading" at Siggraph 2001 (before real time shading actually took on), under the title "hardware noise". The 3D function is numerically equivalent to his Java reference code available in the PDF course notes, although I re-implemented it from scratch to get more readable code. The 1D, 2D and 4D cases were implemented from scratch by me from Ken Perlin's text.

This file has no dependencies on any other file, not even its own header file. The header file is made for use by external code only.

#include "main/imports.h"
#include "prog_noise.h"

Defines

#define FASTFLOOR(x)   ( ((x)>0) ? ((int)x) : (((int)x)-1) )
#define F2   0.366025403f
#define G2   0.211324865f
#define F3   0.333333333f
#define G3   0.166666667f
#define F4   0.309016994f
#define G4   0.138196601f

Functions

static float grad1 (int hash, float x)
static float grad2 (int hash, float x, float y)
static float grad3 (int hash, float x, float y, float z)
static float grad4 (int hash, float x, float y, float z, float t)
GLfloat _mesa_noise1 (GLfloat x)
 1D simplex noise
GLfloat _mesa_noise2 (GLfloat x, GLfloat y)
 2D simplex noise
GLfloat _mesa_noise3 (GLfloat x, GLfloat y, GLfloat z)
 3D simplex noise
GLfloat _mesa_noise4 (GLfloat x, GLfloat y, GLfloat z, GLfloat w)
 4D simplex noise

Variables

unsigned char perm [512]
 Permutation table.
static unsigned char simplex [64][4]
 A lookup table to traverse the simplex around a given point in 4D.


Define Documentation

#define F2   0.366025403f

#define F3   0.333333333f

#define F4   0.309016994f

#define FASTFLOOR (  )     ( ((x)>0) ? ((int)x) : (((int)x)-1) )

#define G2   0.211324865f

#define G3   0.166666667f

#define G4   0.138196601f


Function Documentation

GLfloat _mesa_noise1 ( GLfloat  x  ) 

1D simplex noise

GLfloat _mesa_noise2 ( GLfloat  x,
GLfloat  y 
)

2D simplex noise

GLfloat _mesa_noise3 ( GLfloat  x,
GLfloat  y,
GLfloat  z 
)

3D simplex noise

GLfloat _mesa_noise4 ( GLfloat  x,
GLfloat  y,
GLfloat  z,
GLfloat  w 
)

4D simplex noise

static float grad1 ( int  hash,
float  x 
) [static]

static float grad2 ( int  hash,
float  x,
float  y 
) [static]

static float grad3 ( int  hash,
float  x,
float  y,
float  z 
) [static]

static float grad4 ( int  hash,
float  x,
float  y,
float  z,
float  t 
) [static]


Variable Documentation

unsigned char perm[512]

Permutation table.

This is just a random jumble of all numbers 0-255, repeated twice to avoid wrapping the index at 255 for each lookup. This needs to be exactly the same for all instances on all platforms, so it's easiest to just keep it as static explicit data. This also removes the need for any initialisation of this class.

Note that making this an int[] instead of a char[] might make the code run faster on platforms with a high penalty for unaligned single byte addressing. Intel x86 is generally single-byte-friendly, but some other CPUs are faster with 4-aligned reads. However, a char[] is smaller, which avoids cache trashing, and that is probably the most important aspect on most architectures. This array is accessed a *lot* by the noise functions. A vector-valued noise over 3D accesses it 96 times, and a float-valued 4D noise 64 times. We want this to fit in the cache!

unsigned char simplex[64][4] [static]

Initial value:

 {
   {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 0, 0, 0}, {0, 2, 3, 1},
   {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 3, 0},
   {0, 2, 1, 3}, {0, 0, 0, 0}, {0, 3, 1, 2}, {0, 3, 2, 1},
   {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 3, 2, 0},
   {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
   {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
   {1, 2, 0, 3}, {0, 0, 0, 0}, {1, 3, 0, 2}, {0, 0, 0, 0},
   {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 3, 0, 1}, {2, 3, 1, 0},
   {1, 0, 2, 3}, {1, 0, 3, 2}, {0, 0, 0, 0}, {0, 0, 0, 0},
   {0, 0, 0, 0}, {2, 0, 3, 1}, {0, 0, 0, 0}, {2, 1, 3, 0},
   {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
   {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
   {2, 0, 1, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
   {3, 0, 1, 2}, {3, 0, 2, 1}, {0, 0, 0, 0}, {3, 1, 2, 0},
   {2, 1, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
   {3, 1, 0, 2}, {0, 0, 0, 0}, {3, 2, 0, 1}, {3, 2, 1, 0}
}
A lookup table to traverse the simplex around a given point in 4D.

Details can be found where this table is used, in the 4D noise method. TODO: This should not be required, backport it from Bill's GLSL code!


Generated on Sun Sep 27 06:48:02 2009 for Mesa Vertex and Fragment Program code by  doxygen 1.5.4