c - C 语言的二维柏林噪声

标签 c algorithm

我关注了this tutorial .

当我在代码(光线追踪)中实现它并将其应用到球体上时,我得到一个单色球体,上面有一条较暗的像素条纹。当我更改随机浮点生成器时,我得到了基本的线性噪声,这不是我的目标。你能解释一下我错过了什么吗?

这是我的代码:

#include    <stdlib.h>
#include    <math.h>

float       noise(int x, int y)
{
  int       n;

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

float       interpolate(float a, float b, float x)
{
  float     pi_mod;
  float     f_unk;

  pi_mod = x * 3.1415927;
  f_unk = (1 - cos(pi_mod)) * 0.5;
  return (a * (1 - f_unk) + b * x);
}

float       smooth_noise(int x, int y)
{
  float     corners;
  float     center;
  float     sides;

  corners = (noise(x - 1, y - 1) + noise(x + 1, y - 1) +
         noise(x - 1, x + 1) + noise(x + 1, y + 1)) / 16;
  sides = (noise(x - 1, y) + noise(x + 1, y) + noise(x, y - 1) +
       noise(x, y + 1)) / 8;
  center = noise(x, y) / 4;
  return (corners + sides + center);
}

float       noise_handler(float x, float y)
{
  int       int_val[2];
  float     frac_val[2];
  float     value[4];
  float     res[2];

  int_val[0] = (int)x;
  int_val[1] = (int)y;
  frac_val[0] = x - int_val[0];
  frac_val[1] = y - int_val[1];
  value[0] = smooth_noise(int_val[0], int_val[1]);
  value[1] = smooth_noise(int_val[0] + 1, int_val[1]);
  value[2] = smooth_noise(int_val[0], int_val[1] + 1);
  value[3] = smooth_noise(int_val[0] + 1, int_val[1] + 1);
  res[0] = interpolate(value[0], value[1], frac_val[0]);
  res[1] = interpolate(value[2], value[3], frac_val[0]);
  return (interpolate(res[0], res[1], frac_val[1]));
}

float       perlin_two(float x, float y)
{
  float     total;
  float     per;
  float     amp;
  int       hz;
  int       i;
  int       octave;

  total = 0.0;
  per = 0.5;
  octave = 10;
  i = 0;
  while (i < octave)
    {
      hz = pow(2, i);
      amp = pow(per, (float)i);
      total += noise_handler(x * (float)hz, y * (float)hz) * amp;
      i += 1;
    }
  return (total);
}

编辑:我在噪声函数中发现了一个错误(我认为 XOR 操作数就像幂函数一样......现在我得到一个条形码,就好像 y 参数在运算中被忽略一样......

最佳答案

最近必须用 C 语言实现这个,这篇文章帮助我开始了。如前所述,对噪声函数进行了一项修复。

float noise(int x, int y) {
    int n;

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

最重要的是,在高度图中实现柏林噪声时,分形布朗运动非常重要。我没有遵循 Hugo Elias 的伪代码,而是使用了这个 Google Code Snippet .

float perlin_two(float x, float y, float gain, int octaves, int hgrid) {
    int i;
    float total = 0.0f;
    float frequency = 1.0f/(float)hgrid;
    float amplitude = gain;
    float lacunarity = 2.0;

    for (i = 0; i < octaves; ++i)
    {
        total += noise_handler((float)x * frequency, (float)y * frequency) * amplitude;         
        frequency *= lacunarity;
        amplitude *= gain;
    } 

    return (total);
}

在我这样做之前,我在统一“剥离”或只是完成随机的高度图方面遇到了同样的问题。

关于c - C 语言的二维柏林噪声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16569660/

相关文章:

algorithm - 在 1 GB 内存中排序 10GB 数据。我将如何做?

algorithm - 基数排序解释

c - 链表段错误

c - 如何创建一个代表c数组的 `ffi_type`?

使用非阻塞键盘输入退出创建无限循环

ios - 如果 sqlite3_prepare_v2() 返回一个错误代码,我该如何获取错误代码?

algorithm - 关于空间复杂度的一般混淆

algorithm - 查找具有加权顶点的多边形的质心

javascript - 解释算法背后的数学原理

c - sendto 需要很多时间