【问题标题】:Perlin noise, blocky,c++Perlin 噪声,块状,C++
【发布时间】:2014-05-21 09:34:45
【问题描述】:

好的,首先,我正在尝试实现 Perlin 噪声算法,但我设法实现了一些奇怪的东西,但我找不到解决方案。我正在使用 matlab 来可视化我已经检查过这个问题的结果:

"Blocky" Perlin noise

我是从这个网站做的:

http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

还有一个我现在找不到的网站,但我会尽快更新。

所以这里有一些关于这个问题的图片:

如果增加缩放,这就是问题 http://i.stack.imgur.com/KkD7u.png

这里是 .cpp-s:

//perlin.cpp
     #include "Perlin_H.h"
    #include <stdlib.h>
    #include <math.h>
    #include <iostream>
    #include <random>
    using namespace std;

double Perlin::interp1(double a, double b, double x) {

    double ft = x * 3.1415927;
    double f = (1.0-cos(ft)) * 0.5;
    //return (b-x > b-1/2) ? b-x : a+x;
    return a * (1.0-f) + b * f;
}

double Perlin::smoothNoise(double x,double y) {
    double corners =  ( rand2(x-1, y-1)+rand2(x+1, y-1)+rand2(x-1, y+1)+rand2(x+1, y+1) ) / 16;
    double sides = ( rand2(x-1, y)  +rand2(x+1, y)  +rand2(x, y-1)  +rand2(x, y+1) ) /  8;
    double center = rand2(x,y)/4;

    return corners + sides +center;
}



double Perlin::lininterp1(double a,double b, double x) {
    return a*(1-x) + b * x;
}
double Perlin::rand2(double x, double y) {

    int n = (int)x + (int)y*57;
    //n=pow((n<<13),n);
    n=(n<<13)^n;
    return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);

}

double Perlin::noise(double x, double y) {
    double floorX = (double)floor(x);
    double floorY = (double)floor(y);

    double s,t,u,v;

    s = smoothNoise(floorX,floorY);
    t = smoothNoise(floorX+1,floorY);
    u = smoothNoise(floorY,floorY+1);
    v = smoothNoise(floorX+1,floorY+1);

    double int1 = interp1(s,t,x-floorX);
    double int2 = interp1(u,v,x-floorX);

    return interp1(int1,int2,y-floorY);
}

//main.cpp

#include "Perlin_H.h"
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>;
using namespace std;
int main() {
    const int h=64,w=64,octaves=2;
    double p=1/1;
    double zoom = 30;
    Perlin perlin;

    double map[h][w];
    ofstream output;
    output.open("map.txt");
    for(int i = 0; i < h ; i++) {   
        for(int j = 0; j < w ; j++) {
            map[i][j] = 0;
        }
    }
    double freq = 2;




    for(int i = 0; i < h ; i++) {


        for(int j = 0; j < w ; j++) {

            double getnoise = 0;
            for(int a=0; a < octaves; a++) {


                double freq = pow(2,a);

                double amp = pow(p,a);
                getnoise = perlin.noise((((double)i)*freq)/zoom-(a*10),
                    ((((double)j))*freq)/zoom+(a*10))*amp;
                int color = (int)((getnoise * 128.0) + 128.0);
                if(color > 255) color = 255;
                if(color < 0) color = 0;

                map[i][j] = color;


            }
        output  << map[i][j] << "\t";
        }
        output << "\n";

    }


    output.close();



    system("PAUSE");
    return 0;
}

【问题讨论】:

标签: c++ noise perlin-noise


【解决方案1】:

这是一个错字!

s = smoothNoise(floorX,floorY);
t = smoothNoise(floorX+1,floorY);
u = smoothNoise(floorY,floorY+1);
v = smoothNoise(floorX+1,floorY+1);

尝试: u = smoothNoise(floorX, floorY +1)

这解释了为什么对角线没有块状外观(其中 x=y),以及为什么许多常见的特征形状以镜像和倾斜的方式巧妙地偏离。 由于 rand2(floor(y), floor(y)+1) != rand2(floor(x), floor(y+1)) 通常很明显,因此会导致单元不连续。

【讨论】:

  • 哦,非常感谢!这是不使用 ctrl + c ctrl+v 的完美示例:D
【解决方案2】:

在您的实现中没有发现数学错误,我怀疑这是一个数字格式问题。

当从不同侧获取时网格点值实际上不相同时创建此类块模式 - 当rand2(floor(n) +1 ,y) != rand2(floor(n+1) ,y)

要修复它,请将 floorX 声明为 intlong,并将其传递给 smoothNoise() 和 rand2()。

这可能是由于整数值 floorXfloorX + 1 的表示形式中的浮点错误而发生的。大小为 ulp 或更小的 epsilon 可以有任何一个符号。加法 [floor(n) + 1] 和直接取地板 [floor(n+1)] 的结果由不同的代码绑定,因此不需要共享选择哪边出错的模式。当结果在不同方面出错时,int 类型的强制转换将 0.99999999999 和 0.0000000001 平等地剥离,将数学上等价的数字视为不同。

【讨论】:

  • 不幸的是,它似乎不起作用,或者我又做错了什么。我将double floorX = (double)floor(x); 更改为int floorX = floor(x); 并没有任何更改,然后我也将其更改为floorY,仍然没有结果,然后我将s = smoothNoise(floorX+1,floorY); etc. 更改为s = smoothNoise(floor(x+1),floorY); 以及以上所有内容的组合,但都没有工作。也许随机生成器也有问题。
  • double Perlin::rand2(int x, int y) 起到了伪随机散列函数的作用。可能是别的东西。
猜你喜欢
  • 2014-02-15
  • 1970-01-01
  • 2014-10-06
  • 2020-06-06
  • 2011-09-20
  • 2021-06-30
  • 2013-07-23
  • 2020-06-22
  • 2011-03-19
相关资源
最近更新 更多