Perlin噪声2D:将静态变为云 [英] Perlin Noise 2D: turning static into clouds

查看:155
本文介绍了Perlin噪声2D:将静态变为云的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绕着Perlin的噪音。

I am trying to wrap my head around Perlin noise.

以下文章帮助了我,我一直在尝试重新创建它提供的云类型图像。

The following article has helped and I have been trying to recreate the cloud type images that it provides.

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

我的噪音代码如下:

#include "terrain_generator.hpp"

using namespace std;


#define PI 3.1415927;

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

float cosine_interpolate(float a, float b, float x)
{
    float ft = x * PI;
    float f = (1 - cos(ft)) * 0.5;
    float result =  a*(1-f) + b*f;
    return result;
}

float smooth_noise_2D(float x, float y)
{  
    float corners = ( noise(x-1, y-1)+noise(x+1, y-1)+noise(x-1, y+1)+noise(x+1, y+1) ) / 16;
    float sides   = ( noise(x-1, y)  +noise(x+1, y)  +noise(x, y-1)  +noise(x, y+1) ) /  8;
    float center  =  noise(x, y) / 4;

    return corners + sides + center;
}

float interpolated_noise(float x, float y)
{
    int x_whole = (int) x;
    float x_frac = x - x_whole;

    int y_whole = (int) y;
    float y_frac = y - y_whole;

    float v1 = smooth_noise_2D(x_whole, y_whole); 
    float v2 = smooth_noise_2D(x_whole, y_whole+1); 
    float v3 = smooth_noise_2D(x_whole+1, y_whole); 
    float v4 = smooth_noise_2D(x_whole+1, y_whole+1); 

    float i1 = cosine_interpolate(v1,v3,x_frac);
    float i2 = cosine_interpolate(v2,v4,x_frac);

    return cosine_interpolate(i1, i2, y_frac);
}


float perlin_noise_2D(float x, float y)
{
    int octaves=5;
    float persistence=0.5;
    float total = 0;

    for(int i=0; i<octaves-1; i++)
    {
        float frequency = pow(2,i);
        float amplitude = pow(persistence,i);
        total = total + interpolated_noise(x * frequency, y * frequency) * amplitude;
    }
    return total;
}






,我试图让他在文章中描述的云。


To actually implement the algorithm, I am trying to make the clouds he depicted in the article.

我使用openGL,我正在创建自己的纹理并粘贴到覆盖屏幕的四边形。这是不相干的。在下面的代码中,只需知道set像素函数正确工作,其参数(x,y,red,green,blue)

I am using openGL and I am making my own texture and pasting it onto a quad that covers the screen. That is irrelevant though. In the code below, just know that the set pixel function works correctly and that its parameters are (x, y, red, green, blue).

这实质上是我的绘制循环:

This is essentially my draw loop:

for(int y=0; y<texture_height; y++)
{
    for(int x=0; x<texture_width; x++)
    {
        seed2+=1;
        float Val=perlin_noise_2D(x,y);
        Val = Val/2.0;
        Val = (Val + 1.0) / 2.0; 
        setPixel(x,y,Val,Val,Val);
    }
}

我得到的是以下内容:

如何操作我的算法以实现我正在寻找的效果?改变八度音阶的持续性或数量似乎并没有做太多。

How can I manipulate my algorithm to achieve the effect I am looking for? changing the persistence or number of octaves doesn't seem to do much at all.

推荐答案

由于结果看起来几乎像白噪声,您的样本在perlin噪声中可能相距太远。尝试使用比像素坐标小的像素坐标来评估噪声。

As your result looks almost like white noise, your samples are probably too far apart within the perlin noise. Try using something smaller than the pixel coordinates to evaluate the noise at.

类似这样的东西:

code> perlin_noise_2D((float)x / texture_width,(float)y / texture_height);

perlin_noise_2D((float)x/texture_width,(float)y/texture_height);

这篇关于Perlin噪声2D:将静态变为云的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆