首页 > 解决方案 > 我正在尝试在 C++ 上移植 1D perlin 噪声教程

问题描述

我正在尝试使用 SFMl lib 在 C++ 上移植一维 perlin 噪声教程:(javascript 中的教程链接)https://codepen.io/Tobsta/post/procedural-generation-part-1-1d-perlin-noise

但是这不起作用,我没有收到任何错误,但这就是我得到的:https ://i.imgur.com/2tAPhsH.png 。基本上是一条直线

这就是我应该得到的:https ://i.imgur.com/GPnfsuK.png

这是来自上述链接的移植代码:

TerrainBorder 构造函数:

TerrainBorder::TerrainBorder(sf::RenderWindow &window) {

    M = 4294967296;
    A = 1664525;
    C = 1;

    std::random_device rd;
    std::mt19937 rng(rd());
    std::uniform_int_distribution<int> dist(0, M);

    Z = floor(dist(rng) * M);
    x = 0;
    y = window.getSize().y / 2.0f;
    amp = 100;
    wl = 100;
    fq = 1.0f / wl;
    a = rand();
    b = rand();

    ar = sf::VertexArray(sf::Points);
}

功能:

double TerrainBorder::rand()
{
    Z =  (A * Z + C) % M;
    return Z / M - 0.5;
}

double TerrainBorder::interpolate(double pa, double pb , double px) {
    double ft = px * PI,
           f = (1 - cos(ft)) * 0.5;
    return pa * (1 - f) + pb * f;
}

void TerrainBorder::drawPoints(sf::RenderWindow &window) {
    while (x < window.getSize().x) {

        if (static_cast<int> (x) % wl == 0) {
            a = b;
            b = rand();
            y = window.getSize().y / 2 + a * amp;
        } else {
            y = window.getSize().y / 2 + interpolate(a, b, static_cast<int> (x) 
            % wl / wl) * amp;
        }
        ar.append(sf::Vertex(sf::Vector2f(x, y)));
        x += 1;
    }
}

然后我正在绘制sf::VectorArray(其中包含sf::Vertex游戏循环中的所有

标签: javascriptc++sfmlperlin-noise

解决方案


无论如何,我解决了我的问题 ty :) 我必须处理类型问题:p

我发现:

double c = x % 100 / 100;
std::cout << c << std::endl; // 0

!=

double c = x % 100;
std::cout << c / 100 << std::endl; // Some numbers depending on x

如果它可以在未来帮助任何人:)


推荐阅读