///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
/// Copyright (c) 2005 – 2015 G-Truc Creation (www.g-truc.net)
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the “Software”), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// Restrictions:
/// By making use of the Software for military purposes, you choose to make
/// a Bunny unhappy.
///
/// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref gtc_noise
/// @file glm/gtc/noise.inl
/// @date 2011-04-21 / 2012-04-07
/// @author Christophe Riccio
///////////////////////////////////////////////////////////////////////////////////
// Based on the work of Stefan Gustavson and Ashima Arts on “webgl-noise”:
// https://github.com/ashima/webgl-noise
// Following Stefan Gustavson’s paper “Simplex noise demystified”:
// http://www.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
///////////////////////////////////////////////////////////////////////////////////
namespace glm{
namespace gtc
{
template
GLM_FUNC_QUALIFIER tvec4
{
tvec3
T pW = static_cast
tvec4
pXYZ = pXYZ + (tvec3
return tvec4
}
}//namespace gtc
// Classic Perlin noise
template
GLM_FUNC_QUALIFIER T perlin(tvec2
{
tvec4
tvec4
Pi = mod(Pi, tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
gx = gx – tx;
tvec2
tvec2
tvec2
tvec2
tvec4
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
T n00 = dot(g00, tvec2
T n10 = dot(g10, tvec2
T n01 = dot(g01, tvec2
T n11 = dot(g11, tvec2
tvec2
tvec2
T n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return T(2.3) * n_xy;
}
// Classic Perlin noise
template
GLM_FUNC_QUALIFIER T perlin(tvec3
{
tvec3
tvec3
Pi0 = detail::mod289(Pi0);
Pi1 = detail::mod289(Pi1);
tvec3
tvec3
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
gx0 = fract(gx0);
tvec4
tvec4
gx0 -= sz0 * (step(T(0), gx0) – T(0.5));
gy0 -= sz0 * (step(T(0), gy0) – T(0.5));
tvec4
tvec4
gx1 = fract(gx1);
tvec4
tvec4
gx1 -= sz1 * (step(T(0), gx1) – T(0.5));
gy1 -= sz1 * (step(T(0), gy1) – T(0.5));
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec4
g000 *= norm0.x;
g010 *= norm0.y;
g100 *= norm0.z;
g110 *= norm0.w;
tvec4
g001 *= norm1.x;
g011 *= norm1.y;
g101 *= norm1.z;
g111 *= norm1.w;
T n000 = dot(g000, Pf0);
T n100 = dot(g100, tvec3
T n010 = dot(g010, tvec3
T n110 = dot(g110, tvec3
T n001 = dot(g001, tvec3
T n101 = dot(g101, tvec3
T n011 = dot(g011, tvec3
T n111 = dot(g111, Pf1);
tvec3
tvec4
tvec2
T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
return T(2.2) * n_xyz;
}
/*
// Classic Perlin noise
template
GLM_FUNC_QUALIFIER T perlin(tvec3
{
tvec3
tvec3
Pi0 = mod(Pi0, T(289));
Pi1 = mod(Pi1, T(289));
tvec3
tvec3
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
gx0 = fract(gx0);
tvec4
tvec4
gx0 -= sz0 * (step(0.0, gx0) – T(0.5));
gy0 -= sz0 * (step(0.0, gy0) – T(0.5));
tvec4
tvec4
gx1 = fract(gx1);
tvec4
tvec4
gx1 -= sz1 * (step(T(0), gx1) – T(0.5));
gy1 -= sz1 * (step(T(0), gy1) – T(0.5));
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec4
g000 *= norm0.x;
g010 *= norm0.y;
g100 *= norm0.z;
g110 *= norm0.w;
tvec4
g001 *= norm1.x;
g011 *= norm1.y;
g101 *= norm1.z;
g111 *= norm1.w;
T n000 = dot(g000, Pf0);
T n100 = dot(g100, tvec3
T n010 = dot(g010, tvec3
T n110 = dot(g110, tvec3
T n001 = dot(g001, tvec3
T n101 = dot(g101, tvec3
T n011 = dot(g011, tvec3
T n111 = dot(g111, Pf1);
tvec3
tvec4
tvec2
tvec2
tvec2
T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
return T(2.2) * n_xyz;
}
*/
// Classic Perlin noise
template
GLM_FUNC_QUALIFIER T perlin(tvec4
{
tvec4
tvec4
Pi0 = mod(Pi0, tvec4
Pi1 = mod(Pi1, tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
gx00 = fract(gx00) – T(0.5);
gy00 = fract(gy00) – T(0.5);
gz00 = fract(gz00) – T(0.5);
tvec4
tvec4
gx00 -= sw00 * (step(T(0), gx00) – T(0.5));
gy00 -= sw00 * (step(T(0), gy00) – T(0.5));
tvec4
tvec4
tvec4
gx01 = fract(gx01) – T(0.5);
gy01 = fract(gy01) – T(0.5);
gz01 = fract(gz01) – T(0.5);
tvec4
tvec4
gx01 -= sw01 * (step(T(0), gx01) – T(0.5));
gy01 -= sw01 * (step(T(0), gy01) – T(0.5));
tvec4
tvec4
tvec4
gx10 = fract(gx10) – T(0.5);
gy10 = fract(gy10) – T(0.5);
gz10 = fract(gz10) – T(0.5);
tvec4
tvec4
gx10 -= sw10 * (step(T(0), gx10) – T(0.5));
gy10 -= sw10 * (step(T(0), gy10) – T(0.5));
tvec4
tvec4
tvec4
gx11 = fract(gx11) – T(0.5);
gy11 = fract(gy11) – T(0.5);
gz11 = fract(gz11) – T(0.5);
tvec4
tvec4
gx11 -= sw11 * (step(T(0), gx11) – T(0.5));
gy11 -= sw11 * (step(T(0), gy11) – T(0.5));
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
g0000 *= norm00.x;
g0100 *= norm00.y;
g1000 *= norm00.z;
g1100 *= norm00.w;
tvec4
g0001 *= norm01.x;
g0101 *= norm01.y;
g1001 *= norm01.z;
g1101 *= norm01.w;
tvec4
g0010 *= norm10.x;
g0110 *= norm10.y;
g1010 *= norm10.z;
g1110 *= norm10.w;
tvec4
g0011 *= norm11.x;
g0111 *= norm11.y;
g1011 *= norm11.z;
g1111 *= norm11.w;
T n0000 = dot(g0000, Pf0);
T n1000 = dot(g1000, tvec4
T n0100 = dot(g0100, tvec4
T n1100 = dot(g1100, tvec4
T n0010 = dot(g0010, tvec4
T n1010 = dot(g1010, tvec4
T n0110 = dot(g0110, tvec4
T n1110 = dot(g1110, tvec4
T n0001 = dot(g0001, tvec4
T n1001 = dot(g1001, tvec4
T n0101 = dot(g0101, tvec4
T n1101 = dot(g1101, tvec4
T n0011 = dot(g0011, tvec4
T n1011 = dot(g1011, tvec4
T n0111 = dot(g0111, tvec4
T n1111 = dot(g1111, Pf1);
tvec4
tvec4
tvec4
tvec4
tvec2
T n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x);
return T(2.2) * n_xyzw;
}
// Classic Perlin noise, periodic variant
template
GLM_FUNC_QUALIFIER T perlin(tvec2
{
tvec4
tvec4
Pi = mod(Pi, tvec4
Pi = mod(Pi, tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
gx = gx – tx;
tvec2
tvec2
tvec2
tvec2
tvec4
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
T n00 = dot(g00, tvec2
T n10 = dot(g10, tvec2
T n01 = dot(g01, tvec2
T n11 = dot(g11, tvec2
tvec2
tvec2
T n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return T(2.3) * n_xy;
}
// Classic Perlin noise, periodic variant
template
GLM_FUNC_QUALIFIER T perlin(tvec3
{
tvec3
tvec3
Pi0 = mod(Pi0, tvec3
Pi1 = mod(Pi1, tvec3
tvec3
tvec3
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
gx0 = fract(gx0);
tvec4
tvec4
gx0 -= sz0 * (step(T(0), gx0) – T(0.5));
gy0 -= sz0 * (step(T(0), gy0) – T(0.5));
tvec4
tvec4
gx1 = fract(gx1);
tvec4
tvec4
gx1 -= sz1 * (step(T(0), gx1) – T(0.5));
gy1 -= sz1 * (step(T(0), gy1) – T(0.5));
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec3
tvec4
g000 *= norm0.x;
g010 *= norm0.y;
g100 *= norm0.z;
g110 *= norm0.w;
tvec4
g001 *= norm1.x;
g011 *= norm1.y;
g101 *= norm1.z;
g111 *= norm1.w;
T n000 = dot(g000, Pf0);
T n100 = dot(g100, tvec3
T n010 = dot(g010, tvec3
T n110 = dot(g110, tvec3
T n001 = dot(g001, tvec3
T n101 = dot(g101, tvec3
T n011 = dot(g011, tvec3
T n111 = dot(g111, Pf1);
tvec3
tvec4
tvec2
T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
return T(2.2) * n_xyz;
}
// Classic Perlin noise, periodic version
template
GLM_FUNC_QUALIFIER T perlin(tvec4
{
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
gx00 = fract(gx00) – T(0.5);
gy00 = fract(gy00) – T(0.5);
gz00 = fract(gz00) – T(0.5);
tvec4
tvec4
gx00 -= sw00 * (step(T(0), gx00) – T(0.5));
gy00 -= sw00 * (step(T(0), gy00) – T(0.5));
tvec4
tvec4
tvec4
gx01 = fract(gx01) – T(0.5);
gy01 = fract(gy01) – T(0.5);
gz01 = fract(gz01) – T(0.5);
tvec4
tvec4
gx01 -= sw01 * (step(T(0), gx01) – T(0.5));
gy01 -= sw01 * (step(T(0), gy01) – T(0.5));
tvec4
tvec4
tvec4
gx10 = fract(gx10) – T(0.5);
gy10 = fract(gy10) – T(0.5);
gz10 = fract(gz10) – T(0.5);
tvec4
tvec4
gx10 -= sw10 * (step(T(0), gx10) – T(0.5));
gy10 -= sw10 * (step(T(0), gy10) – T(0.5));
tvec4
tvec4
tvec4
gx11 = fract(gx11) – T(0.5);
gy11 = fract(gy11) – T(0.5);
gz11 = fract(gz11) – T(0.5);
tvec4
tvec4
gx11 -= sw11 * (step(T(0), gx11) – T(0.5));
gy11 -= sw11 * (step(T(0), gy11) – T(0.5));
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
g0000 *= norm00.x;
g0100 *= norm00.y;
g1000 *= norm00.z;
g1100 *= norm00.w;
tvec4
g0001 *= norm01.x;
g0101 *= norm01.y;
g1001 *= norm01.z;
g1101 *= norm01.w;
tvec4
g0010 *= norm10.x;
g0110 *= norm10.y;
g1010 *= norm10.z;
g1110 *= norm10.w;
tvec4
g0011 *= norm11.x;
g0111 *= norm11.y;
g1011 *= norm11.z;
g1111 *= norm11.w;
T n0000 = dot(g0000, Pf0);
T n1000 = dot(g1000, tvec4
T n0100 = dot(g0100, tvec4
T n1100 = dot(g1100, tvec4
T n0010 = dot(g0010, tvec4
T n1010 = dot(g1010, tvec4
T n0110 = dot(g0110, tvec4
T n1110 = dot(g1110, tvec4
T n0001 = dot(g0001, tvec4
T n1001 = dot(g1001, tvec4
T n0101 = dot(g0101, tvec4
T n1101 = dot(g1101, tvec4
T n0011 = dot(g0011, tvec4
T n1011 = dot(g1011, tvec4
T n0111 = dot(g0111, tvec4
T n1111 = dot(g1111, Pf1);
tvec4
tvec4
tvec4
tvec4
tvec2
T n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x);
return T(2.2) * n_xyzw;
}
template
GLM_FUNC_QUALIFIER T simplex(glm::tvec2
{
tvec4
T( 0.211324865405187), // (3.0 – sqrt(3.0)) / 6.0
T( 0.366025403784439), // 0.5 * (sqrt(3.0) – 1.0)
T(-0.577350269189626), // -1.0 + 2.0 * C.x
T( 0.024390243902439)); // 1.0 / 41.0
// First corner
tvec2
tvec2
// Other corners
//i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
//i1.y = 1.0 – i1.x;
tvec2
// x0 = x0 – 0.0 + 0.0 * C.xx ;
// x1 = x0 – i1 + 1.0 * C.xx ;
// x2 = x0 – 1.0 + 2.0 * C.xx ;
tvec4
x12 = tvec4
// Permutations
i = mod(i, tvec2
tvec3
detail::permute(i.y + tvec3
+ i.x + tvec3
tvec3
dot(x0, x0),
dot(tvec2
dot(tvec2
m = m * m ;
m = m * m ;
// 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)
tvec3
tvec3
tvec3
tvec3
// Normalise gradients implicitly by scaling m
// Inlined for speed: m *= taylorInvSqrt( a0*a0 + h*h );
m *= static_cast
// Compute final noise value at P
tvec3
g.x = a0.x * x0.x + h.x * x0.y;
//g.yz = a0.yz * x12.xz + h.yz * x12.yw;
g.y = a0.y * x12.x + h.y * x12.y;
g.z = a0.z * x12.z + h.z * x12.w;
return T(130) * dot(m, g);
}
template
GLM_FUNC_QUALIFIER T simplex(tvec3
{
tvec2
tvec4
// First corner
tvec3
tvec3
// Other corners
tvec3
tvec3
tvec3
tvec3
// x0 = x0 – 0.0 + 0.0 * C.xxx;
// x1 = x0 – i1 + 1.0 * C.xxx;
// x2 = x0 – i2 + 2.0 * C.xxx;
// x3 = x0 – 1.0 + 3.0 * C.xxx;
tvec3
tvec3
tvec3
// Permutations
i = detail::mod289(i);
tvec4
i.z + tvec4
i.y + tvec4
i.x + tvec4
// Gradients: 7×7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
T n_ = static_cast
tvec3
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
// vec4 s0 = vec4(lessThan(b0,0.0))*2.0 – 1.0;
// vec4 s1 = vec4(lessThan(b1,0.0))*2.0 – 1.0;
tvec4
tvec4
tvec4
tvec4
tvec4
tvec3
tvec3
tvec3
tvec3
// Normalise gradients
tvec4
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
// Mix final noise value
tvec4
m = m * m;
return T(42) * dot(m * m, tvec4
}
template
GLM_FUNC_QUALIFIER T simplex(tvec4
{
tvec4
0.138196601125011, // (5 – sqrt(5))/20 G4
0.276393202250021, // 2 * G4
0.414589803375032, // 3 * G4
-0.447213595499958); // -1 + 4 * G4
// (sqrt(5) – 1)/4 = F4, used once below
T const F4 = static_cast
// First corner
tvec4
tvec4
// Other corners
// Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
tvec4
tvec3
tvec3
// i0.x = dot(isX, vec3(1.0));
//i0.x = isX.x + isX.y + isX.z;
//i0.yzw = static_cast
i0 = tvec4
// i0.y += dot(isYZ.xy, vec2(1.0));
i0.y += isYZ.x + isYZ.y;
//i0.zw += 1.0 – tvec2
i0.z += static_cast
i0.w += static_cast
i0.z += isYZ.z;
i0.w += static_cast
// i0 now contains the unique values 0,1,2,3 in each channel
tvec4
tvec4
tvec4
// x0 = x0 – 0.0 + 0.0 * C.xxxx
// x1 = x0 – i1 + 0.0 * C.xxxx
// x2 = x0 – i2 + 0.0 * C.xxxx
// x3 = x0 – i3 + 0.0 * C.xxxx
// x4 = x0 – 1.0 + 4.0 * C.xxxx
tvec4
tvec4
tvec4
tvec4
// Permutations
i = mod(i, tvec4
T j0 = detail::permute(detail::permute(detail::permute(detail::permute(i.w) + i.z) + i.y) + i.x);
tvec4
i.w + tvec4
i.z + tvec4
i.y + tvec4
i.x + tvec4
// Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
// 7*7*6 = 294, which is close to the ring size 17*17 = 289.
tvec4
tvec4
tvec4
tvec4
tvec4
tvec4
// Normalise gradients
tvec4
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
p4 *= detail::taylorInvSqrt(dot(p4, p4));
// Mix contributions from the five corners
tvec3
tvec2
m0 = m0 * m0;
m1 = m1 * m1;
return T(49) *
(dot(m0 * m0, tvec3
dot(m1 * m1, tvec2
}
}//namespace glm