gpt4 book ai didi

c++ - 为什么我的 perlin 噪音这么..嘈杂?

转载 作者:行者123 更新时间:2023-11-30 05:31:59 24 4
gpt4 key购买 nike

<分区>

我在执行 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 .

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com