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.

364 lines
13 KiB

4 years ago
  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 core
  24. /// @file glm/core/func_noise.inl
  25. /// @date 2008-08-01 / 2011-09-27
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. namespace glm
  29. {
  30. template <typename T>
  31. GLM_FUNC_QUALIFIER T noise1(T const & x)
  32. {
  33. return noise1(glm::detail::tvec2<T>(x, T(0)));
  34. }
  35. template <typename T>
  36. GLM_FUNC_QUALIFIER glm::detail::tvec2<T> noise2(T const & x)
  37. {
  38. return glm::detail::tvec2<T>(
  39. noise1(x + T(0.0)),
  40. noise1(x + T(1.0)));
  41. }
  42. template <typename T>
  43. GLM_FUNC_QUALIFIER glm::detail::tvec3<T> noise3(T const & x)
  44. {
  45. return glm::detail::tvec3<T>(
  46. noise1(x - T(1.0)),
  47. noise1(x + T(0.0)),
  48. noise1(x + T(1.0)));
  49. }
  50. template <typename T>
  51. GLM_FUNC_QUALIFIER glm::detail::tvec4<T> noise4(T const & x)
  52. {
  53. return glm::detail::tvec4<T>(
  54. noise1(x - T(1.0)),
  55. noise1(x + T(0.0)),
  56. noise1(x + T(1.0)),
  57. noise1(x + T(2.0)));
  58. }
  59. template <typename T>
  60. GLM_FUNC_QUALIFIER T noise1(glm::detail::tvec2<T> const & v)
  61. {
  62. detail::tvec4<T> const C = detail::tvec4<T>(
  63. T( 0.211324865405187), // (3.0 - sqrt(3.0)) / 6.0
  64. T( 0.366025403784439), // 0.5 * (sqrt(3.0) - 1.0)
  65. T(-0.577350269189626), // -1.0 + 2.0 * C.x
  66. T( 0.024390243902439)); // 1.0 / 41.0
  67. // First corner
  68. detail::tvec2<T> i = floor(v + dot(v, detail::tvec2<T>(C[1])));
  69. detail::tvec2<T> x0 = v - i + dot(i, detail::tvec2<T>(C[0]));
  70. // Other corners
  71. //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
  72. //i1.y = 1.0 - i1.x;
  73. detail::tvec2<T> i1 = (x0.x > x0.y) ? detail::tvec2<T>(1, 0) : detail::tvec2<T>(0, 1);
  74. // x0 = x0 - 0.0 + 0.0 * C.xx ;
  75. // x1 = x0 - i1 + 1.0 * C.xx ;
  76. // x2 = x0 - 1.0 + 2.0 * C.xx ;
  77. 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);
  78. x12 = detail::tvec4<T>(detail::tvec2<T>(x12) - i1, x12.z, x12.w);
  79. // Permutations
  80. i = mod(i, T(289)); // Avoid truncation effects in permutation
  81. detail::tvec3<T> p = permute(
  82. permute(i.y + detail::tvec3<T>(T(0), i1.y, T(1)))
  83. + i.x + detail::tvec3<T>(T(0), i1.x, T(1)));
  84. detail::tvec3<T> m = max(T(0.5) - detail::tvec3<T>(
  85. dot(x0, x0),
  86. dot(detail::tvec2<T>(x12.x, x12.y), detail::tvec2<T>(x12.x, x12.y)),
  87. dot(detail::tvec2<T>(x12.z, x12.w), detail::tvec2<T>(x12.z, x12.w))), T(0));
  88. m = m * m ;
  89. m = m * m ;
  90. // Gradients: 41 points uniformly over a line, mapped onto a diamond.
  91. // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
  92. detail::tvec3<T> x = T(2) * fract(p * C.w) - T(1);
  93. detail::tvec3<T> h = abs(x) - T(0.5);
  94. detail::tvec3<T> ox = floor(x + T(0.5));
  95. detail::tvec3<T> a0 = x - ox;
  96. // Normalise gradients implicitly by scaling m
  97. // Inlined for speed: m *= taylorInvSqrt( a0*a0 + h*h );
  98. m *= T(1.79284291400159) - T(0.85373472095314) * (a0 * a0 + h * h);
  99. // Compute final noise value at P
  100. detail::tvec3<T> g;
  101. g.x = a0.x * x0.x + h.x * x0.y;
  102. //g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  103. g.y = a0.y * x12.x + h.y * x12.y;
  104. g.z = a0.z * x12.z + h.z * x12.w;
  105. return T(130) * dot(m, g);
  106. }
  107. template <typename T>
  108. GLM_FUNC_QUALIFIER T noise1(detail::tvec3<T> const & v)
  109. {
  110. detail::tvec2<T> const C(1.0 / 6.0, 1.0 / 3.0);
  111. detail::tvec4<T> const D(0.0, 0.5, 1.0, 2.0);
  112. // First corner
  113. detail::tvec3<T> i(floor(v + dot(v, detail::tvec3<T>(C.y))));
  114. detail::tvec3<T> x0(v - i + dot(i, detail::tvec3<T>(C.x)));
  115. // Other corners
  116. detail::tvec3<T> g(step(detail::tvec3<T>(x0.y, x0.z, x0.x), x0));
  117. detail::tvec3<T> l(T(1) - g);
  118. detail::tvec3<T> i1(min(g, detail::tvec3<T>(l.z, l.x, l.y)));
  119. detail::tvec3<T> i2(max(g, detail::tvec3<T>(l.z, l.x, l.y)));
  120. // x0 = x0 - 0.0 + 0.0 * C.xxx;
  121. // x1 = x0 - i1 + 1.0 * C.xxx;
  122. // x2 = x0 - i2 + 2.0 * C.xxx;
  123. // x3 = x0 - 1.0 + 3.0 * C.xxx;
  124. detail::tvec3<T> x1(x0 - i1 + C.x);
  125. detail::tvec3<T> x2(x0 - i2 + C.y); // 2.0*C.x = 1/3 = C.y
  126. detail::tvec3<T> x3(x0 - D.y); // -1.0+3.0*C.x = -0.5 = -D.y
  127. // Permutations
  128. i = mod289(i);
  129. detail::tvec4<T> p(permute(permute(permute(
  130. i.z + detail::tvec4<T>(T(0), i1.z, i2.z, T(1))) +
  131. i.y + detail::tvec4<T>(T(0), i1.y, i2.y, T(1))) +
  132. i.x + detail::tvec4<T>(T(0), i1.x, i2.x, T(1))));
  133. // Gradients: 7x7 points over a square, mapped onto an octahedron.
  134. // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
  135. T n_ = T(0.142857142857); // 1.0/7.0
  136. detail::tvec3<T> ns(n_ * detail::tvec3<T>(D.w, D.y, D.z) - detail::tvec3<T>(D.x, D.z, D.x));
  137. detail::tvec4<T> j(p - T(49) * floor(p * ns.z * ns.z)); // mod(p,7*7)
  138. detail::tvec4<T> x_(floor(j * ns.z));
  139. detail::tvec4<T> y_(floor(j - T(7) * x_)); // mod(j,N)
  140. detail::tvec4<T> x(x_ * ns.x + ns.y);
  141. detail::tvec4<T> y(y_ * ns.x + ns.y);
  142. detail::tvec4<T> h(T(1) - abs(x) - abs(y));
  143. detail::tvec4<T> b0(x.x, x.y, y.x, y.y);
  144. detail::tvec4<T> b1(x.z, x.w, y.z, y.w);
  145. // vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
  146. // vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
  147. detail::tvec4<T> s0(floor(b0) * T(2) + T(1));
  148. detail::tvec4<T> s1(floor(b1) * T(2) + T(1));
  149. detail::tvec4<T> sh(-step(h, detail::tvec4<T>(0.0)));
  150. 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);
  151. 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);
  152. detail::tvec3<T> p0(a0.x, a0.y, h.x);
  153. detail::tvec3<T> p1(a0.z, a0.w, h.y);
  154. detail::tvec3<T> p2(a1.x, a1.y, h.z);
  155. detail::tvec3<T> p3(a1.z, a1.w, h.w);
  156. // Normalise gradients
  157. detail::tvec4<T> norm = taylorInvSqrt(detail::tvec4<T>(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
  158. p0 *= norm.x;
  159. p1 *= norm.y;
  160. p2 *= norm.z;
  161. p3 *= norm.w;
  162. // Mix final noise value
  163. 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));
  164. m = m * m;
  165. return T(42) * dot(m * m, detail::tvec4<T>(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));
  166. }
  167. template <typename T>
  168. GLM_FUNC_QUALIFIER T noise1(detail::tvec4<T> const & v)
  169. {
  170. detail::tvec4<T> const C(
  171. 0.138196601125011, // (5 - sqrt(5))/20 G4
  172. 0.276393202250021, // 2 * G4
  173. 0.414589803375032, // 3 * G4
  174. -0.447213595499958); // -1 + 4 * G4
  175. // (sqrt(5) - 1)/4 = F4, used once below
  176. T const F4 = T(0.309016994374947451);
  177. // First corner
  178. detail::tvec4<T> i = floor(v + dot(v, vec4(F4)));
  179. detail::tvec4<T> x0 = v - i + dot(i, vec4(C.x));
  180. // Other corners
  181. // Rank sorting originally contributed by Bill Licea-Kane, AMD (formerly ATI)
  182. detail::tvec4<T> i0;
  183. detail::tvec3<T> isX = step(detail::tvec3<T>(x0.y, x0.z, x0.w), detail::tvec3<T>(x0.x));
  184. detail::tvec3<T> isYZ = step(detail::tvec3<T>(x0.z, x0.w, x0.w), detail::tvec3<T>(x0.y, x0.y, x0.z));
  185. // i0.x = dot(isX, vec3(1.0));
  186. //i0.x = isX.x + isX.y + isX.z;
  187. //i0.yzw = T(1) - isX;
  188. i0 = detail::tvec4<T>(isX.x + isX.y + isX.z, T(1) - isX);
  189. // i0.y += dot(isYZ.xy, vec2(1.0));
  190. i0.y += isYZ.x + isYZ.y;
  191. //i0.zw += 1.0 - detail::tvec2<T>(isYZ.x, isYZ.y);
  192. i0.z += T(1) - isYZ.x;
  193. i0.w += T(1) - isYZ.y;
  194. i0.z += isYZ.z;
  195. i0.w += T(1) - isYZ.z;
  196. // i0 now contains the unique values 0,1,2,3 in each channel
  197. detail::tvec4<T> i3 = clamp(i0, 0.0, 1.0);
  198. detail::tvec4<T> i2 = clamp(i0 - 1.0, 0.0, 1.0);
  199. detail::tvec4<T> i1 = clamp(i0 - 2.0, 0.0, 1.0);
  200. // x0 = x0 - 0.0 + 0.0 * C.xxxx
  201. // x1 = x0 - i1 + 0.0 * C.xxxx
  202. // x2 = x0 - i2 + 0.0 * C.xxxx
  203. // x3 = x0 - i3 + 0.0 * C.xxxx
  204. // x4 = x0 - 1.0 + 4.0 * C.xxxx
  205. detail::tvec4<T> x1 = x0 - i1 + C.x;
  206. detail::tvec4<T> x2 = x0 - i2 + C.y;
  207. detail::tvec4<T> x3 = x0 - i3 + C.z;
  208. detail::tvec4<T> x4 = x0 + C.w;
  209. // Permutations
  210. i = mod(i, T(289));
  211. T j0 = permute(permute(permute(permute(i.w) + i.z) + i.y) + i.x);
  212. detail::tvec4<T> j1 = permute(permute(permute(permute(
  213. i.w + detail::tvec4<T>(i1.w, i2.w, i3.w, T(1)))
  214. + i.z + detail::tvec4<T>(i1.z, i2.z, i3.z, T(1)))
  215. + i.y + detail::tvec4<T>(i1.y, i2.y, i3.y, T(1)))
  216. + i.x + detail::tvec4<T>(i1.x, i2.x, i3.x, T(1)));
  217. // Gradients: 7x7x6 points over a cube, mapped onto a 4-cross polytope
  218. // 7*7*6 = 294, which is close to the ring size 17*17 = 289.
  219. detail::tvec4<T> ip = detail::tvec4<T>(T(1) / T(294), T(1) / T(49), T(1) / T(7), T(0));
  220. detail::tvec4<T> p0 = grad4(j0, ip);
  221. detail::tvec4<T> p1 = grad4(j1.x, ip);
  222. detail::tvec4<T> p2 = grad4(j1.y, ip);
  223. detail::tvec4<T> p3 = grad4(j1.z, ip);
  224. detail::tvec4<T> p4 = grad4(j1.w, ip);
  225. // Normalise gradients
  226. detail::tvec4<T> norm = taylorInvSqrt(detail::tvec4<T>(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
  227. p0 *= norm.x;
  228. p1 *= norm.y;
  229. p2 *= norm.z;
  230. p3 *= norm.w;
  231. p4 *= taylorInvSqrt(dot(p4, p4));
  232. // Mix contributions from the five corners
  233. detail::tvec3<T> m0 = max(T(0.6) - detail::tvec3<T>(dot(x0, x0), dot(x1, x1), dot(x2, x2)), T(0));
  234. detail::tvec2<T> m1 = max(T(0.6) - detail::tvec2<T>(dot(x3, x3), dot(x4, x4) ), T(0));
  235. m0 = m0 * m0;
  236. m1 = m1 * m1;
  237. return T(49) *
  238. (dot(m0 * m0, detail::tvec3<T>(dot(p0, x0), dot(p1, x1), dot(p2, x2))) +
  239. dot(m1 * m1, detail::tvec2<T>(dot(p3, x3), dot(p4, x4))));
  240. }
  241. template <typename T>
  242. GLM_FUNC_QUALIFIER glm::detail::tvec2<T> noise2(glm::detail::tvec2<T> const & x)
  243. {
  244. return glm::detail::tvec2<T>(
  245. noise1(x + glm::detail::tvec2<T>(0.0)),
  246. noise1(glm::detail::tvec2<T>(0.0) - x));
  247. }
  248. template <typename T>
  249. GLM_FUNC_QUALIFIER glm::detail::tvec2<T> noise2(glm::detail::tvec3<T> const & x)
  250. {
  251. return glm::detail::tvec2<T>(
  252. noise1(x + glm::detail::tvec3<T>(0.0)),
  253. noise1(glm::detail::tvec3<T>(0.0) - x));
  254. }
  255. template <typename T>
  256. GLM_FUNC_QUALIFIER glm::detail::tvec2<T> noise2(glm::detail::tvec4<T> const & x)
  257. {
  258. return glm::detail::tvec2<T>(
  259. noise1(x + glm::detail::tvec4<T>(0.0)),
  260. noise1(glm::detail::tvec4<T>(0.0) - x));
  261. }
  262. template <typename T>
  263. GLM_FUNC_QUALIFIER glm::detail::tvec3<T> noise3(glm::detail::tvec2<T> const & x)
  264. {
  265. return glm::detail::tvec3<T>(
  266. noise1(x - glm::detail::tvec2<T>(1.0)),
  267. noise1(x + glm::detail::tvec2<T>(0.0)),
  268. noise1(x + glm::detail::tvec2<T>(1.0)));
  269. }
  270. template <typename T>
  271. GLM_FUNC_QUALIFIER glm::detail::tvec3<T> noise3(glm::detail::tvec3<T> const & x)
  272. {
  273. return glm::detail::tvec3<T>(
  274. noise1(x - glm::detail::tvec3<T>(1.0)),
  275. noise1(x + glm::detail::tvec3<T>(0.0)),
  276. noise1(x + glm::detail::tvec3<T>(1.0)));
  277. }
  278. template <typename T>
  279. GLM_FUNC_QUALIFIER glm::detail::tvec3<T> noise3(glm::detail::tvec4<T> const & x)
  280. {
  281. return glm::detail::tvec3<T>(
  282. noise1(x - glm::detail::tvec4<T>(1.0)),
  283. noise1(x + glm::detail::tvec4<T>(0.0)),
  284. noise1(x + glm::detail::tvec4<T>(1.0)));
  285. }
  286. template <typename T>
  287. GLM_FUNC_QUALIFIER glm::detail::tvec4<T> noise4(glm::detail::tvec2<T> const & x)
  288. {
  289. return glm::detail::tvec4<T>(
  290. noise1(x - glm::detail::tvec2<T>(1.0)),
  291. noise1(x + glm::detail::tvec2<T>(0.0)),
  292. noise1(x + glm::detail::tvec2<T>(1.0)),
  293. noise1(x + glm::detail::tvec2<T>(2.0)));
  294. }
  295. template <typename T>
  296. GLM_FUNC_QUALIFIER glm::detail::tvec4<T> noise4(glm::detail::tvec3<T> const & x)
  297. {
  298. return glm::detail::tvec4<T>(
  299. noise1(x - glm::detail::tvec3<T>(1.0)),
  300. noise1(x + glm::detail::tvec3<T>(0.0)),
  301. noise1(x + glm::detail::tvec3<T>(1.0)),
  302. noise1(x + glm::detail::tvec3<T>(2.0)));
  303. }
  304. template <typename T>
  305. GLM_FUNC_QUALIFIER glm::detail::tvec4<T> noise4(glm::detail::tvec4<T> const & x)
  306. {
  307. return glm::detail::tvec4<T>(
  308. noise1(x - glm::detail::tvec4<T>(1.0)),
  309. noise1(x + glm::detail::tvec4<T>(0.0)),
  310. noise1(x + glm::detail::tvec4<T>(1.0)),
  311. noise1(x + glm::detail::tvec4<T>(2.0)));
  312. }
  313. }//namespace glm