I'm having trouble with my perlin noise implementation. For some reason, it just seems too noisy. I don't see how I could use this for a game terrain. Here is a screenshot:
Here is my generation code:
double persistence = .25;
double octaves = 1;
double Game::GeneratePerlin2D(const double& x, const double& y)
{
double total = 0;
for (int i = 0; i < octaves; i++)
{
double frequency = pow(2, i);
double amplitude = pow(persistence, i);
total += InterpolatedNoise(x * frequency, y * frequency) * amplitude;
}
return total;
}
double Game::InterpolatedNoise(const double& x, const double& y)
{
int intX = (int)x;
double fractionX = x - intX;
int intY = (int)y;
double fractionY = y - intY;
// Get the smoothed noise values for each corner
double s = SmoothNoise(intX, intY); // Bottom left
double t = SmoothNoise(intX + 1, intY); // Bottom right
double u = SmoothNoise(intX, intY + 1); // Top left
double v = SmoothNoise(intX + 1, intY + 1); // Top right
// Interpolate the corner values to get the middle values
double i = Interpolate(s, t, fractionX); // Bottom middle
double ii = Interpolate(u, v, fractionX); // Top middle
// Interpolate the middle values to get the center value
return Interpolate(i, ii, fractionY);
}
double Game::SmoothNoise(const int& x, const int& y)
{
double corners = (Noise(x - 1, y - 1) + Noise(x + 1, y - 1) + Noise(x - 1, y + 1) + Noise(x + 1, y + 1)) / 16;
double sides = (Noise(x - 1, y) + Noise(x + 1, y) + Noise(x, y - 1) + Noise(x, y + 1)) / 8;
double center = Noise(x, y) / 4;
return corners + sides + center;
}
double Game::Interpolate(const double& a, const double& b, const double& w)
{
// Cosine interpolation
double ft = w * M_PI;
double f = (1 - cos(ft)) * 0.5;
return a * (1 - f) + b * f;
}
double Game::Noise(const int& x, const int& y)
{
int n = x + y * 57;
n = (n << 13) ^ n;
return (double)(1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
}
And here is my hacky rendering code, for debug purposes:
for (int y = 0; y < 512; y++)
{
for (int x = 0; x < 512; x++)
{
double val = GeneratePerlin2D((x), (y));
Rectangle rect(x, y, 1.0, 1.0);
SDL_Color color{(Uint8)(val * 128 + 128), (Uint8)(val * 128 + 128), (Uint8)(val * 128 + 128), 255};
m_renderer->RenderFillRect(rect, color);
}
}
I have tried both linear and cosine interpolation, double and triple checked my type casting and variables, and I don't know what is going on here. What am I missing?
I based the implementation off of this article.
I was able to fix the problem. I had to change two things. One, I had to divide the sample x,y by the texture size, and then I also had to cast the x and y to a double.
for (int y = 0; y < 512; y++)
{
for (int x = 0; x < 512; x++)
{
double val = GeneratePerlin2D((double)(x) / 512.0 * 10, (double)(y) / 512.0 * 10);
Rectangle rect(x - 256, y - 256, 1.0, 1.0);
SDL_Color color{(Uint8)(val * 128 + 128), (Uint8)(val * 128 + 128), (Uint8)(val * 128 + 128), 255};
m_renderer->RenderFillRect(rect, color);
}
}