为什么我的perlin噪音如此......吵?

时间:2016-02-11 06:11:30

标签: c++ sdl-2 perlin-noise procedural-generation procedural

我的perlin噪音实现有问题。出于某种原因,它似乎太吵了。我不知道如何将它用于游戏地形。这是一个截图:

Perlin Noise Problem

这是我的代码:

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);
}

这是我的hacky渲染代码,用于调试目的:

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);
    }
}

我尝试过线性和余弦插值,双重和三重检查我的类型转换和变量,我不知道这里发生了什么。我错过了什么?

我的实施基于this article

2 个答案:

答案 0 :(得分:1)

我能够解决问题。我不得不改变两件事。一,我不得不将样本x,y除以纹理大小,然后我还必须将x和y转换为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);
    }
}

答案 1 :(得分:0)

对于初学者来说,你已经将你的八度音阶设置为1,所以不是将一系列噪音函数相加在一起,而是只有一个噪音函数。