尝试使用 perlinnoise 生成地形

问题描述

我正在尝试使用柏林噪声生成程序地形。之前,我只是创建了一个 1000 * 1000 顶点地形,所以我只有一个简单的函数,可以用噪声值填充高度图。

但是现在我试图生成一个地形,当相机四处移动时会生成块,我认为这种方法不合适,因为每个块都将使用它自己的随机高度图生成,并且块看起来彼此不一致。相反,我使用这个头文件生成只需要 x 和 y 的噪声,但是我给它的任何值似乎都没有生成一致的值,我不知道为什么

class Noise {    
public:
int seed;

Noise(int seed) {
    this->seed = seed;
}

float noise(int x,int y) {
    int n = x + y * 57 + seed;
    n = ( n << 13 ) ^ n;
    float rand = 1 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f;
    return rand;
}

float noise(int x,int y,int z) {
    int n = x + y + z * 57 + seed;
    n = ( n << 13 ) ^ n;
    float rand = 1 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f;
    return rand;
}

float smoothNoise(int x,int y) {
    float corners = (noise(x+1,y+1) + noise(x+1,y-1) + noise(x-1,y+1) + noise(x-1,y-1)) / 16.0f;
    float sides = (noise(x,y+1) + noise(x,y) + noise(x+1,y)) / 8.0f;
    float center = noise(x,y) / 4.0f;
    
    return corners + sides + center;
}

float interpolatednoise(float x,float y) {
    int integerX = (int)x;
    float fractionalX = x - integerX;
    
    int integerY = (int)y;
    float fractionalY = y - integerY;
    
    float v1 = smoothNoise(integerX,integerY);
    float v2 = smoothNoise(integerX + 1,integerY);
    float v3 = smoothNoise(integerX,integerY + 1);
    float v4 = smoothNoise(integerX + 1,integerY + 1);
    
    float i1 = interpolateCosine(v1,v2,fractionalX);
    float i2 = interpolateCosine(v3,v4,fractionalX);
    
    return interpolateCosine(i1,i2,fractionalY);
    
}

// x must be in the range [0,1]
float interpolateLinear(float a,float b,float x) {
    return a * (1 - x) + b * x;
}

float interpolateCosine(float a,float x) {
    float ft = x * (float)glm::pi<float>();
    float f = (1 - ((float)glm::pi<float>())) * 0.5f;
    return a * (1 - f) + b * f;
} 

};`

我这样调用 interpolatednoise():

for(int y=x; x < config->width;x++)
{
   for(int y = 0; y < config->height;y++)
   { 
       map[x * config->height + y] = noise.interpolatednoise((config->width * gridPosX + x) * 0.1f,(config->height * gridPosY + y) * 0.1f)/2.0f + 0.5f; // convert from -1:1 to 0:1
   }  
} 

这就是使用这个地形的样子


:

但我希望地形平坦。我传递给函数的参数除以某个值。我将此值设置得越大,地形越随机,框越小,我设置得越小,我只会得到更大的框,但地形看起来更一致。

解决方法

我不太确定您是如何调用 Noise 构造函数的,但我看到的一个问题是在构造函数中。它应该是 this->seed = seed;,否则您将参数种子分配给它自身的值。 Noise 的种子变量只是一个随机数,我觉得这会引起问题。同样,不确定您如何调用 Noise 构造函数,但这是我最好的猜测。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...