int permute4(int *b) { int i; permute3(b); for(i=2;i<5;i++) { swap(1,i); permute3(b); swap(1,i); } }
float duWater::snoise(float v[2]) { float C_x = 0.211324865405187; // (3.0-sqrt(3.0))/6.0 float C_y = 0.366025403784439; // 0.5*(sqrt(3.0)-1.0) float C_z = -0.577350269189626; // -1.0 + 2.0 * C.x float C_w = 0.024390243902439; // 1.0 / 41.0 // First corner float v_dot_Cyy = v[0] * C_y + v[1] * C_y; int i_x = floor(v[0] + v_dot_Cyy); int i_y = floor(v[1] + v_dot_Cyy); float i_dot_Cxx = i_x * C_x + i_y * C_x; float x0_x = v[0] - i_x + i_dot_Cxx; float x0_y = v[1] - i_y + i_dot_Cxx; // Other corners int i1_x = x0_x > x0_y ? 1 : 0; int i1_y = 1 - i1_x; float x12_x = x0_x + C_x - i1_x; float x12_y = x0_y + C_x - i1_y; float x12_z = x0_x + C_z; float x12_w = x0_y + C_z; // Permutations i_x %= 289; // Avoid truncation effects in permutation i_y %= 289; int p_x = permute3( permute3(i_y) + i_x); int p_y = permute3( permute3(i_y + i1_y) + i_x + i1_x); int p_z = permute3( permute3(i_y + 1) + i_x + 1); float m_x = fmax(0.5f - (x0_x * x0_x + x0_y * x0_y ), 0.f); float m_y = fmax(0.5f - (x12_x * x12_x + x12_y * x12_y), 0.f); float m_z = fmax(0.5f - (x12_z * x12_z + x12_w * x12_w), 0.f); m_x *= m_x * m_x * m_x; m_y *= m_y * m_y * m_y; m_z *= m_z * m_z * m_z; // Gradients: 41 points uniformly over a line, mapped onto a diamond. // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287) double int_part; float x_x = 2.f * modf(double(p_x * C_w), &int_part) - 1.f; float x_y = 2.f * modf(double(p_y * C_w), &int_part) - 1.f; float x_z = 2.f * modf(double(p_z * C_w), &int_part) - 1.f; float h_x = fabs(x_x) - 0.5f; float h_y = fabs(x_y) - 0.5f; float h_z = fabs(x_z) - 0.5f; float ox_x = floor(x_x + 0.5f); float ox_y = floor(x_y + 0.5f); float ox_z = floor(x_z + 0.5f); float a0_x = x_x - ox_x; float a0_y = x_y - ox_y; float a0_z = x_z - ox_z; // Normalise gradients implicitly by scaling m // Approximation of: m *= inversesqrt( a0*a0 + h*h ); m_x *= 1.79284291400159 - 0.85373472095314 * (a0_x * a0_x + h_x * h_x); m_y *= 1.79284291400159 - 0.85373472095314 * (a0_y * a0_y + h_y * h_y); m_z *= 1.79284291400159 - 0.85373472095314 * (a0_z * a0_z + h_z * h_z); // Compute final noise value at P float g_x = a0_x * x0_x + h_x * x0_y; float g_y = a0_y * x12_x + h_y * x12_y; float g_z = a0_z * x12_z + h_z * x12_w; float m_dot_g = m_x * g_x + m_y * g_y + m_z * g_z; return 130.f * m_dot_g; }