You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

867 lines
37 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref gtc_noise
  24. /// @file glm/gtc/noise.inl
  25. /// @date 2011-04-21 / 2012-04-07
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. // Based on the work of Stefan Gustavson and Ashima Arts on "webgl-noise":
  29. // https://github.com/ashima/webgl-noise
  30. // Following Stefan Gustavson's paper "Simplex noise demystified":
  31. // http://www.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
  32. ///////////////////////////////////////////////////////////////////////////////////
  33. namespace glm
  34. {
  35. template <typename T>
  36. GLM_FUNC_QUALIFIER T mod289(T const & x)
  37. {
  38. return x - floor(x * T(1.0 / 289.0)) * T(289.0);
  39. }
  40. template <typename T>
  41. GLM_FUNC_QUALIFIER T permute(T const & x)
  42. {
  43. return mod289(((x * T(34)) + T(1)) * x);
  44. }
  45. template <typename T, template<typename> class vecType>
  46. GLM_FUNC_QUALIFIER vecType<T> permute(vecType<T> const & x)
  47. {
  48. return mod289(((x * T(34)) + T(1)) * x);
  49. }
  50. template <typename T>
  51. GLM_FUNC_QUALIFIER T taylorInvSqrt(T const & r)
  52. {
  53. return T(1.79284291400159) - T(0.85373472095314) * r;
  54. }
  55. template <typename T, template<typename> class vecType>
  56. GLM_FUNC_QUALIFIER vecType<T> taylorInvSqrt(vecType<T> const & r)
  57. {
  58. return T(1.79284291400159) - T(0.85373472095314) * r;
  59. }
  60. template <typename T, template <typename> class vecType>
  61. GLM_FUNC_QUALIFIER vecType<T> fade(vecType<T> const & t)
  62. {
  63. return t * t * t * (t * (t * T(6) - T(15)) + T(10));
  64. }
  65. template <typename T>
  66. GLM_FUNC_QUALIFIER detail::tvec4<T> grad4(T const & j, detail::tvec4<T> const & ip)
  67. {
  68. detail::tvec3<T> pXYZ = floor(fract(detail::tvec3<T>(j) * detail::tvec3<T>(ip)) * T(7)) * ip[2] - T(1);
  69. T pW = T(1.5) - dot(abs(pXYZ), detail::tvec3<T>(1));
  70. detail::tvec4<T> s = detail::tvec4<T>(lessThan(detail::tvec4<T>(pXYZ, pW), detail::tvec4<T>(0.0)));
  71. pXYZ = pXYZ + (detail::tvec3<T>(s) * T(2) - T(1)) * s.w;
  72. return detail::tvec4<T>(pXYZ, pW);
  73. }
  74. // Classic Perlin noise
  75. template <typename T>
  76. GLM_FUNC_QUALIFIER T perlin(detail::tvec2<T> const & P)
  77. {
  78. detail::tvec4<T> Pi = glm::floor(detail::tvec4<T>(P.x, P.y, P.x, P.y)) + detail::tvec4<T>(0.0, 0.0, 1.0, 1.0);
  79. detail::tvec4<T> Pf = glm::fract(detail::tvec4<T>(P.x, P.y, P.x, P.y)) - detail::tvec4<T>(0.0, 0.0, 1.0, 1.0);
  80. Pi = mod(Pi, T(289)); // To avoid truncation effects in permutation
  81. detail::tvec4<T> ix(Pi.x, Pi.z, Pi.x, Pi.z);
  82. detail::tvec4<T> iy(Pi.y, Pi.y, Pi.w, Pi.w);
  83. detail::tvec4<T> fx(Pf.x, Pf.z, Pf.x, Pf.z);
  84. detail::tvec4<T> fy(Pf.y, Pf.y, Pf.w, Pf.w);
  85. detail::tvec4<T> i = glm::permute(glm::permute(ix) + iy);
  86. detail::tvec4<T> gx = T(2) * glm::fract(i / T(41)) - T(1);
  87. detail::tvec4<T> gy = glm::abs(gx) - T(0.5);
  88. detail::tvec4<T> tx = glm::floor(gx + T(0.5));
  89. gx = gx - tx;
  90. detail::tvec2<T> g00(gx.x, gy.x);
  91. detail::tvec2<T> g10(gx.y, gy.y);
  92. detail::tvec2<T> g01(gx.z, gy.z);
  93. detail::tvec2<T> g11(gx.w, gy.w);
  94. detail::tvec4<T> norm = taylorInvSqrt(detail::tvec4<T>(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  95. g00 *= norm.x;
  96. g01 *= norm.y;
  97. g10 *= norm.z;
  98. g11 *= norm.w;
  99. T n00 = dot(g00, detail::tvec2<T>(fx.x, fy.x));
  100. T n10 = dot(g10, detail::tvec2<T>(fx.y, fy.y));
  101. T n01 = dot(g01, detail::tvec2<T>(fx.z, fy.z));
  102. T n11 = dot(g11, detail::tvec2<T>(fx.w, fy.w));
  103. detail::tvec2<T> fade_xy = fade(detail::tvec2<T>(Pf.x, Pf.y));
  104. detail::tvec2<T> n_x = mix(detail::tvec2<T>(n00, n01), detail::tvec2<T>(n10, n11), fade_xy.x);
  105. T n_xy = mix(n_x.x, n_x.y, fade_xy.y);
  106. return T(2.3) * n_xy;
  107. }
  108. // Classic Perlin noise
  109. template <typename T>
  110. GLM_FUNC_QUALIFIER T perlin(detail::tvec3<T> const & P)
  111. {
  112. detail::tvec3<T> Pi0 = floor(P); // Integer part for indexing
  113. detail::tvec3<T> Pi1 = Pi0 + T(1); // Integer part + 1
  114. Pi0 = mod289(Pi0);
  115. Pi1 = mod289(Pi1);
  116. detail::tvec3<T> Pf0 = fract(P); // Fractional part for interpolation
  117. detail::tvec3<T> Pf1 = Pf0 - T(1); // Fractional part - 1.0
  118. detail::tvec4<T> ix(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  119. detail::tvec4<T> iy = detail::tvec4<T>(detail::tvec2<T>(Pi0.y), detail::tvec2<T>(Pi1.y));
  120. detail::tvec4<T> iz0(Pi0.z);
  121. detail::tvec4<T> iz1(Pi1.z);
  122. detail::tvec4<T> ixy = permute(permute(ix) + iy);
  123. detail::tvec4<T> ixy0 = permute(ixy + iz0);
  124. detail::tvec4<T> ixy1 = permute(ixy + iz1);
  125. detail::tvec4<T> gx0 = ixy0 * T(1.0 / 7.0);
  126. detail::tvec4<T> gy0 = fract(floor(gx0) * T(1.0 / 7.0)) - T(0.5);
  127. gx0 = fract(gx0);
  128. detail::tvec4<T> gz0 = detail::tvec4<T>(0.5) - abs(gx0) - abs(gy0);
  129. detail::tvec4<T> sz0 = step(gz0, detail::tvec4<T>(0.0));
  130. gx0 -= sz0 * (step(T(0), gx0) - T(0.5));
  131. gy0 -= sz0 * (step(T(0), gy0) - T(0.5));
  132. detail::tvec4<T> gx1 = ixy1 * T(1.0 / 7.0);
  133. detail::tvec4<T> gy1 = fract(floor(gx1) * T(1.0 / 7.0)) - T(0.5);
  134. gx1 = fract(gx1);
  135. detail::tvec4<T> gz1 = detail::tvec4<T>(0.5) - abs(gx1) - abs(gy1);
  136. detail::tvec4<T> sz1 = step(gz1, detail::tvec4<T>(0.0));
  137. gx1 -= sz1 * (step(T(0), gx1) - T(0.5));
  138. gy1 -= sz1 * (step(T(0), gy1) - T(0.5));
  139. detail::tvec3<T> g000(gx0.x, gy0.x, gz0.x);
  140. detail::tvec3<T> g100(gx0.y, gy0.y, gz0.y);
  141. detail::tvec3<T> g010(gx0.z, gy0.z, gz0.z);
  142. detail::tvec3<T> g110(gx0.w, gy0.w, gz0.w);
  143. detail::tvec3<T> g001(gx1.x, gy1.x, gz1.x);
  144. detail::tvec3<T> g101(gx1.y, gy1.y, gz1.y);
  145. detail::tvec3<T> g011(gx1.z, gy1.z, gz1.z);
  146. detail::tvec3<T> g111(gx1.w, gy1.w, gz1.w);
  147. detail::tvec4<T> norm0 = taylorInvSqrt(detail::tvec4<T>(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  148. g000 *= norm0.x;
  149. g010 *= norm0.y;
  150. g100 *= norm0.z;
  151. g110 *= norm0.w;
  152. detail::tvec4<T> norm1 = taylorInvSqrt(detail::tvec4<T>(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  153. g001 *= norm1.x;
  154. g011 *= norm1.y;
  155. g101 *= norm1.z;
  156. g111 *= norm1.w;
  157. T n000 = dot(g000, Pf0);
  158. T n100 = dot(g100, detail::tvec3<T>(Pf1.x, Pf0.y, Pf0.z));
  159. T n010 = dot(g010, detail::tvec3<T>(Pf0.x, Pf1.y, Pf0.z));
  160. T n110 = dot(g110, detail::tvec3<T>(Pf1.x, Pf1.y, Pf0.z));
  161. T n001 = dot(g001, detail::tvec3<T>(Pf0.x, Pf0.y, Pf1.z));
  162. T n101 = dot(g101, detail::tvec3<T>(Pf1.x, Pf0.y, Pf1.z));
  163. T n011 = dot(g011, detail::tvec3<T>(Pf0.x, Pf1.y, Pf1.z));
  164. T n111 = dot(g111, Pf1);
  165. detail::tvec3<T> fade_xyz = fade(Pf0);
  166. detail::tvec4<T> n_z = mix(detail::tvec4<T>(n000, n100, n010, n110), detail::tvec4<T>(n001, n101, n011, n111), fade_xyz.z);
  167. detail::tvec2<T> n_yz = mix(detail::tvec2<T>(n_z.x, n_z.y), detail::tvec2<T>(n_z.z, n_z.w), fade_xyz.y);
  168. T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
  169. return T(2.2) * n_xyz;
  170. }
  171. /*
  172. // Classic Perlin noise
  173. template <typename T>
  174. GLM_FUNC_QUALIFIER T perlin(detail::tvec3<T> const & P)
  175. {
  176. detail::tvec3<T> Pi0 = floor(P); // Integer part for indexing
  177. detail::tvec3<T> Pi1 = Pi0 + T(1); // Integer part + 1
  178. Pi0 = mod(Pi0, T(289));
  179. Pi1 = mod(Pi1, T(289));
  180. detail::tvec3<T> Pf0 = fract(P); // Fractional part for interpolation
  181. detail::tvec3<T> Pf1 = Pf0 - T(1); // Fractional part - 1.0
  182. detail::tvec4<T> ix(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  183. detail::tvec4<T> iy(Pi0.y, Pi0.y, Pi1.y, Pi1.y);
  184. detail::tvec4<T> iz0(Pi0.z);
  185. detail::tvec4<T> iz1(Pi1.z);
  186. detail::tvec4<T> ixy = permute(permute(ix) + iy);
  187. detail::tvec4<T> ixy0 = permute(ixy + iz0);
  188. detail::tvec4<T> ixy1 = permute(ixy + iz1);
  189. detail::tvec4<T> gx0 = ixy0 / T(7);
  190. detail::tvec4<T> gy0 = fract(floor(gx0) / T(7)) - T(0.5);
  191. gx0 = fract(gx0);
  192. detail::tvec4<T> gz0 = detail::tvec4<T>(0.5) - abs(gx0) - abs(gy0);
  193. detail::tvec4<T> sz0 = step(gz0, detail::tvec4<T>(0.0));
  194. gx0 -= sz0 * (step(0.0, gx0) - T(0.5));
  195. gy0 -= sz0 * (step(0.0, gy0) - T(0.5));
  196. detail::tvec4<T> gx1 = ixy1 / T(7);
  197. detail::tvec4<T> gy1 = fract(floor(gx1) / T(7)) - T(0.5);
  198. gx1 = fract(gx1);
  199. detail::tvec4<T> gz1 = detail::tvec4<T>(0.5) - abs(gx1) - abs(gy1);
  200. detail::tvec4<T> sz1 = step(gz1, detail::tvec4<T>(0.0));
  201. gx1 -= sz1 * (step(T(0), gx1) - T(0.5));
  202. gy1 -= sz1 * (step(T(0), gy1) - T(0.5));
  203. detail::tvec3<T> g000(gx0.x, gy0.x, gz0.x);
  204. detail::tvec3<T> g100(gx0.y, gy0.y, gz0.y);
  205. detail::tvec3<T> g010(gx0.z, gy0.z, gz0.z);
  206. detail::tvec3<T> g110(gx0.w, gy0.w, gz0.w);
  207. detail::tvec3<T> g001(gx1.x, gy1.x, gz1.x);
  208. detail::tvec3<T> g101(gx1.y, gy1.y, gz1.y);
  209. detail::tvec3<T> g011(gx1.z, gy1.z, gz1.z);
  210. detail::tvec3<T> g111(gx1.w, gy1.w, gz1.w);
  211. detail::tvec4<T> norm0 = taylorInvSqrt(detail::tvec4<T>(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  212. g000 *= norm0.x;
  213. g010 *= norm0.y;
  214. g100 *= norm0.z;
  215. g110 *= norm0.w;
  216. detail::tvec4<T> norm1 = taylorInvSqrt(detail::tvec4<T>(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  217. g001 *= norm1.x;
  218. g011 *= norm1.y;
  219. g101 *= norm1.z;
  220. g111 *= norm1.w;
  221. T n000 = dot(g000, Pf0);
  222. T n100 = dot(g100, detail::tvec3<T>(Pf1.x, Pf0.y, Pf0.z));
  223. T n010 = dot(g010, detail::tvec3<T>(Pf0.x, Pf1.y, Pf0.z));
  224. T n110 = dot(g110, detail::tvec3<T>(Pf1.x, Pf1.y, Pf0.z));
  225. T n001 = dot(g001, detail::tvec3<T>(Pf0.x, Pf0.y, Pf1.z));
  226. T n101 = dot(g101, detail::tvec3<T>(Pf1.x, Pf0.y, Pf1.z));
  227. T n011 = dot(g011, detail::tvec3<T>(Pf0.x, Pf1.y, Pf1.z));
  228. T n111 = dot(g111, Pf1);
  229. detail::tvec3<T> fade_xyz = fade(Pf0);
  230. detail::tvec4<T> n_z = mix(detail::tvec4<T>(n000, n100, n010, n110), detail::tvec4<T>(n001, n101, n011, n111), fade_xyz.z);
  231. detail::tvec2<T> n_yz = mix(
  232. detail::tvec2<T>(n_z.x, n_z.y),
  233. detail::tvec2<T>(n_z.z, n_z.w), fade_xyz.y);
  234. T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
  235. return T(2.2) * n_xyz;
  236. }
  237. */
  238. // Classic Perlin noise
  239. template <typename T>
  240. GLM_FUNC_QUALIFIER T perlin(detail::tvec4<T> const & P)
  241. {
  242. detail::tvec4<T> Pi0 = floor(P); // Integer part for indexing
  243. detail::tvec4<T> Pi1 = Pi0 + T(1); // Integer part + 1
  244. Pi0 = mod(Pi0, T(289));
  245. Pi1 = mod(Pi1, T(289));
  246. detail::tvec4<T> Pf0 = fract(P); // Fractional part for interpolation
  247. detail::tvec4<T> Pf1 = Pf0 - T(1); // Fractional part - 1.0
  248. detail::tvec4<T> ix(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  249. detail::tvec4<T> iy(Pi0.y, Pi0.y, Pi1.y, Pi1.y);
  250. detail::tvec4<T> iz0(Pi0.z);
  251. detail::tvec4<T> iz1(Pi1.z);
  252. detail::tvec4<T> iw0(Pi0.w);
  253. detail::tvec4<T> iw1(Pi1.w);
  254. detail::tvec4<T> ixy = permute(permute(ix) + iy);
  255. detail::tvec4<T> ixy0 = permute(ixy + iz0);
  256. detail::tvec4<T> ixy1 = permute(ixy + iz1);
  257. detail::tvec4<T> ixy00 = permute(ixy0 + iw0);
  258. detail::tvec4<T> ixy01 = permute(ixy0 + iw1);
  259. detail::tvec4<T> ixy10 = permute(ixy1 + iw0);
  260. detail::tvec4<T> ixy11 = permute(ixy1 + iw1);
  261. detail::tvec4<T> gx00 = ixy00 / T(7);
  262. detail::tvec4<T> gy00 = floor(gx00) / T(7);
  263. detail::tvec4<T> gz00 = floor(gy00) / T(6);
  264. gx00 = fract(gx00) - T(0.5);
  265. gy00 = fract(gy00) - T(0.5);
  266. gz00 = fract(gz00) - T(0.5);
  267. detail::tvec4<T> gw00 = detail::tvec4<T>(0.75) - abs(gx00) - abs(gy00) - abs(gz00);
  268. detail::tvec4<T> sw00 = step(gw00, detail::tvec4<T>(0.0));
  269. gx00 -= sw00 * (step(T(0), gx00) - T(0.5));
  270. gy00 -= sw00 * (step(T(0), gy00) - T(0.5));
  271. detail::tvec4<T> gx01 = ixy01 / T(7);
  272. detail::tvec4<T> gy01 = floor(gx01) / T(7);
  273. detail::tvec4<T> gz01 = floor(gy01) / T(6);
  274. gx01 = fract(gx01) - T(0.5);
  275. gy01 = fract(gy01) - T(0.5);
  276. gz01 = fract(gz01) - T(0.5);
  277. detail::tvec4<T> gw01 = detail::tvec4<T>(0.75) - abs(gx01) - abs(gy01) - abs(gz01);
  278. detail::tvec4<T> sw01 = step(gw01, detail::tvec4<T>(0.0));
  279. gx01 -= sw01 * (step(T(0), gx01) - T(0.5));
  280. gy01 -= sw01 * (step(T(0), gy01) - T(0.5));
  281. detail::tvec4<T> gx10 = ixy10 / T(7);
  282. detail::tvec4<T> gy10 = floor(gx10) / T(7);
  283. detail::tvec4<T> gz10 = floor(gy10) / T(6);
  284. gx10 = fract(gx10) - T(0.5);
  285. gy10 = fract(gy10) - T(0.5);
  286. gz10 = fract(gz10) - T(0.5);
  287. detail::tvec4<T> gw10 = detail::tvec4<T>(0.75) - abs(gx10) - abs(gy10) - abs(gz10);
  288. detail::tvec4<T> sw10 = step(gw10, detail::tvec4<T>(0));
  289. gx10 -= sw10 * (step(T(0), gx10) - T(0.5));
  290. gy10 -= sw10 * (step(T(0), gy10) - T(0.5));
  291. detail::tvec4<T> gx11 = ixy11 / T(7);
  292. detail::tvec4<T> gy11 = floor(gx11) / T(7);
  293. detail::tvec4<T> gz11 = floor(gy11) / T(6);
  294. gx11 = fract(gx11) - T(0.5);
  295. gy11 = fract(gy11) - T(0.5);
  296. gz11 = fract(gz11) - T(0.5);
  297. detail::tvec4<T> gw11 = detail::tvec4<T>(0.75) - abs(gx11) - abs(gy11) - abs(gz11);
  298. detail::tvec4<T> sw11 = step(gw11, detail::tvec4<T>(0.0));
  299. gx11 -= sw11 * (step(T(0), gx11) - T(0.5));
  300. gy11 -= sw11 * (step(T(0), gy11) - T(0.5));
  301. detail::tvec4<T> g0000(gx00.x, gy00.x, gz00.x, gw00.x);
  302. detail::tvec4<T> g1000(gx00.y, gy00.y, gz00.y, gw00.y);
  303. detail::tvec4<T> g0100(gx00.z, gy00.z, gz00.z, gw00.z);
  304. detail::tvec4<T> g1100(gx00.w, gy00.w, gz00.w, gw00.w);
  305. detail::tvec4<T> g0010(gx10.x, gy10.x, gz10.x, gw10.x);
  306. detail::tvec4<T> g1010(gx10.y, gy10.y, gz10.y, gw10.y);
  307. detail::tvec4<T> g0110(gx10.z, gy10.z, gz10.z, gw10.z);
  308. detail::tvec4<T> g1110(gx10.w, gy10.w, gz10.w, gw10.w);
  309. detail::tvec4<T> g0001(gx01.x, gy01.x, gz01.x, gw01.x);
  310. detail::tvec4<T> g1001(gx01.y, gy01.y, gz01.y, gw01.y);
  311. detail::tvec4<T> g0101(gx01.z, gy01.z, gz01.z, gw01.z);
  312. detail::tvec4<T> g1101(gx01.w, gy01.w, gz01.w, gw01.w);
  313. detail::tvec4<T> g0011(gx11.x, gy11.x, gz11.x, gw11.x);
  314. detail::tvec4<T> g1011(gx11.y, gy11.y, gz11.y, gw11.y);
  315. detail::tvec4<T> g0111(gx11.z, gy11.z, gz11.z, gw11.z);
  316. detail::tvec4<T> g1111(gx11.w, gy11.w, gz11.w, gw11.w);
  317. detail::tvec4<T> norm00 = taylorInvSqrt(detail::tvec4<T>(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
  318. g0000 *= norm00.x;
  319. g0100 *= norm00.y;
  320. g1000 *= norm00.z;
  321. g1100 *= norm00.w;
  322. detail::tvec4<T> norm01 = taylorInvSqrt(detail::tvec4<T>(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
  323. g0001 *= norm01.x;
  324. g0101 *= norm01.y;
  325. g1001 *= norm01.z;
  326. g1101 *= norm01.w;
  327. detail::tvec4<T> norm10 = taylorInvSqrt(detail::tvec4<T>(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
  328. g0010 *= norm10.x;
  329. g0110 *= norm10.y;
  330. g1010 *= norm10.z;
  331. g1110 *= norm10.w;
  332. detail::tvec4<T> norm11 = taylorInvSqrt(detail::tvec4<T>(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
  333. g0011 *= norm11.x;
  334. g0111 *= norm11.y;
  335. g1011 *= norm11.z;
  336. g1111 *= norm11.w;
  337. T n0000 = dot(g0000, Pf0);
  338. T n1000 = dot(g1000, detail::tvec4<T>(Pf1.x, Pf0.y, Pf0.z, Pf0.w));
  339. T n0100 = dot(g0100, detail::tvec4<T>(Pf0.x, Pf1.y, Pf0.z, Pf0.w));
  340. T n1100 = dot(g1100, detail::tvec4<T>(Pf1.x, Pf1.y, Pf0.z, Pf0.w));
  341. T n0010 = dot(g0010, detail::tvec4<T>(Pf0.x, Pf0.y, Pf1.z, Pf0.w));
  342. T n1010 = dot(g1010, detail::tvec4<T>(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
  343. T n0110 = dot(g0110, detail::tvec4<T>(Pf0.x, Pf1.y, Pf1.z, Pf0.w));
  344. T n1110 = dot(g1110, detail::tvec4<T>(Pf1.x, Pf1.y, Pf1.z, Pf0.w));
  345. T n0001 = dot(g0001, detail::tvec4<T>(Pf0.x, Pf0.y, Pf0.z, Pf1.w));
  346. T n1001 = dot(g1001, detail::tvec4<T>(Pf1.x, Pf0.y, Pf0.z, Pf1.w));
  347. T n0101 = dot(g0101, detail::tvec4<T>(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
  348. T n1101 = dot(g1101, detail::tvec4<T>(Pf1.x, Pf1.y, Pf0.z, Pf1.w));
  349. T n0011 = dot(g0011, detail::tvec4<T>(Pf0.x, Pf0.y, Pf1.z, Pf1.w));
  350. T n1011 = dot(g1011, detail::tvec4<T>(Pf1.x, Pf0.y, Pf1.z, Pf1.w));
  351. T n0111 = dot(g0111, detail::tvec4<T>(Pf0.x, Pf1.y, Pf1.z, Pf1.w));
  352. T n1111 = dot(g1111, Pf1);
  353. detail::tvec4<T> fade_xyzw = fade(Pf0);
  354. detail::tvec4<T> n_0w = mix(detail::tvec4<T>(n0000, n1000, n0100, n1100), detail::tvec4<T>(n0001, n1001, n0101, n1101), fade_xyzw.w);
  355. detail::tvec4<T> n_1w = mix(detail::tvec4<T>(n0010, n1010, n0110, n1110), detail::tvec4<T>(n0011, n1011, n0111, n1111), fade_xyzw.w);
  356. detail::tvec4<T> n_zw = mix(n_0w, n_1w, fade_xyzw.z);
  357. detail::tvec2<T> n_yzw = mix(detail::tvec2<T>(n_zw.x, n_zw.y), detail::tvec2<T>(n_zw.z, n_zw.w), fade_xyzw.y);
  358. T n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x);
  359. return T(2.2) * n_xyzw;
  360. }
  361. // Classic Perlin noise, periodic variant
  362. template <typename T>
  363. GLM_FUNC_QUALIFIER T perlin(detail::tvec2<T> const & P, detail::tvec2<T> const & rep)
  364. {
  365. detail::tvec4<T> Pi = floor(detail::tvec4<T>(P.x, P.y, P.x, P.y)) + detail::tvec4<T>(0.0, 0.0, 1.0, 1.0);
  366. detail::tvec4<T> Pf = fract(detail::tvec4<T>(P.x, P.y, P.x, P.y)) - detail::tvec4<T>(0.0, 0.0, 1.0, 1.0);
  367. Pi = mod(Pi, detail::tvec4<T>(rep.x, rep.y, rep.x, rep.y)); // To create noise with explicit period
  368. Pi = mod(Pi, T(289)); // To avoid truncation effects in permutation
  369. detail::tvec4<T> ix(Pi.x, Pi.z, Pi.x, Pi.z);
  370. detail::tvec4<T> iy(Pi.y, Pi.y, Pi.w, Pi.w);
  371. detail::tvec4<T> fx(Pf.x, Pf.z, Pf.x, Pf.z);
  372. detail::tvec4<T> fy(Pf.y, Pf.y, Pf.w, Pf.w);
  373. detail::tvec4<T> i = permute(permute(ix) + iy);
  374. detail::tvec4<T> gx = T(2) * fract(i / T(41)) - T(1);
  375. detail::tvec4<T> gy = abs(gx) - T(0.5);
  376. detail::tvec4<T> tx = floor(gx + T(0.5));
  377. gx = gx - tx;
  378. detail::tvec2<T> g00(gx.x, gy.x);
  379. detail::tvec2<T> g10(gx.y, gy.y);
  380. detail::tvec2<T> g01(gx.z, gy.z);
  381. detail::tvec2<T> g11(gx.w, gy.w);
  382. detail::tvec4<T> norm = taylorInvSqrt(detail::tvec4<T>(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
  383. g00 *= norm.x;
  384. g01 *= norm.y;
  385. g10 *= norm.z;
  386. g11 *= norm.w;
  387. T n00 = dot(g00, detail::tvec2<T>(fx.x, fy.x));
  388. T n10 = dot(g10, detail::tvec2<T>(fx.y, fy.y));
  389. T n01 = dot(g01, detail::tvec2<T>(fx.z, fy.z));
  390. T n11 = dot(g11, detail::tvec2<T>(fx.w, fy.w));
  391. detail::tvec2<T> fade_xy = fade(detail::tvec2<T>(Pf.x, Pf.y));
  392. detail::tvec2<T> n_x = mix(detail::tvec2<T>(n00, n01), detail::tvec2<T>(n10, n11), fade_xy.x);
  393. T n_xy = mix(n_x.x, n_x.y, fade_xy.y);
  394. return T(2.3) * n_xy;
  395. }
  396. // Classic Perlin noise, periodic variant
  397. template <typename T>
  398. GLM_FUNC_QUALIFIER T perlin(detail::tvec3<T> const & P, detail::tvec3<T> const & rep)
  399. {
  400. detail::tvec3<T> Pi0 = mod(floor(P), rep); // Integer part, modulo period
  401. detail::tvec3<T> Pi1 = mod(Pi0 + detail::tvec3<T>(1.0), rep); // Integer part + 1, mod period
  402. Pi0 = mod(Pi0, T(289));
  403. Pi1 = mod(Pi1, T(289));
  404. detail::tvec3<T> Pf0 = fract(P); // Fractional part for interpolation
  405. detail::tvec3<T> Pf1 = Pf0 - detail::tvec3<T>(1.0); // Fractional part - 1.0
  406. detail::tvec4<T> ix = detail::tvec4<T>(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  407. detail::tvec4<T> iy = detail::tvec4<T>(Pi0.y, Pi0.y, Pi1.y, Pi1.y);
  408. detail::tvec4<T> iz0(Pi0.z);
  409. detail::tvec4<T> iz1(Pi1.z);
  410. detail::tvec4<T> ixy = permute(permute(ix) + iy);
  411. detail::tvec4<T> ixy0 = permute(ixy + iz0);
  412. detail::tvec4<T> ixy1 = permute(ixy + iz1);
  413. detail::tvec4<T> gx0 = ixy0 / T(7);
  414. detail::tvec4<T> gy0 = fract(floor(gx0) / T(7)) - T(0.5);
  415. gx0 = fract(gx0);
  416. detail::tvec4<T> gz0 = detail::tvec4<T>(0.5) - abs(gx0) - abs(gy0);
  417. detail::tvec4<T> sz0 = step(gz0, detail::tvec4<T>(0));
  418. gx0 -= sz0 * (step(0.0, gx0) - T(0.5));
  419. gy0 -= sz0 * (step(0.0, gy0) - T(0.5));
  420. detail::tvec4<T> gx1 = ixy1 / T(7);
  421. detail::tvec4<T> gy1 = fract(floor(gx1) / T(7)) - T(0.5);
  422. gx1 = fract(gx1);
  423. detail::tvec4<T> gz1 = detail::tvec4<T>(0.5) - abs(gx1) - abs(gy1);
  424. detail::tvec4<T> sz1 = step(gz1, detail::tvec4<T>(0.0));
  425. gx1 -= sz1 * (step(0.0, gx1) - T(0.5));
  426. gy1 -= sz1 * (step(0.0, gy1) - T(0.5));
  427. detail::tvec3<T> g000 = detail::tvec3<T>(gx0.x, gy0.x, gz0.x);
  428. detail::tvec3<T> g100 = detail::tvec3<T>(gx0.y, gy0.y, gz0.y);
  429. detail::tvec3<T> g010 = detail::tvec3<T>(gx0.z, gy0.z, gz0.z);
  430. detail::tvec3<T> g110 = detail::tvec3<T>(gx0.w, gy0.w, gz0.w);
  431. detail::tvec3<T> g001 = detail::tvec3<T>(gx1.x, gy1.x, gz1.x);
  432. detail::tvec3<T> g101 = detail::tvec3<T>(gx1.y, gy1.y, gz1.y);
  433. detail::tvec3<T> g011 = detail::tvec3<T>(gx1.z, gy1.z, gz1.z);
  434. detail::tvec3<T> g111 = detail::tvec3<T>(gx1.w, gy1.w, gz1.w);
  435. detail::tvec4<T> norm0 = taylorInvSqrt(detail::tvec4<T>(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  436. g000 *= norm0.x;
  437. g010 *= norm0.y;
  438. g100 *= norm0.z;
  439. g110 *= norm0.w;
  440. detail::tvec4<T> norm1 = taylorInvSqrt(detail::tvec4<T>(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  441. g001 *= norm1.x;
  442. g011 *= norm1.y;
  443. g101 *= norm1.z;
  444. g111 *= norm1.w;
  445. T n000 = dot(g000, Pf0);
  446. T n100 = dot(g100, detail::tvec3<T>(Pf1.x, Pf0.y, Pf0.z));
  447. T n010 = dot(g010, detail::tvec3<T>(Pf0.x, Pf1.y, Pf0.z));
  448. T n110 = dot(g110, detail::tvec3<T>(Pf1.x, Pf1.y, Pf0.z));
  449. T n001 = dot(g001, detail::tvec3<T>(Pf0.x, Pf0.y, Pf1.z));
  450. T n101 = dot(g101, detail::tvec3<T>(Pf1.x, Pf0.y, Pf1.z));
  451. T n011 = dot(g011, detail::tvec3<T>(Pf0.x, Pf1.y, Pf1.z));
  452. T n111 = dot(g111, Pf1);
  453. detail::tvec3<T> fade_xyz = fade(Pf0);
  454. detail::tvec4<T> n_z = mix(detail::tvec4<T>(n000, n100, n010, n110), detail::tvec4<T>(n001, n101, n011, n111), fade_xyz.z);
  455. detail::tvec2<T> n_yz = mix(detail::tvec2<T>(n_z.x, n_z.y), detail::tvec2<T>(n_z.z, n_z.w), fade_xyz.y);
  456. T n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
  457. return T(2.2) * n_xyz;
  458. }
  459. // Classic Perlin noise, periodic version
  460. template <typename T>
  461. GLM_FUNC_QUALIFIER T perlin(detail::tvec4<T> const & P, detail::tvec4<T> const & rep)
  462. {
  463. detail::tvec4<T> Pi0 = mod(floor(P), rep); // Integer part modulo rep
  464. detail::tvec4<T> Pi1 = mod(Pi0 + T(1), rep); // Integer part + 1 mod rep
  465. detail::tvec4<T> Pf0 = fract(P); // Fractional part for interpolation
  466. detail::tvec4<T> Pf1 = Pf0 - T(1); // Fractional part - 1.0
  467. detail::tvec4<T> ix = detail::tvec4<T>(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  468. detail::tvec4<T> iy = detail::tvec4<T>(Pi0.y, Pi0.y, Pi1.y, Pi1.y);
  469. detail::tvec4<T> iz0(Pi0.z);
  470. detail::tvec4<T> iz1(Pi1.z);
  471. detail::tvec4<T> iw0(Pi0.w);
  472. detail::tvec4<T> iw1(Pi1.w);
  473. detail::tvec4<T> ixy = permute(permute(ix) + iy);
  474. detail::tvec4<T> ixy0 = permute(ixy + iz0);
  475. detail::tvec4<T> ixy1 = permute(ixy + iz1);
  476. detail::tvec4<T> ixy00 = permute(ixy0 + iw0);
  477. detail::tvec4<T> ixy01 = permute(ixy0 + iw1);
  478. detail::tvec4<T> ixy10 = permute(ixy1 + iw0);
  479. detail::tvec4<T> ixy11 = permute(ixy1 + iw1);
  480. detail::tvec4<T> gx00 = ixy00 / T(7);
  481. detail::tvec4<T> gy00 = floor(gx00) / T(7);
  482. detail::tvec4<T> gz00 = floor(gy00) / T(6);
  483. gx00 = fract(gx00) - T(0.5);
  484. gy00 = fract(gy00) - T(0.5);
  485. gz00 = fract(gz00) - T(0.5);
  486. detail::tvec4<T> gw00 = detail::tvec4<T>(0.75) - abs(gx00) - abs(gy00) - abs(gz00);
  487. detail::tvec4<T> sw00 = step(gw00, detail::tvec4<T>(0));
  488. gx00 -= sw00 * (step(0.0, gx00) - T(0.5));
  489. gy00 -= sw00 * (step(0.0, gy00) - T(0.5));
  490. detail::tvec4<T> gx01 = ixy01 / T(7);
  491. detail::tvec4<T> gy01 = floor(gx01) / T(7);
  492. detail::tvec4<T> gz01 = floor(gy01) / T(6);
  493. gx01 = fract(gx01) - T(0.5);
  494. gy01 = fract(gy01) - T(0.5);
  495. gz01 = fract(gz01) - T(0.5);
  496. detail::tvec4<T> gw01 = detail::tvec4<T>(0.75) - abs(gx01) - abs(gy01) - abs(gz01);
  497. detail::tvec4<T> sw01 = step(gw01, detail::tvec4<T>(0.0));
  498. gx01 -= sw01 * (step(0.0, gx01) - T(0.5));
  499. gy01 -= sw01 * (step(0.0, gy01) - T(0.5));
  500. detail::tvec4<T> gx10 = ixy10 / T(7);
  501. detail::tvec4<T> gy10 = floor(gx10) / T(7);
  502. detail::tvec4<T> gz10 = floor(gy10) / T(6);
  503. gx10 = fract(gx10) - T(0.5);
  504. gy10 = fract(gy10) - T(0.5);
  505. gz10 = fract(gz10) - T(0.5);
  506. detail::tvec4<T> gw10 = detail::tvec4<T>(0.75) - abs(gx10) - abs(gy10) - abs(gz10);
  507. detail::tvec4<T> sw10 = step(gw10, detail::tvec4<T>(0.0));
  508. gx10 -= sw10 * (step(0.0, gx10) - T(0.5));
  509. gy10 -= sw10 * (step(0.0, gy10) - T(0.5));
  510. detail::tvec4<T> gx11 = ixy11 / T(7);
  511. detail::tvec4<T> gy11 = floor(gx11) / T(7);
  512. detail::tvec4<T> gz11 = floor(gy11) / T(6);
  513. gx11 = fract(gx11) - T(0.5);
  514. gy11 = fract(gy11) - T(0.5);
  515. gz11 = fract(gz11) - T(0.5);
  516. detail::tvec4<T> gw11 = detail::tvec4<T>(0.75) - abs(gx11) - abs(gy11) - abs(gz11);
  517. detail::tvec4<T> sw11 = step(gw11, detail::tvec4<T>(0.0));
  518. gx11 -= sw11 * (step(0.0, gx11) - T(0.5));
  519. gy11 -= sw11 * (step(0.0, gy11) - T(0.5));
  520. detail::tvec4<T> g0000(gx00.x, gy00.x, gz00.x, gw00.x);
  521. detail::tvec4<T> g1000(gx00.y, gy00.y, gz00.y, gw00.y);
  522. detail::tvec4<T> g0100(gx00.z, gy00.z, gz00.z, gw00.z);
  523. detail::tvec4<T> g1100(gx00.w, gy00.w, gz00.w, gw00.w);
  524. detail::tvec4<T> g0010(gx10.x, gy10.x, gz10.x, gw10.x);
  525. detail::tvec4<T> g1010(gx10.y, gy10.y, gz10.y, gw10.y);
  526. detail::tvec4<T> g0110(gx10.z, gy10.z, gz10.z, gw10.z);
  527. detail::tvec4<T> g1110(gx10.w, gy10.w, gz10.w, gw10.w);
  528. detail::tvec4<T> g0001(gx01.x, gy01.x, gz01.x, gw01.x);
  529. detail::tvec4<T> g1001(gx01.y, gy01.y, gz01.y, gw01.y);
  530. detail::tvec4<T> g0101(gx01.z, gy01.z, gz01.z, gw01.z);
  531. detail::tvec4<T> g1101(gx01.w, gy01.w, gz01.w, gw01.w);
  532. detail::tvec4<T> g0011(gx11.x, gy11.x, gz11.x, gw11.x);
  533. detail::tvec4<T> g1011(gx11.y, gy11.y, gz11.y, gw11.y);
  534. detail::tvec4<T> g0111(gx11.z, gy11.z, gz11.z, gw11.z);
  535. detail::tvec4<T> g1111(gx11.w, gy11.w, gz11.w, gw11.w);
  536. detail::tvec4<T> norm00 = taylorInvSqrt(detail::tvec4<T>(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
  537. g0000 *= norm00.x;
  538. g0100 *= norm00.y;
  539. g1000 *= norm00.z;
  540. g1100 *= norm00.w;
  541. detail::tvec4<T> norm01 = taylorInvSqrt(detail::tvec4<T>(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
  542. g0001 *= norm01.x;
  543. g0101 *= norm01.y;
  544. g1001 *= norm01.z;
  545. g1101 *= norm01.w;
  546. detail::tvec4<T> norm10 = taylorInvSqrt(detail::tvec4<T>(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
  547. g0010 *= norm10.x;
  548. g0110 *= norm10.y;
  549. g1010 *= norm10.z;
  550. g1110 *= norm10.w;
  551. detail::tvec4<T> norm11 = taylorInvSqrt(detail::tvec4<T>(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
  552. g0011 *= norm11.x;
  553. g0111 *= norm11.y;
  554. g1011 *= norm11.z;
  555. g1111 *= norm11.w;
  556. T n0000 = dot(g0000, Pf0);
  557. T n1000 = dot(g1000, detail::tvec4<T>(Pf1.x, Pf0.y, Pf0.z, Pf0.w));
  558. T n0100 = dot(g0100, detail::tvec4<T>(Pf0.x, Pf1.y, Pf0.z, Pf0.w));
  559. T n1100 = dot(g1100, detail::tvec4<T>(Pf1.x, Pf1.y, Pf0.z, Pf0.w));
  560. T n0010 = dot(g0010, detail::tvec4<T>(Pf0.x, Pf0.y, Pf1.z, Pf0.w));
  561. T n1010 = dot(g1010, detail::tvec4<T>(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
  562. T n0110 = dot(g0110, detail::tvec4<T>(Pf0.x, Pf1.y, Pf1.z, Pf0.w));
  563. T n1110 = dot(g1110, detail::tvec4<T>(Pf1.x, Pf1.y, Pf1.z, Pf0.w));
  564. T n0001 = dot(g0001, detail::tvec4<T>(Pf0.x, Pf0.y, Pf0.z, Pf1.w));
  565. T n1001 = dot(g1001, detail::tvec4<T>(Pf1.x, Pf0.y, Pf0.z, Pf1.w));
  566. T n0101 = dot(g0101, detail::tvec4<T>(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
  567. T n1101 = dot(g1101, detail::tvec4<T>(Pf1.x, Pf1.y, Pf0.z, Pf1.w));
  568. T n0011 = dot(g0011, detail::tvec4<T>(Pf0.x, Pf0.y, Pf1.z, Pf1.w));
  569. T n1011 = dot(g1011, detail::tvec4<T>(Pf1.x, Pf0.y, Pf1.z, Pf1.w));
  570. T n0111 = dot(g0111, detail::tvec4<T>(Pf0.x, Pf1.y, Pf1.z, Pf1.w));
  571. T n1111 = dot(g1111, Pf1);
  572. detail::tvec4<T> fade_xyzw = fade(Pf0);
  573. detail::tvec4<T> n_0w = mix(detail::tvec4<T>(n0000, n1000, n0100, n1100), detail::tvec4<T>(n0001, n1001, n0101, n1101), fade_xyzw.w);
  574. detail::tvec4<T> n_1w = mix(detail::tvec4<T>(n0010, n1010, n0110, n1110), detail::tvec4<T>(n0011, n1011, n0111, n1111), fade_xyzw.w);
  575. detail::tvec4<T> n_zw = mix(n_0w, n_1w, fade_xyzw.z);
  576. detail::tvec2<T> n_yzw = mix(detail::tvec2<T>(n_zw.x, n_zw.y), detail::tvec2<T>(n_zw.z, n_zw.w), fade_xyzw.y);
  577. T n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x);
  578. return T(2.2) * n_xyzw;
  579. }
  580. template <typename T>
  581. GLM_FUNC_QUALIFIER T simplex(glm::detail::tvec2<T> const & v)
  582. {
  583. detail::tvec4<T> const C = detail::tvec4<T>(
  584. T( 0.211324865405187), // (3.0 - sqrt(3.0)) / 6.0
  585. T( 0.366025403784439), // 0.5 * (sqrt(3.0) - 1.0)
  586. T(-0.577350269189626), // -1.0 + 2.0 * C.x
  587. T( 0.024390243902439)); // 1.0 / 41.0
  588. // First corner
  589. detail::tvec2<T> i = floor(v + dot(v, detail::tvec2<T>(C[1])));
  590. detail::tvec2<T> x0 = v - i + dot(i, detail::tvec2<T>(C[0]));
  591. // Other corners
  592. //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
  593. //i1.y = 1.0 - i1.x;
  594. detail::tvec2<T> i1 = (x0.x > x0.y) ? detail::tvec2<T>(1, 0) : detail::tvec2<T>(0, 1);
  595. // x0 = x0 - 0.0 + 0.0 * C.xx ;
  596. // x1 = x0 - i1 + 1.0 * C.xx ;
  597. // x2 = x0 - 1.0 + 2.0 * C.xx ;
  598. detail::tvec4<T> x12 = detail::tvec4<T>(x0.x, x0.y, x0.x, x0.y) + detail::tvec4<T>(C.x, C.x, C.z, C.z);
  599. x12 = detail::tvec4<T>(detail::tvec2<T>(x12) - i1, x12.z, x12.w);
  600. // Permutations
  601. i = mod(i, T(289)); // Avoid truncation effects in permutation
  602. detail::tvec3<T> p = permute(
  603. permute(i.y + detail::tvec3<T>(T(0), i1.y, T(1)))
  604. + i.x + detail::tvec3<T>(T(0), i1.x, T(1)));
  605. detail::tvec3<T> m = max(T(0.5) - detail::tvec3<T>(
  606. dot(x0, x0),
  607. dot(detail::tvec2<T>(x12.x, x12.y), detail::tvec2<T>(x12.x, x12.y)),
  608. dot(detail::tvec2<T>(x12.z, x12.w), detail::tvec2<T>(x12.z, x12.w))), T(0));
  609. m = m * m ;
  610. m = m * m ;
  611. // Gradients: 41 points uniformly over a line, mapped onto a diamond.
  612. // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
  613. detail::tvec3<T> x = T(2) * fract(p * C.w) - T(1);
  614. detail::tvec3<T> h = abs(x) - T(0.5);
  615. detail::tvec3<T> ox = floor(x + T(0.5));
  616. detail::tvec3<T> a0 = x - ox;
  617. // Normalise gradients implicitly by scaling m
  618. // Inlined for speed: m *= taylorInvSqrt( a0*a0 + h*h );
  619. m *= T(1.79284291400159) - T(0.85373472095314) * (a0 * a0 + h * h);
  620. // Compute final noise value at P
  621. detail::tvec3<T> g;
  622. g.x = a0.x * x0.x + h.x * x0.y;
  623. //g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  624. g.y = a0.y * x12.x + h.y * x12.y;
  625. g.z = a0.z * x12.z + h.z * x12.w;
  626. return T(130) * dot(m, g);
  627. }
  628. template <typename T>
  629. GLM_FUNC_QUALIFIER T simplex(detail::tvec3<T> const & v)
  630. {
  631. detail::tvec2<T> const C(1.0 / 6.0, 1.0 / 3.0);
  632. detail::tvec4<T> const D(0.0, 0.5, 1.0, 2.0);
  633. // First corner
  634. detail::tvec3<T> i(floor(v + dot(v, detail::tvec3<T>(C.y))));
  635. detail::tvec3<T> x0(v - i + dot(i, detail::tvec3<T>(C.x)));
  636. // Other corners
  637. detail::tvec3<T> g(step(detail::tvec3<T>(x0.y, x0.z, x0.x), x0));
  638. detail::tvec3<T> l(T(1) - g);
  639. detail::tvec3<T> i1(min(g, detail::tvec3<T>(l.z, l.x, l.y)));
  640. detail::tvec3<T> i2(max(g, detail::tvec3<T>(l.z, l.x, l.y)));
  641. // x0 = x0 - 0.0 + 0.0 * C.xxx;
  642. // x1 = x0 - i1 + 1.0 * C.xxx;
  643. // x2 = x0 - i2 + 2.0 * C.xxx;
  644. // x3 = x0 - 1.0 + 3.0 * C.xxx;
  645. detail::tvec3<T> x1(x0 - i1 + C.x);
  646. detail::tvec3<T> x2(x0 - i2 + C.y); // 2.0*C.x = 1/3 = C.y
  647. detail::tvec3<T> x3(x0 - D.y); // -1.0+3.0*C.x = -0.5 = -D.y
  648. // Permutations
  649. i = mod289(i);
  650. detail::tvec4<T> p(permute(permute(permute(
  651. i.z + detail::tvec4<T>(T(0), i1.z, i2.z, T(1))) +
  652. i.y + detail::tvec4<T>(T(0), i1.y, i2.y, T(1))) +
  653. i.x + detail::tvec4<T>(T(0), i1.x, i2.x, T(1))));
  654. // Gradients: 7x7 points over a square, mapped onto an octahedron.
  655. // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
  656. T n_ = T(0.142857142857); // 1.0/7.0
  657. detail::tvec3<T> ns(n_ * detail::tvec3<T>(D.w, D.y, D.z) - detail::tvec3<T>(D.x, D.z, D.x));
  658. detail::tvec4<T> j(p - T(49) * floor(p * ns.z * ns.z)); // mod(p,7*7)
  659. detail::tvec4<T> x_(floor(j * ns.z));
  660. detail::tvec4<T> y_(floor(j - T(7) * x_)); // mod(j,N)
  661. detail::tvec4<T> x(x_ * ns.x + ns.y);
  662. detail::tvec4<T> y(y_ * ns.x + ns.y);
  663. detail::tvec4<T> h(T(1) - abs(x) - abs(y));
  664. detail::tvec4<T> b0(x.x, x.y, y.x, y.y);
  665. detail::tvec4<T> b1(x.z, x.w, y.z, y.w);
  666. // vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
  667. // vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
  668. detail::tvec4<T> s0(floor(b0) * T(2) + T(1));
  669. detail::tvec4<T> s1(floor(b1) * T(2) + T(1));
  670. detail::tvec4<T> sh(-step(h, detail::tvec4<T>(0.0)));
  671. detail::tvec4<T> a0 = detail::tvec4<T>(b0.x, b0.z, b0.y, b0.w) + detail::tvec4<T>(s0.x, s0.z, s0.y, s0.w) * detail::tvec4<T>(sh.x, sh.x, sh.y, sh.y);
  672. detail::tvec4<T> a1 = detail::tvec4<T>(b1.x, b1.z, b1.y, b1.w) + detail::tvec4<T>(s1.x, s1.z, s1.y, s1.w) * detail::tvec4<T>(sh.z, sh.z, sh.w, sh.w);
  673. detail::tvec3<T> p0(a0.x, a0.y, h.x);
  674. detail::tvec3<T> p1(a0.z, a0.w, h.y);
  675. detail::tvec3<T> p2(a1.x, a1.y, h.z);
  676. detail::tvec3<T> p3(a1.z, a1.w, h.w);
  677. // Normalise gradients
  678. detail::tvec4<T> norm = taylorInvSqrt(detail::tvec4<T>(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
  679. p0 *= norm.x;
  680. p1 *= norm.y;
  681. p2 *= norm.z;
  682. p3 *= norm.w;
  683. // Mix final noise value
  684. detail::tvec4<T> m = max(T(0.6) - detail::tvec4<T>(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), T(0));
  685. m = m * m;
  686. return T(42) * dot(m * m, detail::tvec4<T>(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));
  687. }
  688. template <typename T>
  689. GLM_FUNC_QUALIFIER T simplex(detail::tvec4<T> const & v)
  690. {
  691. detail::tvec4<T> const C(
  692. 0.138196601125011, // (5 - sqrt(5))/20 G4
  693. 0.276393202250021, // 2 * G4
  694. 0.414589803375032, // 3 * G4
  695. -0.447213595499958); // -1 + 4 * G4
  696. // (sqrt(5) - 1)/4 = F4, used once below
  697. T const F4 = T(0.309016994374947451);
  698. // First corner
  699. detail::tvec4<T> i = floor(v + dot(v, vec4(F4)));
  700. detail::tvec4<T> x0 = v - i + dot(i, vec4(C.x));
  701. // Other corners
  702. // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
  703. detail::tvec4<T> i0;
  704. detail::tvec3<T> isX = step(detail::tvec3<T>(x0.y, x0.z, x0.w), detail::tvec3<T>(x0.x));
  705. detail::tvec3<T> isYZ = step(detail::tvec3<T>(x0.z, x0.w, x0.w), detail::tvec3<T>(x0.y, x0.y, x0.z));
  706. // i0.x = dot(isX, vec3(1.0));
  707. //i0.x = isX.x + isX.y + isX.z;
  708. //i0.yzw = T(1) - isX;
  709. i0 = detail::tvec4<T>(isX.x + isX.y + isX.z, T(1) - isX);
  710. // i0.y += dot(isYZ.xy, vec2(1.0));
  711. i0.y += isYZ.x + isYZ.y;
  712. //i0.zw += 1.0 - detail::tvec2<T>(isYZ.x, isYZ.y);
  713. i0.z += T(1) - isYZ.x;
  714. i0.w += T(1) - isYZ.y;
  715. i0.z += isYZ.z;
  716. i0.w += T(1) - isYZ.z;
  717. // i0 now contains the unique values 0,1,2,3 in each channel
  718. detail::tvec4<T> i3 = clamp(i0, 0.0, 1.0);
  719. detail::tvec4<T> i2 = clamp(i0 - 1.0, 0.0, 1.0);
  720. detail::tvec4<T> i1 = clamp(i0 - 2.0, 0.0, 1.0);
  721. // x0 = x0 - 0.0 + 0.0 * C.xxxx
  722. // x1 = x0 - i1 + 0.0 * C.xxxx
  723. // x2 = x0 - i2 + 0.0 * C.xxxx
  724. // x3 = x0 - i3 + 0.0 * C.xxxx
  725. // x4 = x0 - 1.0 + 4.0 * C.xxxx
  726. detail::tvec4<T> x1 = x0 - i1 + C.x;
  727. detail::tvec4<T> x2 = x0 - i2 + C.y;
  728. detail::tvec4<T> x3 = x0 - i3 + C.z;
  729. detail::tvec4<T> x4 = x0 + C.w;
  730. // Permutations
  731. i = mod(i, T(289));
  732. T j0 = permute(permute(permute(permute(i.w) + i.z) + i.y) + i.x);
  733. detail::tvec4<T> j1 = permute(permute(permute(permute(
  734. i.w + detail::tvec4<T>(i1.w, i2.w, i3.w, T(1)))
  735. + i.z + detail::tvec4<T>(i1.z, i2.z, i3.z, T(1)))
  736. + i.y + detail::tvec4<T>(i1.y, i2.y, i3.y, T(1)))
  737. + i.x + detail::tvec4<T>(i1.x, i2.x, i3.x, T(1)));
  738. // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
  739. // 7*7*6 = 294, which is close to the ring size 17*17 = 289.
  740. detail::tvec4<T> ip = detail::tvec4<T>(T(1) / T(294), T(1) / T(49), T(1) / T(7), T(0));
  741. detail::tvec4<T> p0 = grad4(j0, ip);
  742. detail::tvec4<T> p1 = grad4(j1.x, ip);
  743. detail::tvec4<T> p2 = grad4(j1.y, ip);
  744. detail::tvec4<T> p3 = grad4(j1.z, ip);
  745. detail::tvec4<T> p4 = grad4(j1.w, ip);
  746. // Normalise gradients
  747. detail::tvec4<T> norm = taylorInvSqrt(detail::tvec4<T>(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
  748. p0 *= norm.x;
  749. p1 *= norm.y;
  750. p2 *= norm.z;
  751. p3 *= norm.w;
  752. p4 *= taylorInvSqrt(dot(p4, p4));
  753. // Mix contributions from the five corners
  754. detail::tvec3<T> m0 = max(T(0.6) - detail::tvec3<T>(dot(x0, x0), dot(x1, x1), dot(x2, x2)), T(0));
  755. detail::tvec2<T> m1 = max(T(0.6) - detail::tvec2<T>(dot(x3, x3), dot(x4, x4) ), T(0));
  756. m0 = m0 * m0;
  757. m1 = m1 * m1;
  758. return T(49) *
  759. (dot(m0 * m0, detail::tvec3<T>(dot(p0, x0), dot(p1, x1), dot(p2, x2))) +
  760. dot(m1 * m1, detail::tvec2<T>(dot(p3, x3), dot(p4, x4))));
  761. }
  762. }//namespace glm