perlinNoise.h

Go to the documentation of this file.
00001 // perlinNoise.h
00002 // Class to implement coherent noise over 1, 2, or 3 dimensions.
00003 // Implementation based on the Perlin noise function. Thanks to 
00004 // Ken Perlin of NYU for publishing his algorithm online.
00006 // Copyright (c) 2001, Matt Zucker
00007 // You may use this source code as you wish, but it is provided
00008 // with no warranty. Please email me at mazucker@vassar.edu if 
00009 // you find it useful.
00011 
00012 #ifndef _NOISE_CLASS_H_
00013 #define _NOISE_CLASS_H_
00014 
00015 #include <stdlib.h>
00016 
00017 // It must be true that (x % NOISE_WRAP_INDEX) == (x & NOISE_MOD_MASK)
00018 // so NOISE_WRAP_INDEX must be a power of two, and NOISE_MOD_MASK must be
00019 // that power of 2 - 1.  as indices are implemented, as unsigned chars,
00020 // NOISE_WRAP_INDEX shoud be less than or equal to 256.
00021 // There's no good reason to change it from 256, really.
00022 
00023 #define NOISE_WRAP_INDEX  256
00024 #define NOISE_MOD_MASK    255
00025 
00026 // A large power of 2, we'll go for 4096, to add to negative numbers
00027 // in order to make them positive
00028 
00029 #define NOISE_LARGE_PWR2  4096
00030 
00031 class perlinNoise {
00032   private:
00033     static unsigned initialized;
00034   
00035     static unsigned permutationTable[NOISE_WRAP_INDEX*2 + 2]; // permutation table
00036     static float    gradientTable1d[NOISE_WRAP_INDEX*2 + 2];  // 1d gradient lookup table.
00037     static float    gradientTable2d[NOISE_WRAP_INDEX*2 + 2][2]; // 2d gradient lookup table.
00038     static float    gradientTable3d[NOISE_WRAP_INDEX*2 + 2][3]; // 3d gradient lookup table.
00039   static float    gradientTable4d[NOISE_WRAP_INDEX*2 + 2][4]; // 4d gradient lookup table.
00040     
00041     static float    randNoiseFloat();     // generate a random float in [-1,1]
00042     static inline void     normalize2d(float vector[2]);  // normalize a 2d vector
00043     static inline void     normalize3d(float vector[3]);  // normalize a 3d vector
00044     static inline void     normalize4d(float vector[4]);  // normalize a 3d vector
00045     static void     generateLookupTables();   // fill in table entries
00046     
00047   public:
00048     static void     reseed();     // reseed random generator & regenerate tables
00049     static void     reseed(unsigned int rSeed); // same, but with specified seed
00050 
00051     static float    noise1d(const float pos[1]);  // 1D call using an array for passing pos
00052     static float    noise2d(const float pos[2]);  // 2D call using an array for passing pos
00053     static float    noise3d(const float pos[3]);  // 3D call using an array for passing pos
00054   // static float    noise4d(const float pos[4]); // 4D call using an array for passing pos
00055 
00056 
00058 // you can call noise component-wise, too.
00059     
00060   static float noise(float x) { 
00061     return noise1d(&x);
00062   }
00063 
00064   static float noise(float x, float y) { 
00065     float p[2] = { x, y };
00066     return noise2d(p); 
00067   }
00068 
00069   static float noise(float x, float y, float z) { 
00070     float p[3] = { x, y, z };
00071     return noise3d(p);
00072   }
00073   /* static float noise(float x, float y, float z, w) { 
00074      float p[4] = { x, y, z, w };
00075     return noise4d(p);
00076     }*/
00077 
00078   /* static float    noise(float);    // use individual elements for passing pos
00079      static float    noise(float, float);
00080      static float    noise(float, float, float); */
00081 };
00082 
00083 #endif

Generated on Mon Mar 12 21:09:00 2007 for VEE - The Visual Effects Engine by  doxygen 1.4.6