class SGL_Perlin {//http://freespace.virgin.net/hugo.elias/models/m_perlin.htm private: float Linear_Interpolate(float a, float b, float x) { return a*(1.0f-x) + b*x; } float Cosine_Interpolate(float a, float b, float x) { float ft = x * 3.1415927f; float f = (1.0f - cosf(ft)) * 0.5f; return a*(1.0f-f) + b*f; } public: float persistence; int Number_Of_Octaves; float SGL_Noise2D(float x, float y) { int n = (int)x + (int)y * 57; n = (n<<13) ^ n; return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); } float SGL_SmoothNoise_2D(float x, float y) { float corners = ( SGL_Noise2D(x-1, y-1)+SGL_Noise2D(x+1, y-1)+SGL_Noise2D(x-1, y+1)+SGL_Noise2D(x+1, y+1) ) / 16.0f; float sides = ( SGL_Noise2D(x-1, y) +SGL_Noise2D(x+1, y) +SGL_Noise2D(x, y-1) +SGL_Noise2D(x, y+1) ) / 8.0f; float center = SGL_Noise2D(x, y) / 4.0f; return corners + sides + center; } float SGL_InterpolatedNoise_2D(float x, float y) { int integer_X = int(x); float fractional_X = x - integer_X; int integer_Y = int(y); float fractional_Y = y - integer_Y; float v1 = SGL_SmoothNoise_2D((float)integer_X, (float)integer_Y); float v2 = SGL_SmoothNoise_2D((float)integer_X + 1, (float)integer_Y); float v3 = SGL_SmoothNoise_2D((float)integer_X, (float)integer_Y + 1); float v4 = SGL_SmoothNoise_2D((float)integer_X + 1, (float)integer_Y + 1); float i1 = Cosine_Interpolate(v1 , v2 , fractional_X); float i2 = Cosine_Interpolate(v3 , v4 , fractional_X); return Cosine_Interpolate(i1 , i2 , fractional_Y); } float PerlinNoise_2D(float x, float y) { float total = 0; float p = persistence; int n = Number_Of_Octaves - 1; for(int i = 0; i<=n;i++) { float frequency = 2.0f*(float)i;// powf(2.0f,(float)i); float amplitude = p*(float)i;//powf(p,(float)i); total = total + SGL_InterpolatedNoise_2D(x * frequency, y * frequency) * amplitude; } return total; } };