func_common.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2011 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2008-03-08
  5. // Updated : 2010-01-26
  6. // Licence : This source is under MIT License
  7. // File : glm/core/func_common.hpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #ifndef glm_core_func_common
  10. #define glm_core_func_common
  11. #include "_fixes.hpp"
  12. namespace glm
  13. {
  14. namespace core{
  15. namespace function{
  16. namespace common{ //!< Define common functions from Section 8.3 of GLSL 1.30.8 specification. Included in glm namespace.
  17. /// \addtogroup core_funcs
  18. ///@{
  19. //! Returns x if x >= 0; otherwise, it returns -x.
  20. //!
  21. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/abs.xml">GLSL abs man page</a>
  22. //! \li GLSL 1.30.08 specification, section 8.3
  23. template <typename genFIType>
  24. genFIType abs(genFIType const & x);
  25. //! Returns 1.0 if x > 0, 0.0 if x == 0, or -1.0 if x < 0.
  26. //!
  27. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/sign.xml">GLSL sign man page</a>
  28. //! \li GLSL 1.30.08 specification, section 8.3
  29. template <typename genFIType>
  30. genFIType sign(genFIType const & x);
  31. //! Returns a value equal to the nearest integer that is less then or equal to x.
  32. //!
  33. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floor.xml">GLSL floor man page</a>
  34. //! \li GLSL 1.30.08 specification, section 8.3
  35. template <typename genType>
  36. genType floor(genType const & x);
  37. //! Returns a value equal to the nearest integer to x
  38. //! whose absolute value is not larger than the absolute value of x.
  39. //!
  40. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/trunc.xml">GLSL trunc man page</a>
  41. //! \li GLSL 1.30.08 specification, section 8.3
  42. template <typename genType>
  43. genType trunc(genType const & x);
  44. //! Returns a value equal to the nearest integer to x.
  45. //! The fraction 0.5 will round in a direction chosen by the
  46. //! implementation, presumably the direction that is fastest.
  47. //! This includes the possibility that round(x) returns the
  48. //! same value as roundEven(x) for all values of x.
  49. //!
  50. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/round.xml">GLSL round man page</a>
  51. //! \li GLSL 1.30.08 specification, section 8.3
  52. template <typename genType>
  53. genType round(genType const & x);
  54. //! Returns a value equal to the nearest integer to x.
  55. //! A fractional part of 0.5 will round toward the nearest even
  56. //! integer. (Both 3.5 and 4.5 for x will return 4.0.)
  57. //!
  58. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/roundEven.xml">GLSL roundEven man page</a>
  59. //! \li GLSL 1.30.08 specification, section 8.3
  60. template <typename genType>
  61. genType roundEven(genType const & x);
  62. //! Returns a value equal to the nearest integer
  63. //! that is greater than or equal to x.
  64. //!
  65. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/ceil.xml">GLSL ceil man page</a>
  66. //! \li GLSL 1.30.08 specification, section 8.3
  67. template <typename genType>
  68. genType ceil(genType const & x);
  69. //! Return x - floor(x).
  70. //!
  71. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/fract.xml">GLSL fract man page</a>
  72. //! \li GLSL 1.30.08 specification, section 8.3
  73. template <typename genType>
  74. genType fract(genType const & x);
  75. //! Modulus. Returns x - y * floor(x / y)
  76. //! for each component in x using the floating point value y.
  77. //!
  78. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml">GLSL mod man page</a>
  79. //! \li GLSL 1.30.08 specification, section 8.3
  80. template <typename genType>
  81. genType mod(
  82. genType const & x,
  83. genType const & y);
  84. //! Modulus. Returns x - y * floor(x / y)
  85. //! for each component in x using the floating point value y.
  86. //!
  87. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mod.xml">GLSL mod man page</a>
  88. //! \li GLSL 1.30.08 specification, section 8.3
  89. template <typename genType>
  90. genType mod(
  91. genType const & x,
  92. typename genType::value_type const & y);
  93. //! Returns the fractional part of x and sets i to the integer
  94. //! part (as a whole number floating point value). Both the
  95. //! return value and the output parameter will have the same
  96. //! sign as x.
  97. //!
  98. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/modf.xml">GLSL modf man page</a>
  99. //! \li GLSL 1.30.08 specification, section 8.3
  100. template <typename genType>
  101. genType modf(
  102. genType const & x,
  103. genType & i);
  104. //! Returns y if y < x; otherwise, it returns x.
  105. //!
  106. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/min.xml">GLSL min man page</a>
  107. //! \li GLSL 1.30.08 specification, section 8.3
  108. template <typename genType>
  109. genType min(
  110. genType const & x,
  111. genType const & y);
  112. template <typename genType>
  113. genType min(
  114. genType const & x,
  115. typename genType::value_type const & y);
  116. //! Returns y if x < y; otherwise, it returns x.
  117. //!
  118. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/max.xml">GLSL max man page</a>
  119. //! \li GLSL 1.30.08 specification, section 8.3
  120. template <typename genType>
  121. genType max(
  122. genType const & x,
  123. genType const & y);
  124. template <typename genType>
  125. genType max(
  126. genType const & x,
  127. typename genType::value_type const & y);
  128. //! Returns min(max(x, minVal), maxVal) for each component in x
  129. //! using the floating-point values minVal and maxVal.
  130. //!
  131. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/clamp.xml">GLSL clamp man page</a>
  132. //! \li GLSL 1.30.08 specification, section 8.3
  133. template <typename genType>
  134. genType clamp(
  135. genType const & x,
  136. genType const & minVal,
  137. genType const & maxVal);
  138. template <typename genType>
  139. genType clamp(
  140. genType const & x,
  141. typename genType::value_type const & minVal,
  142. typename genType::value_type const & maxVal);
  143. //! \return If genTypeU is a floating scalar or vector:
  144. //! Returns x * (1.0 - a) + y * a, i.e., the linear blend of
  145. //! x and y using the floating-point value a.
  146. //! The value for a is not restricted to the range [0, 1].
  147. //!
  148. //! \return If genTypeU is a boolean scalar or vector:
  149. //! Selects which vector each returned component comes
  150. //! from. For a component of a that is false, the
  151. //! corresponding component of x is returned. For a
  152. //! component of a that is true, the corresponding
  153. //! component of y is returned. Components of x and y that
  154. //! are not selected are allowed to be invalid floating point
  155. //! values and will have no effect on the results. Thus, this
  156. //! provides different functionality than
  157. //! genType mix(genType x, genType y, genType(a))
  158. //! where a is a Boolean vector.
  159. //!
  160. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/mix.xml">GLSL mix man page</a>
  161. //! \li GLSL 1.30.08 specification, section 8.3
  162. //!
  163. //! \param[in] x Floating point scalar or vector.
  164. //! \param[in] y Floating point scalar or vector.
  165. //! \param[in] a Floating point or boolean scalar or vector.
  166. //!
  167. // \todo Test when 'a' is a boolean.
  168. template <typename genTypeT, typename genTypeU>
  169. genTypeT mix(genTypeT const & x, genTypeT const & y, genTypeU const & a);
  170. //! Returns 0.0 if x < edge, otherwise it returns 1.0.
  171. //!
  172. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/step.xml">GLSL step man page</a>
  173. //! \li GLSL 1.30.08 specification, section 8.3
  174. template <typename genType>
  175. genType step(
  176. genType const & edge,
  177. genType const & x);
  178. template <typename genType>
  179. genType step(
  180. typename genType::value_type const & edge,
  181. genType const & x);
  182. //! Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and
  183. //! performs smooth Hermite interpolation between 0 and 1
  184. //! when edge0 < x < edge1. This is useful in cases where
  185. //! you would want a threshold function with a smooth
  186. //! transition. This is equivalent to:
  187. //! genType t;
  188. //! t = clamp ((x – edge0) / (edge1 – edge0), 0, 1);
  189. //! return t * t * (3 – 2 * t);
  190. //! Results are undefined if edge0 >= edge1.
  191. //!
  192. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/smoothstep.xml">GLSL smoothstep man page</a>
  193. //! \li GLSL 1.30.08 specification, section 8.3
  194. template <typename genType>
  195. genType smoothstep(
  196. genType const & edge0,
  197. genType const & edge1,
  198. genType const & x);
  199. template <typename genType>
  200. genType smoothstep(
  201. typename genType::value_type const & edge0,
  202. typename genType::value_type const & edge1,
  203. genType const & x);
  204. //! Returns true if x holds a NaN (not a number)
  205. //! representation in the underlying implementation's set of
  206. //! floating point representations. Returns false otherwise,
  207. //! including for implementations with no NaN
  208. //! representations.
  209. //!
  210. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/isnan.xml">GLSL isnan man page</a>
  211. //! \li GLSL 1.30.08 specification, section 8.3
  212. template <typename genType>
  213. typename genType::bool_type isnan(genType const & x);
  214. //! Returns true if x holds a positive infinity or negative
  215. //! infinity representation in the underlying implementation's
  216. //! set of floating point representations. Returns false
  217. //! otherwise, including for implementations with no infinity
  218. //! representations.
  219. //!
  220. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/isinf.xml">GLSL isinf man page</a>
  221. //! \li GLSL 1.30.08 specification, section 8.3
  222. template <typename genType>
  223. typename genType::bool_type isinf(genType const & x);
  224. //! Returns a signed integer value representing
  225. //! the encoding of a floating-point value. The floatingpoint
  226. //! value's bit-level representation is preserved.
  227. //!
  228. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floatBitsToInt.xml">GLSL floatBitsToInt man page</a>
  229. //! \li GLSL 4.00.08 specification, section 8.3
  230. template <typename genType, typename genIType>
  231. genIType floatBitsToInt(genType const & value);
  232. //! Returns a unsigned integer value representing
  233. //! the encoding of a floating-point value. The floatingpoint
  234. //! value's bit-level representation is preserved.
  235. //!
  236. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/floatBitsToUint.xml">GLSL floatBitsToUint man page</a>
  237. //! \li GLSL 4.00.08 specification, section 8.3
  238. template <typename genType, typename genUType>
  239. genUType floatBitsToUint(genType const & value);
  240. //! Returns a floating-point value corresponding to a signed
  241. //! integer encoding of a floating-point value.
  242. //! If an inf or NaN is passed in, it will not signal, and the
  243. //! resulting floating point value is unspecified. Otherwise,
  244. //! the bit-level representation is preserved.
  245. //!
  246. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/intBitsToFloat.xml">GLSL intBitsToFloat man page</a>
  247. //! \li GLSL 4.00.08 specification, section 8.3
  248. template <typename genType, typename genIType>
  249. genType intBitsToFloat(genIType const & value);
  250. //! Returns a floating-point value corresponding to a
  251. //! unsigned integer encoding of a floating-point value.
  252. //! If an inf or NaN is passed in, it will not signal, and the
  253. //! resulting floating point value is unspecified. Otherwise,
  254. //! the bit-level representation is preserved.
  255. //!
  256. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/uintBitsToFloat.xml">GLSL uintBitsToFloat man page</a>
  257. //! \li GLSL 4.00.08 specification, section 8.3
  258. template <typename genType, typename genUType>
  259. genType uintBitsToFloat(genUType const & value);
  260. //! Computes and returns a * b + c.
  261. //!
  262. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/fma.xml">GLSL fma man page</a>
  263. //! \li GLSL 4.00.08 specification, section 8.3
  264. template <typename genType>
  265. genType fma(genType const & a, genType const & b, genType const & c);
  266. //! Splits x into a floating-point significand in the range
  267. //! [0.5, 1.0) and an integral exponent of two, such that:
  268. //! x = significand * exp(2, exponent)
  269. //!
  270. //! The significand is returned by the function and the
  271. //! exponent is returned in the parameter exp. For a
  272. //! floating-point value of zero, the significant and exponent
  273. //! are both zero. For a floating-point value that is an
  274. //! infinity or is not a number, the results are undefined.
  275. //!
  276. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/frexp.xml">GLSL frexp man page</a>
  277. //! \li GLSL 4.00.08 specification, section 8.3
  278. template <typename genType, typename genIType>
  279. genType frexp(genType const & x, genIType & exp);
  280. //! Builds a floating-point number from x and the
  281. //! corresponding integral exponent of two in exp, returning:
  282. //! significand * exp(2, exponent)
  283. //!
  284. //! If this product is too large to be represented in the
  285. //! floating-point type, the result is undefined.
  286. //!
  287. //! \li <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/ldexp.xml">GLSL ldexp man page</a>;
  288. //! \li GLSL 4.00.08 specification, section 8.3
  289. template <typename genType, typename genIType>
  290. genType ldexp(genType const & x, genIType const & exp);
  291. ///@}
  292. }//namespace common
  293. }//namespace function
  294. }//namespace core
  295. using namespace core::function::common;
  296. }//namespace glm
  297. #include "func_common.inl"
  298. #endif//glm_core_func_common