Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <time.h>
- using namespace std;
- double Noise(int x, int y);
- double SmoothNoise_1(float x, float y);
- double Cosine_Interpolate(float a, float b, float x);
- double InterpolatedNoise_1(float x, float y);
- double PerlinNoise_2D(float persistence, int Number_Of_Octaves, float x, float y);
- const int MAPWIDTH = 4;
- const int MAPHEIGHT = 16;
- int SEED;
- double mapHeight[MAPWIDTH][MAPHEIGHT];
- int main()
- {
- srand(time(NULL));
- SEED = rand();
- for(int y = 0; y < MAPHEIGHT; y++)
- {
- for(int x = 0; x < MAPWIDTH; x++)
- {
- mapHeight[x][y] = PerlinNoise_2D(0.5, 6, SEED + x, SEED + y);
- //mapHeight[x][y] = InterpolatedNoise_1(SEED + x, SEED + y);
- }
- }
- for(int y = 0; y < MAPHEIGHT; y++)
- {
- for(int x = 0; x < MAPWIDTH; x++)
- {
- cout << mapHeight[x][y] << "\t";
- }
- cout << endl;
- }
- cin.get();
- return 0;
- }
- double Noise_1(int x, int y)
- {
- int n = x + y * 57;
- n = (n << 13) ^ n;
- return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
- }
- /*
- double Noise_2(int x, int y)
- {
- int n = x + y * 57;
- n = (n << 13) ^ n;
- return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
- }
- double Noise_3(int x, int y)
- {
- int n = x + y * 57;
- n = (n << 13) ^ n;
- return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
- }
- double Noise_4(int x, int y)
- {
- int n = x + y * 57;
- n = (n << 13) ^ n;
- return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
- }
- */
- double SmoothNoise_1(float x, float y)
- {
- float corners = (Noise_1(x - 1, y - 1) + Noise_1(x + 1, y - 1) + Noise_1(x - 1, y + 1) + Noise_1(x + 1, y + 1)) / 16;
- float sides = (Noise_1(x - 1, y) + Noise_1(x + 1, y) + Noise_1(x, y - 1) + Noise_1(x, y + 1)) / 8;
- float center = Noise_1(x, y) / 4;
- return corners + sides + center;
- }
- /*
- double SmoothNoise_2(float x, float y)
- {
- float corners = (Noise_2(x - 1, y - 1) + Noise_2(x + 1, y - 1) + Noise_2(x - 1, y + 1) + Noise_2(x + 1, y + 1)) / 16;
- float sides = (Noise_2(x - 1, y) + Noise_2(x + 1, y) + Noise_2(x, y - 1) + Noise_2(x, y + 1)) / 8;
- float center = Noise_2(x, y) / 4;
- return corners + sides + center;
- }
- double SmoothNoise_3(float x, float y)
- {
- float corners = (Noise_3(x - 1, y - 1) + Noise_3(x + 1, y - 1) + Noise_3(x - 1, y + 1) + Noise_3(x + 1, y + 1)) / 16;
- float sides = (Noise_3(x - 1, y) + Noise_3(x + 1, y) + Noise_3(x, y - 1) + Noise_3(x, y + 1)) / 8;
- float center = Noise_3(x, y) / 4;
- return corners + sides + center;
- }
- double SmoothNoise_4(float x, float y)
- {
- float corners = (Noise_4(x - 1, y - 1) + Noise_4(x + 1, y - 1) + Noise_4(x - 1, y + 1) + Noise_4(x + 1, y + 1)) / 16;
- float sides = (Noise_4(x - 1, y) + Noise_4(x + 1, y) + Noise_4(x, y - 1) + Noise_4(x, y + 1)) / 8;
- float center = Noise_4(x, y) / 4;
- return corners + sides + center;
- }
- */
- double Cosine_Interpolate(float a, float b, float x)
- {
- float ft = x * 3.1415927;
- float f = (1 - cos(ft)) * .5;
- return a*(1 - f) + b*f;
- }
- double InterpolatedNoise_1(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;
- double v1 = SmoothNoise_1(integer_X, integer_Y);
- double v2 = SmoothNoise_1(integer_X + 1, integer_Y);
- double v3 = SmoothNoise_1(integer_X, integer_Y + 1);
- double v4 = SmoothNoise_1(integer_X + 1, integer_Y + 1);
- /*
- double v2 = SmoothNoise_2(integer_X + 1, integer_Y);
- double v3 = SmoothNoise_3(integer_X, integer_Y + 1);
- double v4 = SmoothNoise_4(integer_X + 1, integer_Y + 1);*/
- double i1 = Cosine_Interpolate(v1, v2, fractional_X);
- double i2 = Cosine_Interpolate(v3, v4, fractional_X);
- return Cosine_Interpolate(i1, i2, fractional_Y);
- }
- double PerlinNoise_2D(float persistence, int Number_Of_Octaves, float x, float y)
- {
- float total = 0;
- int p = persistence;
- int n = Number_Of_Octaves - 1;
- for (int i = 0; i < n; i++)
- {
- float frequency = 2^i;
- float amplitude = p^i;
- total = total + InterpolatedNoise_1(x * frequency, y * frequency) * amplitude;
- }
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement