00001
00002
00003
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
00017 float x = q.x(); float y = q.y(); float z = q.z();
00018 int X = (int)floor(x) & 255;
00019 int Y = (int)floor(y) & 255;
00020 int Z = (int)floor(z) & 255;
00021 x -= floor(x);
00022 y -= floor(y);
00023 z -= floor(z);
00024 float u = fade(x);
00025 float v = fade(y);
00026 float w = fade(z);
00027 int A = p[X ]+Y; int AA = p[A]+Z; int AB = p[A+1]+Z;
00028 int B = p[X+1]+Y; int BA = p[B]+Z; int BB = p[B+1]+Z;
00029 return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ),
00030 grad(p[BA ], x-1, y , z )),
00031 lerp(u, grad(p[AB ], x , y-1, z ),
00032 grad(p[BB ], x-1, y-1, z ))),
00033 lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ),
00034 grad(p[BA+1], x-1, y , z-1 )),
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
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
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;
00061 float u = h<8 ? x : y;
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
00065 static int p[512];
00066 };
00067
00068 #endif