src/rcrt/textures/PerlinNoise.h

Go to the documentation of this file.
00001 // JAVA REFERENCE IMPLEMENTATION OF IMPROVED NOISE - COPYRIGHT 2002 KEN PERLIN.
00002 // http://mrl.nyu.edu/~perlin/noise/
00003 // translated to C++ for 6.837
00004 
00005 #ifndef _PERLIN_NOISE_H_
00006 #define _PERLIN_NOISE_H_
00007 
00008 #include <math.h>
00009 #include <stdio.h>
00010 #include "../math/Point3D.h"
00011 
00012 class PerlinNoise {
00013 
00014 public:
00015    static float noise(const rcrt::Point3D& q) {
00016            //std::cout << "enter noise - q: " << q << std::endl;
00017           float x = q.x(); float y = q.y(); float z = q.z();
00018       int X = (int)floor(x) & 255;                  // FIND UNIT CUBE THAT
00019       int Y = (int)floor(y) & 255;                  // CONTAINS POINT.
00020       int Z = (int)floor(z) & 255;
00021       x -= floor(x);                                // FIND RELATIVE X,Y,Z
00022       y -= floor(y);                                // OF POINT IN CUBE.
00023       z -= floor(z);
00024       float u = fade(x);                                // COMPUTE FADE CURVES
00025       float v = fade(y);                                // FOR EACH OF X,Y,Z.
00026       float w = fade(z);
00027       int A = p[X  ]+Y; int AA = p[A]+Z; int AB = p[A+1]+Z;      // HASH COORDINATES OF
00028       int B = p[X+1]+Y; int BA = p[B]+Z; int BB = p[B+1]+Z;      // THE 8 CUBE CORNERS,
00029       return lerp(w, lerp(v, lerp(u, grad(p[AA  ], x  , y  , z   ),  // AND ADD
00030                                      grad(p[BA  ], x-1, y  , z   )), // BLENDED
00031                              lerp(u, grad(p[AB  ], x  , y-1, z   ),  // RESULTS
00032                                      grad(p[BB  ], x-1, y-1, z   ))),// FROM  8
00033                      lerp(v, lerp(u, grad(p[AA+1], x  , y  , z-1 ),  // CORNERS
00034                                      grad(p[BA+1], x-1, y  , z-1 )), // OF CUBE
00035                              lerp(u, grad(p[AB+1], x  , y-1, z-1 ),
00036                                      grad(p[BB+1], x-1, y-1, z-1 ))));
00037    }
00038    
00039    static float turbulence(const rcrt::Point3D& p, float detail, float quot)
00040    {
00041            //std::cout << "enter turbulence" << std::endl;
00042                 float result(0.0f);
00043                 rcrt::Point3D q = p;
00044 
00045                 for (float scale = 1.0f; scale > detail; scale /= quot)
00046                 {
00047                         q  = q/scale;
00048                         result += noise(q) * scale;
00049                 }
00050                 //std::cout << "return turbulence" << std::endl;
00051                 return result;
00052    }
00053 
00054 private:
00055    static float fade(float t) { 
00056      return t * t * t * (t * (t * 6 - 15) + 10); }
00057    static float lerp(float t, float a, float b) { 
00058      return a + t * (b - a); }
00059    static float grad(int hash, float x, float y, float z) {
00060       int h = hash & 15;                      // CONVERT LO 4 BITS OF HASH CODE
00061       float u = h<8 ? x : y;                 // INTO 12 GRADIENT DIRECTIONS.
00062       float v = h<4 ? y : h==12||h==14 ? x : z;
00063       return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v); }
00064   // permutation
00065   static int p[512];
00066 };
00067 
00068 #endif

Generated on Thu Jan 31 19:26:20 2008 for RenderingCompetitionRayTracer by  doxygen 1.5.3