插值价值噪声中的人工制品

时间:2013-07-10 03:02:47

标签: c++ graphics interpolation noise perlin-noise

我正在尝试创建基本值噪声函数。我已达到输出它的程度,但在输出中会出现意想不到的假象,如对角线不连续的线条和模糊。我似乎无法找到导致它的原因。有人可以看看它,看看我是否在某个地方出错了。

首先,这里有三个图像,它们在每个图像上放大倍数。

http://oi40.tinypic.com/wl2deq.jpg

//data members
float m_amplitude, m_frequency;
int m_period;  //controls the tile size of the noise
vector<vector<float> m_points; //2D array to store the lattice

//The constructor generates the 2D square lattice and populates it.
Noise2D(int period, float frequency, float amplitude)
{
    //initialize the lattice to the appropriate NxN size
    m_points.resize(m_period);
      for (int i = 0; i < m_period; ++i)
        m_points[i].resize(m_period);

    //populates the lattice with values between 0 and 1
    int seed = 209;
            srand(seed);
    for(int i = 0; i < m_period; i++)
    {
        for(int j = 0; j < m_period; j++)
        {
            m_points[i][j] = abs(rand()/(float)RAND_MAX);
        }
    }
}


//Evaluates a position
float Evaluate(float x, float y)
{
    x *= m_frequency;
    y *= m_frequency;

    //Gets the integer values from each component
    int xFloor = (int) x;
    int yFloor = (int) y;

    //Gets the decimal data in the range of [0:1] for each of the components for interpolation
    float tx = x - xFloor;
    float ty = y - yFloor;

    //Finds the appropriate boundary lattice array indices using the modulus technique to ensure periodic noise.
    int xPeriodLower = xFloor % m_period;
    int xPeriodUpper;
    if(xPeriodLower == m_period - 1) 
        xPeriodUpper = 0; 
    else    
        xPeriodUpper = xPeriodLower + 1;

    int yPeriodLower = yFloor % m_period;
    int yPeriodUpper;
    if(yPeriodLower == m_period - 1) 
        yPeriodUpper = 0; 
    else    
        yPeriodUpper = yPeriodLower + 1;

    //The four random values at each boundary. The naming convention for these follow a single 2d coord system 00 for bottom left, 11 for top right
    const float& random00 = m_points[xPeriodLower][yPeriodLower];
    const float& random10 = m_points[xPeriodUpper][yPeriodLower];
    const float& random01 = m_points[xPeriodLower][yPeriodUpper];
    const float& random11 = m_points[xPeriodUpper][yPeriodUpper];

    //Remap the weighting of each t dimension here if you wish to use an s-curve profile.
    float remappedTx = tx;
    float remappedTy = ty;

    return MyMath::Bilinear<float>(remappedTx, remappedTy, random00, random10, random01, random11) * m_amplitude;
}

以下是它依赖的两个插值函数。

template <class T1>
static T1 Bilinear(const T1 &tx, const T1 &ty, const T1 &p00, const T1 &p10, const T1 &p01, const T1 &p11)
{
    return Lerp(    Lerp(p00,p10,tx),
            Lerp(p01,p11,tx),
            ty);
}

template <class T1> //linear interpolation aka Mix
static T1 Lerp(const T1 &a, const T1 &b, const T1 &t)
{
    return a * (1 - t) + b * t;
}

1 个答案:

答案 0 :(得分:1)

一些伪影是线性插值的结果。使用更高阶的插值方法会有所帮助,但它只能解决部分问题。粗略地说,信号的急剧转变会导致伪影。

通过以相等的间隔分布起始噪声值(I.E.您正在插值的值)产生的附加伪像 - 在这种情况下是网格。最高的&amp;最低值只会出现在这些网格点上 - 至少在使用线性插值时。粗略地说,信号中的模式可能会导致伪影。我知道解决这部分问题的两种可能的方法是使用非线性插值和/或随机微调起始噪声值的坐标以打破它们的规律性。

Libnoise对generating coherent noise的解释涵盖了这些问题&amp;更深入的解决方案和一些很好的插图。如果您需要了解它如何处理这些问题,您还可以查看源代码。正如richard-tingle已经提到的那样,simplex noise旨在纠正Perlin噪声中固有的伪像问题;让你的头脑变得有点困难,但它是一种坚固的技术。