Platformer in OpenGL
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.

2346 lines
90 KiB

5 years ago
  1. ![Alt](./doc/manual/logo-mini.png "GLM Logo")
  2. # GLM 0.9.9 Manual
  3. ![Alt](./doc/manual/g-truc.png "G-Truc Logo")
  4. ---
  5. <div style="page-break-after: always;"> </div>
  6. ## Table of Contents
  7. + [0. Licenses](#section0)
  8. + [1. Getting started](#section1)
  9. + [1.1. Using global headers](#section1_1)
  10. + [1.2. Using separated headers](#section1_2)
  11. + [1.3. Using extension headers](#section1_3)
  12. + [1.4. Dependencies](#section1_4)
  13. + [2. Preprocessor configurations](#section2)
  14. + [2.1. GLM\_FORCE\_MESSAGES: Platform auto detection and default configuration](#section2_1)
  15. + [2.2. GLM\_FORCE\_PLATFORM\_UNKNOWN: Force GLM to no detect the build platform](#section2_2)
  16. + [2.3. GLM\_FORCE\_COMPILER\_UNKNOWN: Force GLM to no detect the C++ compiler](#section2_3)
  17. + [2.4. GLM\_FORCE\_ARCH\_UNKNOWN: Force GLM to no detect the build architecture](#section2_4)
  18. + [2.5. GLM\_FORCE\_CXX\_UNKNOWN: Force GLM to no detect the C++ standard](#section2_5)
  19. + [2.6. GLM\_FORCE\_CXX**: C++ language detection](#section2_6)
  20. + [2.7. GLM\_FORCE\_EXPLICIT\_CTOR: Requiring explicit conversions](#section2_7)
  21. + [2.8. GLM\_FORCE\_INLINE: Force inline](#section2_8)
  22. + [2.9. GLM\_FORCE\_DEFAULT\_ALIGNED_GENTYPES: Force GLM to use aligned types by default](#section2_9)
  23. + [2.10. GLM\_FORCE\_SIMD\_**: Using SIMD optimizations](#section2_10)
  24. + [2.11. GLM\_FORCE\_PRECISION\_**: Default precision](#section2_11)
  25. + [2.12. GLM\_FORCE\_SINGLE\_ONLY: Removed explicit 64-bits floating point types](#section2_12)
  26. + [2.13. GLM\_FORCE\_SWIZZLE: Enable swizzle operators](#section2_13)
  27. + [2.14. GLM\_FORCE\_XYZW\_ONLY: Only exposes x, y, z and w components](#section2_14)
  28. + [2.15. GLM\_FORCE\_LEFT\_HANDED: Force left handed coordinate system](#section2_15)
  29. + [2.16. GLM\_FORCE\_DEPTH\_ZERO\_TO\_ONE: Force the use of a clip space between 0 to 1](#section2_16)
  30. + [2.17. GLM\_FORCE\_SIZE\_T\_LENGTH: Vector and matrix static size type](#section2_17)
  31. + [2.18. GLM\_FORCE\_UNRESTRICTED\_GENTYPE: Removing genType restriction](#section2_18)
  32. + [3. Stable extensions](#section3)
  33. + [3.1. Scalar types](#section3_1)
  34. + [3.2. Scalar functions](#section3_2)
  35. + [3.3. Vector types](#section3_3)
  36. + [3.4. Vector types with precision qualifiers](#section3_4)
  37. + [3.5. Vector functions](#section3_5)
  38. + [3.6. Matrix types](#section3_6)
  39. + [3.7. Matrix types with precision qualifiers](#section3_7)
  40. + [3.8. Matrix functions](#section3_8)
  41. + [3.9. Quaternion types](#section3_9)
  42. + [3.10. Quaternion types with precision qualifiers](#section3_10)
  43. + [3.11. Quaternion functions](#section3_11)
  44. + [4. Recommended extensions](#section4)
  45. + [4.1. GLM_GTC_bitfield](#section4_1)
  46. + [4.2. GLM_GTC_color_space](#section4_2)
  47. + [4.3. GLM_GTC_constants](#section4_3)
  48. + [4.4. GLM_GTC_epsilon](#section4_4)
  49. + [4.5. GLM_GTC_integer](#section4_5)
  50. + [4.6. GLM_GTC_matrix_access](#section4_6)
  51. + [4.7. GLM_GTC_matrix_integer](#section4_7)
  52. + [4.8. GLM_GTC_matrix_inverse](#section4_8)
  53. + [4.9. GLM_GTC_matrix_transform](#section4_9)
  54. + [4.10. GLM_GTC_noise](#section4_10)
  55. + [4.11. GLM_GTC_packing](#section4_11)
  56. + [4.12. GLM_GTC_quaternion](#section4_12)
  57. + [4.13. GLM_GTC_random](#section4_13)
  58. + [4.14. GLM_GTC_reciprocal](#section4_14)
  59. + [4.15. GLM_GTC_round](#section4_15)
  60. + [4.16. GLM_GTC_type_alignment](#section4_16)
  61. + [4.17. GLM_GTC_type_precision](#section4_17)
  62. + [4.18. GLM_GTC_type_ptr](#section4_18)
  63. + [4.19. GLM_GTC_ulp](#section4_19)
  64. + [4.20. GLM_GTC_vec1](#section4_20)
  65. + [5. OpenGL interoperability](#section5)
  66. + [5.1. GLM Replacements for deprecated OpenGL functions](#section5_1)
  67. + [5.2. GLM Replacements for GPU functions](#section5_2)
  68. + [6. Known issues](#section6)
  69. + [6.1. Not function](#section6_1)
  70. + [6.2. Precision qualifiers support](#section6_2)
  71. + [7. FAQ](#section7)
  72. + [7.1 Why GLM follows GLSL specification and conventions?](#section7_1)
  73. + [7.2. Does GLM run GLSL programs?](#section7_2)
  74. + [7.3. Does a GLSL compiler build GLM codes?](#section7_3)
  75. + [7.4. Should I use ‘GTX’ extensions?](#section7_4)
  76. + [7.5. Where can I ask my questions?](#section7_5)
  77. + [7.6. Where can I find the documentation of extensions?](#section7_6)
  78. + [7.7. Should I use 'using namespace glm;'?](#section7_7)
  79. + [7.8. Is GLM fast?](#section7_8)
  80. + [7.9. When I build with Visual C++ with /w4 warning level, I have warnings...](#section7_9)
  81. + [7.10. Why some GLM functions can crash because of division by zero?](#section7_10)
  82. + [7.11. What unit for angles us used in GLM?](#section7_11)
  83. + [7.12. Windows headers cause build errors...](#section7_12)
  84. + [7.13. Constant expressions support](#section7_13)
  85. + [8. Code samples](#section8)
  86. + [8.1. Compute a triangle normal](#section8_1)
  87. + [8.2. Matrix transform](#section8_2)
  88. + [8.3. Vector types](#section8_3)
  89. + [8.4. Lighting](#section8_4)
  90. + [9. Contributing to GLM](#section9)
  91. + [9.1. Submitting bug reports](#section9_1)
  92. + [9.2. Contributing to GLM with pull request](#section9_2)
  93. + [9.3. Coding style](#section9_3)
  94. + [10. References](#section10)
  95. + [10.1. OpenGL specifications](#section10_1)
  96. + [10.2. External links](#section10_2)
  97. + [10.3. Projects using GLM](#section10_3)
  98. + [10.4. Tutorials using GLM](#section10_4)
  99. + [10.5. Equivalent for other languages](#section10_5)
  100. + [10.6. Alternatives to GLM](#section10_6)
  101. + [10.7. Acknowledgements](#section10_7)
  102. ---
  103. <div style="page-break-after: always;"> </div>
  104. ## <a name="section0"></a> Licenses
  105. ### The Happy Bunny License (Modified MIT License)
  106. Copyright (c) 2005 - G-Truc Creation
  107. Permission is hereby granted, free of charge, to any person obtaining a
  108. copy of this software and associated documentation files (the
  109. "Software"), to deal in the Software without restriction, including
  110. without limitation the rights to use, copy, modify, merge, publish,
  111. distribute, sublicense, and/or sell copies of the Software, and to
  112. permit persons to whom the Software is furnished to do so, subject to
  113. the following conditions:
  114. The above copyright notice and this permission notice shall be included
  115. in all copies or substantial portions of the Software.
  116. Restrictions: By making use of the Software for military purposes, you
  117. choose to make a Bunny unhappy.
  118. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  119. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  120. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  121. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  122. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  123. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  124. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  125. ![](./doc/manual/frontpage1.png)
  126. ### The MIT License
  127. Copyright (c) 2005 - G-Truc Creation
  128. Permission is hereby granted, free of charge, to any person obtaining a
  129. copy of this software and associated documentation files (the
  130. "Software"), to deal in the Software without restriction, including
  131. without limitation the rights to use, copy, modify, merge, publish,
  132. distribute, sublicense, and/or sell copies of the Software, and to
  133. permit persons to whom the Software is furnished to do so, subject to
  134. the following conditions:
  135. The above copyright notice and this permission notice shall be included
  136. in all copies or substantial portions of the Software.
  137. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  138. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  139. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  140. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  141. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  142. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  143. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  144. ![](./doc/manual/frontpage2.png)
  145. ---
  146. <div style="page-break-after: always;"> </div>
  147. ## <a name="section1"></a> 1. Getting started
  148. ### <a name="section1_1"></a> 1.1. Using global headers
  149. GLM is a header-only library, and thus does not need to be compiled. We can use GLM's implementation of GLSL's mathematics functionality by including the `<glm/glm.hpp>` header:
  150. ```cpp
  151. #include <glm/glm.hpp>
  152. ```
  153. To extend the feature set supported by GLM and keeping the library as close to GLSL as possible, new features are implemented as extensions that can be included thought a separated header:
  154. ```cpp
  155. // Include all GLM core / GLSL features
  156. #include <glm/glm.hpp> // vec2, vec3, mat4, radians
  157. // Include all GLM extensions
  158. #include <glm/ext.hpp> // perspective, translate, rotate
  159. glm::mat4 transform(glm::vec2 const& Orientation, glm::vec3 const& Translate, glm::vec3 const& Up)
  160. {
  161. glm::mat4 Proj = glm::perspective(glm::radians(45.f), 1.33f, 0.1f, 10.f);
  162. glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.f), Translate);
  163. glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Orientation.y, Up);
  164. glm::mat4 View = glm::rotate(ViewRotateX, Orientation.x, Up);
  165. glm::mat4 Model = glm::mat4(1.0f);
  166. return Proj * View * Model;
  167. }
  168. ```
  169. *Note: Including `<glm/glm.hpp>` and `<glm/ext.hpp>` is convenient but pull a lot of code which will significantly increase build time, particularly if these files are included in all source files. We may prefer to use the approaches describe in the two following sections to keep the project build fast.*
  170. ### <a name="section1_2"></a> 1.2. Using separated headers
  171. GLM relies on C++ templates heavily, and may significantly increase compilation times for projects that use it. Hence, user projects could only include the features they actually use. Following is the list of all the core features, based on GLSL specification, headers:
  172. ```cpp
  173. #include <glm/vec2.hpp> // vec2, bvec2, dvec2, ivec2 and uvec2
  174. #include <glm/vec3.hpp> // vec3, bvec3, dvec3, ivec3 and uvec3
  175. #include <glm/vec4.hpp> // vec4, bvec4, dvec4, ivec4 and uvec4
  176. #include <glm/mat2x2.hpp> // mat2, dmat2
  177. #include <glm/mat2x3.hpp> // mat2x3, dmat2x3
  178. #include <glm/mat2x4.hpp> // mat2x4, dmat2x4
  179. #include <glm/mat3x2.hpp> // mat3x2, dmat3x2
  180. #include <glm/mat3x3.hpp> // mat3, dmat3
  181. #include <glm/mat3x4.hpp> // mat3x4, dmat2
  182. #include <glm/mat4x2.hpp> // mat4x2, dmat4x2
  183. #include <glm/mat4x3.hpp> // mat4x3, dmat4x3
  184. #include <glm/mat4x4.hpp> // mat4, dmat4
  185. #include <glm/common.hpp> // all the GLSL common functions: abs, min, mix, isnan, fma, etc.
  186. #include <glm/exponential.hpp> // all the GLSL exponential functions: pow, log, exp2, sqrt, etc.
  187. #include <glm/geometry.hpp> // all the GLSL geometry functions: dot, cross, reflect, etc.
  188. #include <glm/integer.hpp> // all the GLSL integer functions: findMSB, bitfieldExtract, etc.
  189. #include <glm/matrix.hpp> // all the GLSL matrix functions: transpose, inverse, etc.
  190. #include <glm/packing.hpp> // all the GLSL packing functions: packUnorm4x8, unpackHalf2x16, etc.
  191. #include <glm/trigonometric.hpp> // all the GLSL trigonometric functions: radians, cos, asin, etc.
  192. #include <glm/vector_relational.hpp> // all the GLSL vector relational functions: equal, less, etc.
  193. ```
  194. The following is a code sample using separated core headers and an extension:
  195. ```cpp
  196. // Include GLM core features
  197. #include <glm/vec2.hpp> // vec2
  198. #include <glm/vec3.hpp> // vec3
  199. #include <glm/mat4x4.hpp> // mat4
  200. #include <glm/trigonometric.hpp> //radians
  201. // Include GLM extension
  202. #include <glm/ext/matrix_transform.hpp> // perspective, translate, rotate
  203. glm::mat4 transform(glm::vec2 const& Orientation, glm::vec3 const& Translate, glm::vec3 const& Up)
  204. {
  205. glm::mat4 Proj = glm::perspective(glm::radians(45.f), 1.33f, 0.1f, 10.f);
  206. glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.f), Translate);
  207. glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Orientation.y, Up);
  208. glm::mat4 View = glm::rotate(ViewRotateX, Orientation.x, Up);
  209. glm::mat4 Model = glm::mat4(1.0f);
  210. return Proj * View * Model;
  211. }
  212. ```
  213. ### <a name="section1_3"></a> 1.3. Using extension headers
  214. Using GLM through split headers to minimize the project build time:
  215. ```cpp
  216. // Include GLM vector extensions:
  217. #include <glm/ext/vector_float2.hpp> // vec2
  218. #include <glm/ext/vector_float3.hpp> // vec3
  219. #include <glm/ext/vector_trigonometric.hpp> // radians
  220. // Include GLM matrix extensions:
  221. #include <glm/ext/matrix_float4x4.hpp> // mat4
  222. #include <glm/ext/matrix_transform.hpp> // perspective, translate, rotate
  223. glm::mat4 transform(glm::vec2 const& Orientation, glm::vec3 const& Translate, glm::vec3 const& Up)
  224. {
  225. glm::mat4 Proj = glm::perspective(glm::radians(45.f), 1.33f, 0.1f, 10.f);
  226. glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.f), Translate);
  227. glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Orientation.y, Up);
  228. glm::mat4 View = glm::rotate(ViewRotateX, Orientation.x, Up);
  229. glm::mat4 Model = glm::mat4(1.0f);
  230. return Proj * View * Model;
  231. }
  232. ```
  233. ### <a name="section1_4"></a> 1.4. Dependencies
  234. GLM does not depend on external libraries or headers such as `<GL/gl.h>`, [`<GL/glcorearb.h>`](http://www.opengl.org/registry/api/GL/glcorearb.h), `<GLES3/gl3.h>`, `<GL/glu.h>`, or `<windows.h>`.
  235. ---
  236. <div style="page-break-after: always;"> </div>
  237. ## <a name="section2"></a> 2. Preprocessor configurations
  238. ### <a name="section2_1"></a> 2.1. GLM\_FORCE\_MESSAGES: Platform auto detection and default configuration
  239. When included, GLM will first automatically detect the compiler used, the C++ standard supported, the compiler arguments used to configure itself matching the build environment.
  240. For example, if the compiler arguments request AVX code generation, GLM will rely on its code path providing AVX optimizations when available.
  241. We can change GLM configuration using specific C++ preprocessor defines that must be declared before including any GLM headers.
  242. Using `GLM_FORCE_MESSAGES`, GLM will report the configuration as part of the build log.
  243. ```cpp
  244. #define GLM_FORCE_MESSAGES // Or defined when building (e.g. -DGLM_FORCE_SWIZZLE)
  245. #include <glm/glm.hpp>
  246. ```
  247. Example of configuration log generated by `GLM_FORCE_MESSAGES`:
  248. ```cpp
  249. GLM: version 0.9.9.1
  250. GLM: C++ 17 with extensions
  251. GLM: Clang compiler detected
  252. GLM: x86 64 bits with AVX instruction set build target
  253. GLM: Linux platform detected
  254. GLM: GLM_FORCE_SWIZZLE is undefined. swizzling functions or operators are disabled.
  255. GLM: GLM_FORCE_SIZE_T_LENGTH is undefined. .length() returns a glm::length_t, a typedef of int following GLSL.
  256. GLM: GLM_FORCE_UNRESTRICTED_GENTYPE is undefined. Follows strictly GLSL on valid function genTypes.
  257. GLM: GLM_FORCE_DEPTH_ZERO_TO_ONE is undefined. Using negative one to one depth clip space.
  258. GLM: GLM_FORCE_LEFT_HANDED is undefined. Using right handed coordinate system.
  259. ```
  260. The following subsections describe each configurations and defines.
  261. ### <a name="section2_2"></a> 2.2. GLM\_FORCE\_PLATFORM\_UNKNOWN: Force GLM to no detect the build platform
  262. `GLM_FORCE_PLATFORM_UNKNOWN` prevents GLM from detecting the build platform.
  263. ### <a name="section2_3"></a> 2.3. GLM\_FORCE\_COMPILER\_UNKNOWN: Force GLM to no detect the C++ compiler
  264. `GLM_FORCE_COMPILER_UNKNOWN` prevents GLM from detecting the C++ compiler.
  265. ### <a name="section2_4"></a> 2.4. GLM\_FORCE\_ARCH\_UNKNOWN: Force GLM to no detect the build architecture
  266. `GLM_FORCE_ARCH_UNKNOWN` prevents GLM from detecting the build target architechture.
  267. ### <a name="section2_5"></a> 2.5. GLM\_FORCE\_CXX\_UNKNOWN: Force GLM to no detect the C++ standard
  268. `GLM_FORCE_CSS_UNKNOWN` prevents GLM from detecting the C++ compiler standard support.
  269. ### <a name="section2_6"></a> 2.6. GLM\_FORCE\_CXX**: C++ language detection
  270. GLM will automatically take advantage of compilers’ language extensions when enabled. To increase cross platform compatibility and to avoid compiler extensions, a programmer can define `GLM_FORCE_CXX98` before
  271. any inclusion of `<glm/glm.hpp>` to restrict the language feature set C++98:
  272. ```cpp
  273. #define GLM_FORCE_CXX98
  274. #include <glm/glm.hpp>
  275. ```
  276. For C++11, C++14, and C++17 equivalent defines are available:
  277. * `GLM_FORCE_CXX11`
  278. * `GLM_FORCE_CXX14`
  279. * `GLM_FORCE_CXX14`
  280. ```cpp
  281. #define GLM_FORCE_CXX11
  282. #include <glm/glm.hpp>
  283. // If the compiler doesn’t support C++11, compiler errors will happen.
  284. ```
  285. `GLM_FORCE_CXX17` overrides `GLM_FORCE_CXX14`; `GLM_FORCE_CXX14` overrides `GLM_FORCE_CXX11`; and `GLM_FORCE_CXX11` overrides `GLM_FORCE_CXX98` defines.
  286. ### <a name="section2_7"></a> 2.7. GLM\_FORCE\_EXPLICIT\_CTOR: Requiring explicit conversions
  287. GLSL supports implicit conversions of vector and matrix types. For example, an ivec4 can be implicitly converted into `vec4`.
  288. Often, this behaviour is not desirable but following the spirit of the library, this is the default behavior in GLM. However, GLM 0.9.6 introduced the define `GLM_FORCE_EXPLICIT_CTOR` to require explicit conversion for GLM types.
  289. ```cpp
  290. #include <glm/glm.hpp>
  291. void foo()
  292. {
  293. glm::ivec4 a;
  294. ...
  295. glm::vec4 b(a); // Explicit conversion, OK
  296. glm::vec4 c = a; // Implicit conversion, OK
  297. ...
  298. }
  299. ```
  300. With `GLM_FORCE_EXPLICIT_CTOR` define, implicit conversions are not allowed:
  301. ```cpp
  302. #define GLM_FORCE_EXPLICIT_CTOR
  303. #include <glm/glm.hpp>
  304. void foo()
  305. {
  306. glm::ivec4 a;
  307. {
  308. glm::vec4 b(a); // Explicit conversion, OK
  309. glm::vec4 c = a; // Implicit conversion, ERROR
  310. ...
  311. }
  312. ```
  313. ### <a name="section2_8"></a> 2.8. GLM\_FORCE\_INLINE: Force inline
  314. To push further the software performance, a programmer can define `GLM_FORCE_INLINE` before any inclusion of `<glm/glm.hpp>` to force the compiler to inline GLM code.
  315. ```cpp
  316. #define GLM_FORCE_INLINE
  317. #include <glm/glm.hpp>
  318. ```
  319. ### <a name="section2_9"></a> 2.9. GLM\_FORCE\_DEFAULT\_ALIGNED\_GENTYPES: Force GLM to use aligned types by default
  320. Every object type has the property called alignment requirement, which is an integer value (of type `std::size_t`, always a power of 2) representing the number of bytes between successive addresses at which objects of this type can be allocated. The alignment requirement of a type can be queried with alignof or `std::alignment_of`. The pointer alignment function `std::align` can be used to obtain a suitably-aligned pointer within some buffer, and `std::aligned_storage` can be used to obtain suitably-aligned storage.
  321. Each object type imposes its alignment requirement on every object of that type; stricter alignment (with larger alignment requirement) can be requested using C++11 `alignas`.
  322. In order to satisfy alignment requirements of all non-static members of a class, padding may be inserted after some of its members.
  323. GLM supports both packed and aligned types. Packed types allow filling data structure without inserting extra padding. Aligned GLM types align addresses based on the size of the value type of a GLM type.
  324. ```cpp
  325. #define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
  326. #include <glm/glm.hpp>
  327. struct MyStruct
  328. {
  329. glm::vec4 a;
  330. float b;
  331. glm::vec3 c;
  332. };
  333. void foo()
  334. {
  335. printf("MyStruct requires memory padding: %d bytes\n", sizeof(MyStruct));
  336. }
  337. >>> MyStruct requires memory padding: 48 bytes
  338. ```
  339. ```cpp
  340. #include <glm/glm.hpp>
  341. struct MyStruct
  342. {
  343. glm::vec4 a;
  344. float b;
  345. glm::vec3 c;
  346. };
  347. void foo()
  348. {
  349. printf("MyStruct is tightly packed: %d bytes\n", sizeof(MyStruct));
  350. }
  351. >>> MyStruct is tightly packed: 32 bytes
  352. ```
  353. *Note: GLM SIMD optimizations require the use of aligned types*
  354. ### <a name="section2_10"></a> 2.10. GLM\_FORCE\_SIMD\_**: Using SIMD optimizations
  355. GLM provides some SIMD optimizations based on [compiler intrinsics](https://msdn.microsoft.com/en-us/library/26td21ds.aspx).
  356. These optimizations will be automatically thanks to compiler arguments.
  357. For example, if a program is compiled with Visual Studio using `/arch:AVX`, GLM will detect this argument and generate code using AVX instructions automatically when available.
  358. It’s possible to avoid the instruction set detection by forcing the use of a specific instruction set with one of the fallowing define:
  359. `GLM_FORCE_SSE2`, `GLM_FORCE_SSE3`, `GLM_FORCE_SSSE3`, `GLM_FORCE_SSE41`, `GLM_FORCE_SSE42`, `GLM_FORCE_AVX`, `GLM_FORCE_AVX2` or `GLM_FORCE_AVX512`.
  360. The use of intrinsic functions by GLM implementation can be avoided using the define `GLM_FORCE_PURE` before any inclusion of GLM headers. This can be particularly useful if we want to rely on C++14 `constexpr`.
  361. ```cpp
  362. #define GLM_FORCE_PURE
  363. #include <glm/glm.hpp>
  364. static_assert(glm::vec4::length() == 4, "Using GLM C++ 14 constexpr support for compile time tests");
  365. // GLM code will be compiled using pure C++ code without any intrinsics
  366. ```
  367. ```cpp
  368. #define GLM_FORCE_SIMD_AVX2
  369. #include <glm/glm.hpp>
  370. // If the compiler doesn’t support AVX2 instrinsics, compiler errors will happen.
  371. ```
  372. Additionally, GLM provides a low level SIMD API in glm/simd directory for users who are really interested in writing fast algorithms.
  373. ### <a name="section2_11"></a> 2.11. GLM\_FORCE\_PRECISION\_**: Default precision
  374. C++ does not provide a way to implement GLSL default precision selection (as defined in GLSL 4.10 specification section 4.5.3) with GLSL-like syntax.
  375. ```glsl
  376. precision mediump int;
  377. precision highp float;
  378. ```
  379. To use the default precision functionality, GLM provides some defines that need to added before any include of `glm.hpp`:
  380. ```cpp
  381. #define GLM_FORCE_PRECISION_MEDIUMP_INT
  382. #define GLM_FORCE_PRECISION_HIGHP_FLOAT
  383. #include <glm/glm.hpp>
  384. ```
  385. Available defines for floating point types (`glm::vec\*`, `glm::mat\*`):
  386. * `GLM_FORCE_PRECISION_LOWP_FLOAT`: Low precision
  387. * `GLM_FORCE_PRECISION_MEDIUMP_FLOAT`: Medium precision
  388. * `GLM_FORCE_PRECISION_HIGHP_FLOAT`: High precision (default)
  389. Available defines for floating point types (`glm::dvec\*`, `glm::dmat\*`):
  390. * `GLM_FORCE_PRECISION_LOWP_DOUBLE`: Low precision
  391. * `GLM_FORCE_PRECISION_MEDIUMP_DOUBLE`: Medium precision
  392. * `GLM_FORCE_PRECISION_HIGHP_DOUBLE`: High precision (default)
  393. Available defines for signed integer types (`glm::ivec\*`):
  394. * `GLM_FORCE_PRECISION_LOWP_INT`: Low precision
  395. * `GLM_FORCE_PRECISION_MEDIUMP_INT`: Medium precision
  396. * `GLM_FORCE_PRECISION_HIGHP_INT`: High precision (default)
  397. Available defines for unsigned integer types (`glm::uvec\*`):
  398. * `GLM_FORCE_PRECISION_LOWP_UINT`: Low precision
  399. * `GLM_FORCE_PRECISION_MEDIUMP_UINT`: Medium precision
  400. * `GLM_FORCE_PRECISION_HIGHP_UINT`: High precision (default)
  401. ### <a name="section2_12"></a> 2.12. GLM\_FORCE\_SINGLE\_ONLY: Removed explicit 64-bits floating point types
  402. Some platforms (Dreamcast) doesn't support double precision floating point values. To compile on such platforms, GCC has the `--m4-single-only` build argument. When defining `GLM_FORCE_SINGLE_ONLY` before including GLM headers, GLM releases the requirement of double precision floating point values support. Effectivement, all the float64 types are no longer defined and double behaves like float.
  403. ### <a name="section2_13"></a> 2.13. GLM\_FORCE\_SWIZZLE: Enable swizzle operators
  404. Shader languages like GLSL often feature so-called swizzle expressions, which may be used to freely select and arrange a vector's components. For example, `variable.x`, `variable.xzy` and `variable.zxyy` respectively form a scalar, a 3D vector and a 4D vector. The result of a swizzle expression in GLSL can be either an R-value or an L-value. Swizzle expressions can be written with characters from exactly one of `xyzw` (usually for positions), `rgba` (usually for colors), and `stpq` (usually for texture coordinates).
  405. ```glsl
  406. vec4 A;
  407. vec2 B;
  408. B.yx = A.wy;
  409. B = A.xx;
  410. vec3 C = A.bgr;
  411. vec3 D = B.rsz; // Invalid, won't compile
  412. ```
  413. GLM supports some of this functionality. Swizzling can be enabled by defining `GLM_FORCE_SWIZZLE`.
  414. *Note: Enabling swizzle expressions will massively increase the size of your binaries and the time it takes to compile them!*
  415. GLM has two levels of swizzling support described in the following subsections.
  416. #### 2.13.1. Swizzle functions for standard C++ 98
  417. When compiling GLM as C++98, R-value swizzle expressions are simulated through member functions of each vector type.
  418. ```cpp
  419. #define GLM_FORCE_SWIZZLE // Or defined when building (e.g. -DGLM_FORCE_SWIZZLE)
  420. #include <glm/glm.hpp>
  421. void foo()
  422. {
  423. glm::vec4 const ColorRGBA = glm::vec4(1.0f, 0.5f, 0.0f, 1.0f);
  424. glm::vec3 const ColorBGR = ColorRGBA.bgr();
  425. glm::vec3 const PositionA = glm::vec3(1.0f, 0.5f, 0.0f);
  426. glm::vec3 const PositionB = PositionXYZ.xyz() * 2.0f;
  427. glm::vec2 const TexcoordST = glm::vec2(1.0f, 0.5f);
  428. glm::vec4 const TexcoordSTPQ = TexcoordST.stst();
  429. }
  430. ```
  431. Swizzle operators return a **copy** of the component values, and thus *can't* be used as L-values to change a vector's values.
  432. ```cpp
  433. #define GLM_FORCE_SWIZZLE
  434. #include <glm/glm.hpp>
  435. void foo()
  436. {
  437. glm::vec3 const A = glm::vec3(1.0f, 0.5f, 0.0f);
  438. // No compiler error, but A is not modified.
  439. // An anonymous copy is being modified (and then discarded).
  440. A.bgr() = glm::vec3(2.0f, 1.5f, 1.0f); // A is not modified!
  441. }
  442. ```
  443. #### 2.13.2. Swizzle operations for C++ 98 with language extensions
  444. Visual C++, GCC and Clang support, as a _non-standard language extension_, anonymous `struct`s as `union` members. This permits a powerful swizzling implementation that both allows L-value swizzle expressions and GLSL-like syntax. To use this feature, the language extension must be enabled by a supporting compiler and `GLM_FORCE_SWIZZLE` must be `#define`d.
  445. ```cpp
  446. #define GLM_FORCE_SWIZZLE
  447. #include <glm/glm.hpp>
  448. // Only guaranteed to work with Visual C++!
  449. // Some compilers that support Microsoft extensions may compile this.
  450. void foo()
  451. {
  452. glm::vec4 ColorRGBA = glm::vec4(1.0f, 0.5f, 0.0f, 1.0f);
  453. // l-value:
  454. glm::vec4 ColorBGRA = ColorRGBA.bgra;
  455. // r-value:
  456. ColorRGBA.bgra = ColorRGBA;
  457. // Both l-value and r-value
  458. ColorRGBA.bgra = ColorRGBA.rgba;
  459. }
  460. ```
  461. This version returns implementation-specific objects that _implicitly convert_ to their respective vector types. As a consequence of this design, these extra types **can't be directly used** as C++ function arguments; they must be converted through constructors or `operator()`.
  462. ```cpp
  463. #define GLM_FORCE_SWIZZLE
  464. #include <glm/glm.hpp>
  465. using namespace glm;
  466. void foo()
  467. {
  468. vec4 Color = vec4(1.0f, 0.5f, 0.0f, 1.0f);
  469. // Generates compiler errors. Color.rgba is not a vector type.
  470. vec4 ClampedA = clamp(Color.rgba, 0.f, 1.f); // ERROR
  471. // Explicit conversion through a constructor
  472. vec4 ClampedB = clamp(vec4(Color.rgba), 0.f, 1.f); // OK
  473. // Explicit conversion through operator()
  474. vec4 ClampedC = clamp(Color.rgba(), 0.f, 1.f); // OK
  475. }
  476. ```
  477. *Note: The implementation has a caveat: Swizzle operator types must be different on both size of the equal operator or the operation will fail. There is no known fix for this issue to date*
  478. ### <a name="section2_14"></a> 2.14. GLM\_FORCE\_XYZW\_ONLY: Only exposes x, y, z and w components
  479. Following GLSL specifications, GLM supports three sets of components to access vector types member: x, y, z, w; r, g, b, a; and s, t, p, q.
  480. Also, this is making vector component very expressive in the code, it may make debugging vector types a little cubersom as the debuggers will typically display three time the values for each compoenents due to the existance of the three sets.
  481. To simplify vector types, GLM allows exposing only x, y, z and w components thanks to `GLM_FORCE_XYZW_ONLY` define.
  482. ### <a name="section2_15"></a> 2.15. GLM\_FORCE\_LEFT\_HANDED: Force left handed coordinate system
  483. By default, OpenGL is using a right handed coordinate system. However, others APIs such as Direct3D have done different choice and relies on the left handed coordinate system.
  484. GLM allows switching the coordinate system to left handed by defining `GLM_FORCE_LEFT_HANDED`.
  485. ### <a name="section2_16"></a> 2.16. GLM\_FORCE\_DEPTH\_ZERO\_TO\_ONE: Force the use of a clip space between 0 to 1
  486. By default, OpenGL is using a -1 to 1 clip space in Z-axis. However, others APIs such as Direct3D relies on a clip space between 0 to 1 in Z-axis.
  487. GLM allows switching the clip space in Z-axis to 0 to 1 by defining `GLM_FORCE_DEPTH_ZERO_TO_ONE`.
  488. ### <a name="section2_17"></a> 2.17. GLM\_FORCE\_SIZE\_T\_LENGTH: Vector and matrix static size
  489. GLSL supports the member function .length() for all vector and matrix types.
  490. ```cpp
  491. #include <glm/glm.hpp>
  492. void foo(vec4 const& v)
  493. {
  494. int Length = v.length();
  495. ...
  496. }
  497. ```
  498. This function returns an `int` however this function typically interacts with STL `size_t` based code. GLM provides `GLM_FORCE_SIZE_T_LENGTH` pre-processor configuration so that member functions `length()` return a `size_t`.
  499. Additionally, GLM defines the type `glm::length_t` to identify `length()` returned type, independently from `GLM_FORCE_SIZE_T_LENGTH`.
  500. ```cpp
  501. #define GLM_FORCE_SIZE_T_LENGTH
  502. #include <glm/glm.hpp>
  503. void foo(vec4 const& v)
  504. {
  505. glm::length_t Length = v.length();
  506. ...
  507. }
  508. ```
  509. ### <a name="section2_18"></a> 2.18. GLM\_FORCE\_UNRESTRICTED\_GENTYPE: Removing genType restriction
  510. GLSL has restrictions on types supported by certain functions that may appear excessive.
  511. By default, GLM follows the GLSL specification as accurately as possible however it's possible to relax these rules using `GLM_FORCE_UNRESTRICTED_GENTYPE` define.
  512. ```cpp
  513. #include <glm/glm.hpp>
  514. float average(float const A, float const B)
  515. {
  516. return glm::mix(A, B, 0.5f); // By default glm::mix only supports floating-point types
  517. }
  518. ```
  519. By defining GLM\_FORCE\_UNRESTRICTED\_GENTYPE, we allow using integer types:
  520. ```cpp
  521. #define GLM_FORCE_UNRESTRICTED_GENTYPE
  522. #include <glm/glm.hpp>
  523. int average(int const A, int const B)
  524. {
  525. return glm::mix(A, B, 0.5f); // integers are ok thanks to GLM_FORCE_UNRESTRICTED_GENTYPE
  526. }
  527. ```
  528. ---
  529. <div style="page-break-after: always;"> </div>
  530. ## <a name="section3"></a> 3. Stable extensions
  531. ### <a name="section3_1"></a> 3.1. Scalar types
  532. #### 3.1.1. GLM_EXT_scalar_int_sized
  533. This extension exposes sized and signed integer types.
  534. Include `<glm/ext/scalar_int_sized.hpp>` to use these features.
  535. #### 3.1.2. GLM_EXT_scalar_uint_sized
  536. This extension exposes sized and unsigned integer types.
  537. ```cpp
  538. #include <glm/ext/scalar_common.hpp>
  539. glm::uint64 pack(glm::uint32 A, glm::uint16 B, glm::uint8 C, glm::uint8 D)
  540. {
  541. glm::uint64 ShiftA = 0;
  542. glm::uint64 ShiftB = sizeof(glm::uint32) * 8;
  543. glm::uint64 ShiftC = (sizeof(glm::uint32) + sizeof(glm::uint16)) * 8;
  544. glm::uint64 ShiftD = (sizeof(glm::uint32) + sizeof(glm::uint16) + sizeof(glm::uint8)) * 8;
  545. return (glm::uint64(A) << ShiftA) | (glm::uint64(B) << ShiftB) | (glm::uint64(C) << ShiftC) | (glm::uint64(D) << ShiftD);
  546. }
  547. ```
  548. Include `<glm/ext/scalar_uint_sized.hpp>` to use these features.
  549. ### <a name="section3_2"></a> 3.2. Scalar functions
  550. #### 3.2.1. GLM_EXT_scalar_common
  551. This extension exposes support for `min` and `max` functions taking more than two scalar arguments. Also, it adds `fmin` and `fmax` variants which prevents `NaN` propagation.
  552. ```cpp
  553. #include <glm/ext/scalar_common.hpp>
  554. float positiveMax(float const a, float const b)
  555. {
  556. return glm::fmax(a, b, 0.0f);
  557. }
  558. ```
  559. Include `<glm/ext/scalar_common.hpp>` to use these features.
  560. #### 3.2.2. GLM_EXT_scalar_relational
  561. This extension exposes `equal` and `notEqual` scalar variants which takes an epsilon argument.
  562. ```cpp
  563. #include <glm/ext/scalar_relational.hpp>
  564. bool epsilonEqual(float const a, float const b)
  565. {
  566. float const CustomEpsilon = 0.0001f;
  567. return glm::equal(a, b, CustomEpsilon);
  568. }
  569. ```
  570. Include `<glm/ext/scalar_relational.hpp>` to use these features.
  571. #### 3.2.3. GLM_EXT_scalar_constants
  572. This extension exposes useful constants such as `epsilon` and `pi`.
  573. ```cpp
  574. #include <glm/ext/scalar_constants.hpp>
  575. float circumference(float const Diameter)
  576. {
  577. return glm::pi<float>() * Diameter;
  578. }
  579. ```
  580. ```cpp
  581. #include <glm/common.hpp> // abs
  582. #include <glm/ext/scalar_constants.hpp> // epsilon
  583. bool equalULP1(float const a, float const b)
  584. {
  585. return glm::abs(a - b) <= glm::epsilon<float>();
  586. }
  587. ```
  588. Include `<glm/ext/scalar_constants.hpp>` to use these features.
  589. ### <a name="section3_3"></a> 3.3. Vector types
  590. #### 3.3.1. GLM_EXT_vector_float1
  591. This extension exposes single-precision floating point vector with 1 component: `vec1`.
  592. Include `<glm/ext/vector_float1.hpp>` to use these features.
  593. #### 3.3.2. GLM_EXT_vector_float2
  594. This extension exposes single-precision floating point vector with 2 components: `vec2`.
  595. Include `<glm/ext/vector_float2.hpp>` to use these features.
  596. #### 3.3.3. GLM_EXT_vector_float3
  597. This extension exposes single-precision floating point vector with 3 components: `vec3`.
  598. Include `<glm/ext/vector_float3.hpp>` to use these features.
  599. #### 3.3.4. GLM_EXT_vector_float4
  600. This extension exposes single-precision floating point vector with 4 components: `vec4`.
  601. Include `<glm/ext/vector_float4.hpp>` to use these features.
  602. #### 3.3.5. GLM_EXT_vector_double1
  603. This extension exposes double-precision floating point vector with 1 component: `dvec1`.
  604. Include `<glm/ext/vector_double1.hpp>` to use these features.
  605. #### 3.3.6. GLM_EXT_vector_double2
  606. This extension exposes double-precision floating point vector with 2 components: `dvec2`.
  607. Include `<glm/ext/vector_double2.hpp>` to use these features.
  608. #### 3.3.7. GLM_EXT_vector_double3
  609. This extension exposes double-precision floating point vector with 3 components: `dvec3`.
  610. Include `<glm/ext/vector_double3.hpp>` to use these features.
  611. #### 3.3.8. GLM_EXT_vector_double4
  612. This extension exposes double-precision floating point vector with 4 components: `dvec4`.
  613. Include `<glm/ext/vector_double4.hpp>` to use these features.
  614. #### 3.3.9. GLM_EXT_vector_int1
  615. This extension exposes signed integer vector with 1 component: `ivec1`.
  616. Include `<glm/ext/vector_int1.hpp>` to use these features.
  617. #### 3.3.10. GLM_EXT_vector_int2
  618. This extension exposes signed integer vector with 2 components: `ivec2`.
  619. Include `<glm/ext/vector_int2.hpp>` to use these features.
  620. #### 3.3.11. GLM_EXT_vector_int3
  621. This extension exposes signed integer vector with 3 components: `ivec3`.
  622. Include `<glm/ext/vector_int3.hpp>` to use these features.
  623. #### 3.3.12. GLM_EXT_vector_int4
  624. This extension exposes signed integer vector with 4 components: `ivec4`.
  625. Include `<glm/ext/vector_int4.hpp>` to use these features.
  626. #### 3.3.13. GLM_EXT_vector_int1
  627. This extension exposes unsigned integer vector with 1 component: `uvec1`.
  628. Include `<glm/ext/vector_uint1.hpp>` to use these features.
  629. #### 3.3.14. GLM_EXT_vector_uint2
  630. This extension exposes unsigned integer vector with 2 components: `uvec2`.
  631. Include `<glm/ext/vector_uint2.hpp>` to use these features.
  632. #### 3.3.15. GLM_EXT_vector_uint3
  633. This extension exposes unsigned integer vector with 3 components: `uvec3`.
  634. Include `<glm/ext/vector_uint3.hpp>` to use these features.
  635. #### 3.3.16. GLM_EXT_vector_uint4
  636. This extension exposes unsigned integer vector with 4 components: `uvec4`.
  637. Include `<glm/ext/vector_uint4.hpp>` to use these features.
  638. #### 3.3.17. GLM_EXT_vector_bool1
  639. This extension exposes boolean vector with 1 component: `bvec1`.
  640. Include `<glm/ext/vector_bool1.hpp>` to use these features.
  641. #### 3.3.18. GLM_EXT_vector_bool2
  642. This extension exposes boolean vector with 2 components: `bvec2`.
  643. Include `<glm/ext/vector_bool2.hpp>` to use these features.
  644. #### 3.3.19. GLM_EXT_vector_bool3
  645. This extension exposes boolean vector with 3 components: `bvec3`.
  646. Include `<glm/ext/vector_bool3.hpp>` to use these features.
  647. #### 3.3.20. GLM_EXT_vector_bool4
  648. This extension exposes boolean vector with 4 components: `bvec4`.
  649. Include `<glm/ext/vector_bool4.hpp>` to use these features.
  650. ### <a name="section3_4"></a> 3.4. Vector types with precision qualifiers
  651. #### 3.4.1. GLM_EXT_vector_float1_precision
  652. This extension exposes single-precision floating point vector with 1 component using various precision in term of ULPs: `lowp_vec1`, `mediump_vec1` and `highp_vec1`.
  653. Include `<glm/ext/vector_float1_precision.hpp>` to use these features.
  654. #### 3.4.2. GLM_EXT_vector_float2_precision
  655. This extension exposes single-precision floating point vector with 2 components using various precision in term of ULPs: `lowp_vec2`, `mediump_vec2` and `highp_vec2`.
  656. Include `<glm/ext/vector_float2_precision.hpp>` to use these features.
  657. #### 3.4.3. GLM_EXT_vector_float3_precision
  658. This extension exposes single-precision floating point vector with 3 components using various precision in term of ULPs: `lowp_vec3`, `mediump_vec3` and `highp_vec3`.
  659. Include `<glm/ext/vector_float3_precision.hpp>` to use these features.
  660. #### 3.4.4. GLM_EXT_vector_float4_precision
  661. This extension exposes single-precision floating point vector with 4 components using various precision in term of ULPs: `lowp_vec4`, `mediump_vec4` and `highp_vec4`.
  662. Include `<glm/ext/vector_float4_precision.hpp>` to use these features.
  663. #### 3.4.5. GLM_EXT_vector_double1_precision
  664. This extension exposes double-precision floating point vector with 1 component using various precision in term of ULPs: `lowp_dvec1`, `mediump_dvec1` and `highp_dvec1`.
  665. Include `<glm/ext/vector_double1_precision.hpp>` to use these features.
  666. #### 3.4.6. GLM_EXT_vector_double2_precision
  667. This extension exposes double-precision floating point vector with 2 components using various precision in term of ULPs: `lowp_dvec2`, `mediump_dvec2` and `highp_dvec2`.
  668. Include `<glm/ext/vector_double2_precision.hpp>` to use these features.
  669. #### 3.4.7. GLM_EXT_vector_double3_precision
  670. This extension exposes double-precision floating point vector with 3 components using various precision in term of ULPs: `lowp_dvec3`, `mediump_dvec3` and `highp_dvec3`.
  671. Include `<glm/ext/vector_double3_precision.hpp>` to use these features.
  672. #### 3.4.8. GLM_EXT_vector_double4_precision
  673. This extension exposes double-precision floating point vector with 4 components using various precision in term of ULPs: `lowp_dvec4`, `mediump_dvec4` and `highp_dvec4`.
  674. Include `<glm/ext/vector_double4_precision.hpp>` to use these features.
  675. ### <a name="section3_4"></a> 3.5. Vector functions
  676. #### 3.5.1. GLM_EXT_vector_common
  677. This extension exposes support for `min` and `max` functions taking more than two vector arguments. Also, it adds `fmin` and `fmax` variants which prevents `NaN` propagation.
  678. ```cpp
  679. #include <glm/ext/vector_float2.hpp> // vec2
  680. #include <glm/ext/vector_common.hpp> // fmax
  681. float positiveMax(float const a, float const b)
  682. {
  683. return glm::fmax(a, b, 0.0f);
  684. }
  685. ```
  686. Include `<glm/ext/vector_common.hpp>` to use these features.
  687. #### 3.5.2. GLM_EXT_vector_relational
  688. This extension exposes `equal` and `notEqual` vector variants which takes an epsilon argument.
  689. ```cpp
  690. #include <glm/ext/vector_float2.hpp> // vec2
  691. #include <glm/ext/vector_relational.hpp> // equal, all
  692. bool epsilonEqual(glm::vec2 const& A, glm::vec2 const& B)
  693. {
  694. float const CustomEpsilon = 0.0001f;
  695. return glm::all(glm::equal(A, B, CustomEpsilon));
  696. }
  697. ```
  698. Include `<glm/ext/vector_relational.hpp>` to use these features.
  699. ### <a name="section3_6"></a> 3.6. Matrix types
  700. #### 3.6.1. GLM_EXT_matrix_float2x2
  701. This extension exposes single-precision floating point vector with 2 columns by 2 rows: `mat2x2`.
  702. Include `<glm/ext/matrix_float2x2.hpp>` to use these features.
  703. #### 3.6.2. GLM_EXT_matrix_float2x3
  704. This extension exposes single-precision floating point vector with 2 columns by 3 rows: `mat2x3`.
  705. Include `<glm/ext/matrix_float2x3.hpp>` to use these features.
  706. #### 3.6.3. GLM_EXT_matrix_float2x4
  707. This extension exposes single-precision floating point vector with 2 columns by 4 rows: `mat2x4`.
  708. Include `<glm/ext/matrix_float2x4.hpp>` to use these features.
  709. #### 3.6.4. GLM_EXT_matrix_float3x2
  710. This extension exposes single-precision floating point vector with 3 columns by 2 rows: `mat3x2`.
  711. Include `<glm/ext/matrix_float3x2.hpp>` to use these features.
  712. #### 3.6.5. GLM_EXT_matrix_float3x3
  713. This extension exposes single-precision floating point vector with 3 columns by 3 rows: `mat3x3`.
  714. Include `<glm/ext/matrix_float3x3.hpp>` to use these features.
  715. #### 3.6.6. GLM_EXT_matrix_float3x4
  716. This extension exposes single-precision floating point vector with 3 columns by 4 rows: `mat3x4`.
  717. Include `<glm/ext/matrix_float3x4.hpp>` to use these features.
  718. #### 3.6.7. GLM_EXT_matrix_float4x2
  719. This extension exposes single-precision floating point vector with 4 columns by 2 rows: `mat4x2`.
  720. Include `<glm/ext/matrix_float4x2.hpp>` to use these features.
  721. #### 3.6.8. GLM_EXT_matrix_float4x3
  722. This extension exposes single-precision floating point vector with 4 columns by 3 rows: `mat4x3`.
  723. Include `<glm/ext/matrix_float4x3.hpp>` to use these features.
  724. #### 3.6.9. GLM_EXT_matrix_float4x4
  725. This extension exposes single-precision floating point vector with 4 columns by 4 rows: `mat4x4`.
  726. Include `<glm/ext/matrix_float4x4.hpp>` to use these features.
  727. #### 3.6.10. GLM_EXT_matrix_double2x2
  728. This extension exposes double-precision floating point vector with 2 columns by 2 rows: `dmat2x2`.
  729. Include `<glm/ext/matrix_double2x2.hpp>` to use these features.
  730. #### 3.6.11. GLM_EXT_matrix_double2x3
  731. This extension exposes double-precision floating point vector with 2 columns by 3 rows: `dmat2x3`.
  732. Include `<glm/ext/matrix_double2x3.hpp>` to use these features.
  733. #### 3.6.12. GLM_EXT_matrix_double2x4
  734. This extension exposes double-precision floating point vector with 2 columns by 4 rows: `dmat2x4`.
  735. Include `<glm/ext/matrix_double2x4.hpp>` to use these features.
  736. #### 3.6.13. GLM_EXT_matrix_double3x2
  737. This extension exposes double-precision floating point vector with 3 columns by 2 rows: `dmat3x2`.
  738. Include `<glm/ext/matrix_double3x2.hpp>` to use these features.
  739. #### 3.6.14. GLM_EXT_matrix_double3x3
  740. This extension exposes double-precision floating point vector with 3 columns by 3 rows: `dmat3x3`.
  741. Include `<glm/ext/matrix_double3x3.hpp>` to use these features.
  742. #### 3.6.15. GLM_EXT_matrix_double3x4
  743. This extension exposes double-precision floating point vector with 3 columns by 4 rows: `dmat3x4`.
  744. Include `<glm/ext/matrix_double3x4.hpp>` to use these features.
  745. #### 3.6.16. GLM_EXT_matrix_double4x2
  746. This extension exposes double-precision floating point vector with 4 columns by 2 rows: `dmat4x2`.
  747. Include `<glm/ext/matrix_double4x2.hpp>` to use these features.
  748. #### 3.6.17. GLM_EXT_matrix_double4x3
  749. This extension exposes double-precision floating point vector with 4 columns by 3 rows: `dmat4x3`.
  750. Include `<glm/ext/matrix_double4x3.hpp>` to use these features.
  751. #### 3.6.18. GLM_EXT_matrix_double4x4
  752. This extension exposes double-precision floating point vector with 4 columns by 4 rows: `dmat4x4`.
  753. Include `<glm/ext/matrix_double4x4.hpp>` to use these features.
  754. ### <a name="section3_7"></a> 3.7. Matrix types with precision qualifiers
  755. #### 3.7.1. GLM_EXT_matrix_float2x2_precision
  756. This extension exposes single-precision floating point vector with 2 columns by 2 rows using various precision in term of ULPs: `lowp_mat2x2`, `mediump_mat2x2` and `highp_mat2x2`.
  757. Include `<glm/ext/matrix_float2x2_precision.hpp>` to use these features.
  758. #### 3.7.2. GLM_EXT_matrix_float2x3_precision
  759. This extension exposes single-precision floating point vector with 2 columns by 3 rows using various precision in term of ULPs: `lowp_mat2x3`, `mediump_mat2x3` and `highp_mat2x3`.
  760. Include `<glm/ext/matrix_float2x3_precision.hpp>` to use these features.
  761. #### 3.7.3. GLM_EXT_matrix_float2x4_precision
  762. This extension exposes single-precision floating point vector with 2 columns by 4 rows using various precision in term of ULPs: `lowp_mat2x4`, `mediump_mat2x4` and `highp_mat2x4`.
  763. Include `<glm/ext/matrix_float2x4_precision.hpp>` to use these features.
  764. #### 3.7.4. GLM_EXT_matrix_float3x2_precision
  765. This extension exposes single-precision floating point vector with 3 columns by 2 rows using various precision in term of ULPs: `lowp_mat3x2`, `mediump_mat3x2` and `highp_mat3x2`.
  766. Include `<glm/ext/matrix_float3x2_precision.hpp>` to use these features.
  767. #### 3.7.5. GLM_EXT_matrix_float3x3_precision
  768. This extension exposes single-precision floating point vector with 3 columns by 3 rows using various precision in term of ULPs: `lowp_mat3x3`, `mediump_mat3x3` and `highp_mat3x3`.
  769. Include `<glm/ext/matrix_float3x3_precision.hpp>` to use these features.
  770. #### 3.7.6. GLM_EXT_matrix_float3x4_precision
  771. This extension exposes single-precision floating point vector with 3 columns by 4 rows using various precision in term of ULPs: `lowp_mat3x4`, `mediump_mat3x4` and `highp_mat3x4`.
  772. Include `<glm/ext/matrix_float3x4_precision.hpp>` to use these features.
  773. #### 3.7.7. GLM_EXT_matrix_float4x2_precision
  774. This extension exposes single-precision floating point vector with 4 columns by 2 rows using various precision in term of ULPs: `lowp_mat4x2`, `mediump_mat4x2` and `highp_mat4x2`.
  775. Include `<glm/ext/matrix_float4x2_precision.hpp>` to use these features.
  776. #### 3.7.8. GLM_EXT_matrix_float4x3_precision
  777. This extension exposes single-precision floating point vector with 4 columns by 3 rows using various precision in term of ULPs: `lowp_mat4x3`, `mediump_mat4x3` and `highp_mat4x3`.
  778. Include `<glm/ext/matrix_float4x3_precision.hpp>` to use these features.
  779. #### 3.7.9. GLM_EXT_matrix_float4x4_precision
  780. This extension exposes single-precision floating point vector with 4 columns by 4 rows using various precision in term of ULPs: `lowp_mat4x4`, `mediump_mat4x4` and `highp_mat4x4`.
  781. Include `<glm/ext/matrix_float4x4_precision.hpp>` to use these features.
  782. #### 3.7.10. GLM_EXT_matrix_double2x2_precision
  783. This extension exposes double-precision floating point vector with 2 columns by 2 rows using various precision in term of ULPs: `lowp_dmat2x2`, `mediump_dmat2x2` and `highp_dmat2x2`.
  784. Include `<glm/ext/matrix_double2x2_precision.hpp>` to use these features.
  785. #### 3.7.11. GLM_EXT_matrix_double2x3_precision
  786. This extension exposes double-precision floating point vector with 2 columns by 3 rows using various precision in term of ULPs: `lowp_dmat2x3`, `mediump_dmat2x3` and `highp_dmat2x3`.
  787. Include `<glm/ext/matrix_double2x3_precision.hpp>` to use these features.
  788. #### 3.7.12. GLM_EXT_matrix_double2x4_precision
  789. This extension exposes double-precision floating point vector with 2 columns by 4 rows using various precision in term of ULPs: `lowp_dmat2x4`, `mediump_dmat2x4` and `highp_dmat2x4`.
  790. Include `<glm/ext/matrix_double2x4_precision.hpp>` to use these features.
  791. #### 3.7.13. GLM_EXT_matrix_double3x2_precision
  792. This extension exposes double-precision floating point vector with 3 columns by 2 rows using various precision in term of ULPs: `lowp_dmat3x2`, `mediump_dmat3x2` and `highp_dmat3x2`.
  793. Include `<glm/ext/matrix_double3x2_precision.hpp>` to use these features.
  794. #### 3.7.14. GLM_EXT_matrix_double3x3_precision
  795. This extension exposes double-precision floating point vector with 3 columns by 3 rows using various precision in term of ULPs: `lowp_dmat3x3`, `mediump_dmat3x3` and `highp_dmat3x3`.
  796. Include `<glm/ext/matrix_double3x3_precision.hpp>` to use these features.
  797. #### 3.7.15. GLM_EXT_matrix_double3x4_precision
  798. This extension exposes double-precision floating point vector with 3 columns by 4 rows using various precision in term of ULPs: `lowp_dmat3x4`, `mediump_dmat3x4` and `highp_dmat3x4`.
  799. Include `<glm/ext/matrix_double3x4_precision.hpp>` to use these features.
  800. #### 3.7.16. GLM_EXT_matrix_double4x2_precision
  801. This extension exposes double-precision floating point vector with 4 columns by 2 rows using various precision in term of ULPs: `lowp_dmat4x2`, `mediump_dmat4x2` and `highp_dmat4x2`.
  802. Include `<glm/ext/matrix_double4x2_precision.hpp>` to use these features.
  803. #### 3.7.17. GLM_EXT_matrix_double4x3_precision
  804. This extension exposes double-precision floating point vector with 4 columns by 3 rows using various precision in term of ULPs: `lowp_dmat4x3`, `mediump_dmat4x3` and `highp_dmat4x3`.
  805. Include `<glm/ext/matrix_double4x3_precision.hpp>` to use these features.
  806. #### 3.7.18. GLM_EXT_matrix_double4x4_precision
  807. This extension exposes double-precision floating point vector with 4 columns by 4 rows using various precision in term of ULPs: `lowp_dmat4x4`, `mediump_dmat4x4` and `highp_dmat4x4`.
  808. Include `<glm/ext/matrix_double4x4_precision.hpp>` to use these features.
  809. ### <a name="section3_8"></a> 3.8. Matrix functions
  810. #### 3.8.1. GLM_EXT_matrix_relational
  811. This extension exposes `equal` and `notEqual` matrix variants which takes an optional epsilon argument.
  812. ```cpp
  813. #include <glm/ext/vector_bool4.hpp> // bvec4
  814. #include <glm/ext/matrix_float4x4.hpp> // mat4
  815. #include <glm/ext/matrix_relational.hpp> // equal, all
  816. bool epsilonEqual(glm::mat4 const& A, glm::mat4 const& B)
  817. {
  818. float const CustomEpsilon = 0.0001f;
  819. glm::bvec4 const ColumnEqual = glm::equal(A, B, CustomEpsilon); // Evaluation per column
  820. return glm::all(ColumnEqual);
  821. }
  822. ```
  823. Include `<glm/ext/matrix_relational.hpp>` to use these features.
  824. #### 3.8.2. GLM_EXT_matrix_transform
  825. This extension exposes matrix transformation functions: `translate`, `rotate` and `scale`.
  826. ```cpp
  827. #include <glm/ext/vector_float2.hpp> // vec2
  828. #include <glm/ext/vector_float3.hpp> // vec3
  829. #include <glm/ext/matrix_float4x4.hpp> // mat4x4
  830. #include <glm/ext/matrix_transform.hpp> // translate, rotate, scale, identity
  831. glm::mat4 computeModelViewMatrix(float Translate, glm::vec2 const & Rotate)
  832. {
  833. glm::mat4 View = glm::translate(glm::identity(), glm::vec3(0.0f, 0.0f, -Translate));
  834. View = glm::rotate(View, Rotate.y, glm::vec3(-1.0f, 0.0f, 0.0f));
  835. View = glm::rotate(View, Rotate.x, glm::vec3(0.0f, 1.0f, 0.0f));
  836. glm::mat4 Model = glm::scale(glm::identity(), glm::vec3(0.5f));
  837. return View * Model;
  838. }
  839. ```
  840. Include `<glm/ext/matrix_transform.hpp>` to use these features.
  841. #### 3.8.3. GLM_EXT_matrix_clip_space
  842. This extension exposes functions to transform scenes into the clip space.
  843. ```cpp
  844. #include <glm/ext/matrix_float4x4.hpp> // mat4x4
  845. #include <glm/ext/matrix_clip_space.hpp> // perspective
  846. #include <glm/trigonometric.hpp> // radians
  847. glm::mat4 computeProjection(float Width, float Height)
  848. {
  849. return glm::perspective(glm::radians(45.0f), Width / Height, 0.1f, 100.f);
  850. }
  851. ```
  852. Include `<glm/ext/matrix_clip_space.hpp>` to use these features.
  853. #### 3.8.4. GLM_EXT_matrix_projection
  854. This extension exposes functions to map object coordinates into window coordinates and reverse
  855. Include `<glm/ext/matrix_projection.hpp>` to use these features.
  856. ### <a name="section3_9"></a> 3.9. Quaternion types
  857. #### 3.9.1. GLM_EXT_quaternion_float
  858. This extension exposes single-precision floating point quaternion: `quat`.
  859. Include `<glm/ext/quaternion_float.hpp>` to use these features.
  860. #### 3.9.2. GLM_EXT_quaternion_double
  861. This extension exposes double-precision floating point quaternion: `dquat`.
  862. Include `<glm/ext/quaternion_double.hpp>` to use these features.
  863. ### <a name="section3_10"></a> 3.10. Quaternion types with precision qualifiers
  864. #### 3.10.1. GLM_EXT_quaternion_float_precision
  865. This extension exposes single-precision floating point quaternion using various precision in term of ULPs: `lowp_quat`, `mediump_quat` and `highp_quat`.
  866. Include `<glm/ext/quaternion_float_precision.hpp>` to use these features.
  867. #### 3.10.2. GLM_EXT_quaternion_double_precision
  868. This extension exposes double-precision floating point quaternion using various precision in term of ULPs: `lowp_dquat`, `mediump_dquat` and `highp_dquat`.
  869. Include `<glm/ext/quaternion_double_precision.hpp>` to use these features.
  870. ### <a name="section3_11"></a> 3.11. Quaternion functions
  871. #### 3.11.1. GLM_EXT_quaternion_common
  872. This extension exposes common quaternion functions such as `slerp`, `conjugate` and `inverse`.
  873. Include `<glm/ext/quaternion_common.hpp>` to use these features.
  874. #### 3.11.2. GLM_EXT_quaternion_geometric
  875. This extension exposes geometric quaternion functions such as `length`, `normalize`, `dot` and `cross`.
  876. Include `<glm/ext/quaternion_geometric.hpp>` to use these features.
  877. #### 3.11.3. GLM_EXT_quaternion_trigonometric
  878. This extension exposes trigonometric quaternion functions such as `angle` and `axis`.
  879. Include `<glm/ext/quaternion_trigonometric.hpp>` to use these features.
  880. #### 3.11.4. GLM_EXT_quaternion_exponential
  881. This extensions expose exponential functions for quaternions such as `exp`, `log`, `pow` and `sqrt`.
  882. Include `<glm/ext/quaternion_exponential.hpp>` to use these features.
  883. #### 3.11.5. GLM_EXT_quaternion_relational
  884. This extension exposes relational functions to compare quaternions.
  885. Include `<glm/ext/quaternion_relational.hpp>` to use these features.
  886. #### 3.11.6. GLM_EXT_quaternion_transform
  887. This extension exposes functions to transform objects.
  888. Include `<glm/ext/quaternion_transform.hpp>` to use these features.
  889. ---
  890. <div style="page-break-after: always;"> </div>
  891. ## <a name="section4"></a> 4. Recommended extensions
  892. GLM extends the core GLSL feature set with extensions. These extensions include: quaternion, transformation, spline, matrix inverse, color spaces, etc.
  893. To include an extension, we only need to include the dedicated header file. Once included, the features are added to the GLM namespace.
  894. ```cpp
  895. #include <glm/glm.hpp>
  896. #include <glm/gtc/matrix_transform.hpp>
  897. int foo()
  898. {
  899. glm::vec4 Position = glm::vec4(glm:: vec3(0.0f), 1.0f);
  900. glm::mat4 Model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));
  901. glm::vec4 Transformed = Model * Position;
  902. ...
  903. return 0;
  904. }
  905. ```
  906. When an extension is included, all the dependent core functionalities and extensions will be included as well.
  907. ### <a name="section4_1"></a> 4.1. GLM_GTC_bitfield
  908. Fast bitfield operations on scalar and vector variables.
  909. `<glm/gtc/bitfield.hpp>` need to be included to use these features.
  910. ### <a name="section4_2"></a> 4.2. GLM_GTC_color_space
  911. Conversion between linear RGB and sRGB color spaces.
  912. `<glm/gtc/color_space.hpp>` need to be included to use these features.
  913. ### <a name="section4_3"></a> 4.3. GLM_GTC_constants
  914. Provide a list of built-in constants.
  915. `<glm/gtc/constants.hpp>` need to be included to use these features.
  916. ### <a name="section4_4"></a> 4.4. GLM\_GTC\_epsilon
  917. Approximate equality comparisons for floating-point numbers, possibly with a user-defined epsilon.
  918. `<glm/gtc/epsilon.hpp>` need to be included to use these features.
  919. ### <a name="section4_5"></a> 4.5. GLM\_GTC\_integer
  920. Integer variants of core GLM functions.
  921. `<glm/gtc/integer.hpp>` need to be included to use these features.
  922. ### <a name="section4_6"></a> 4.6. GLM\_GTC\_matrix\_access
  923. Functions to conveniently access the individual rows or columns of a matrix.
  924. `<glm/gtc/matrix_access.hpp>` need to be included to use these features.
  925. ### <a name="section4_7"></a> 4.7. GLM\_GTC\_matrix\_integer
  926. Integer matrix types similar to the core floating-point matrices. Some operations (such as inverse and determinant) are not supported.
  927. `<glm/gtc/matrix_integer.hpp>` need to be included to use these features.
  928. ### <a name="section4_8"></a> 4.8. GLM\_GTC\_matrix\_inverse
  929. Additional matrix inverse functions.
  930. `<glm/gtc/matrix_inverse.hpp>` need to be included to use these features.
  931. ### <a name="section4_9"></a> 4.9. GLM\_GTC\_matrix\_transform
  932. Matrix transformation functions that follow the OpenGL fixed-function conventions.
  933. For example, the `lookAt` function generates a transformation matrix that projects world coordinates into eye coordinates suitable for projection matrices (e.g. `perspective`, `ortho`). See the OpenGL compatibility specifications for more information about the layout of these generated matrices.
  934. The matrices generated by this extension use standard OpenGL fixed-function conventions. For example, the `lookAt` function generates a transform from world space into the specific eye space that the
  935. projective matrix functions (`perspective`, `ortho`, etc) are designed to expect. The OpenGL compatibility specifications define the particular layout of this eye space.
  936. `<glm/gtc/matrix_transform.hpp>` need to be included to use these features.
  937. ### <a name="section4_10"></a> 4.10. GLM\_GTC\_noise
  938. Define 2D, 3D and 4D procedural noise functions.
  939. <`glm/gtc/noise.hpp>` need to be included to use these features.
  940. ![](/doc/manual/noise-simplex1.jpg)
  941. Figure 4.10.1: glm::simplex(glm::vec2(x / 16.f, y / 16.f));
  942. ![](/doc/manual/noise-simplex2.jpg)
  943. Figure 4.10.2: glm::simplex(glm::vec3(x / 16.f, y / 16.f, 0.5f));
  944. ![](/doc/manual/noise-simplex3.jpg)
  945. Figure 4.10.3: glm::simplex(glm::vec4(x / 16.f, y / 16.f, 0.5f, 0.5f));
  946. ![](/doc/manual/noise-perlin1.jpg)
  947. Figure 4.10.4: glm::perlin(glm::vec2(x / 16.f, y / 16.f));
  948. ![](/doc/manual/noise-perlin2.jpg)
  949. Figure 4.10.5: glm::perlin(glm::vec3(x / 16.f, y / 16.f, 0.5f));
  950. ![](/doc/manual/noise-perlin3.jpg)
  951. Figure 4.10.6: glm::perlin(glm::vec4(x / 16.f, y / 16.f, 0.5f, 0.5f)));
  952. ![](/doc/manual/noise-perlin4.png)
  953. Figure 4.10.7: glm::perlin(glm::vec2(x / 16.f, y / 16.f), glm::vec2(2.0f));
  954. ![](/doc/manual/noise-perlin5.png)
  955. Figure 4.10.8: glm::perlin(glm::vec3(x / 16.f, y / 16.f, 0.5f), glm::vec3(2.0f));
  956. ![](/doc/manual/noise-perlin6.png)
  957. Figure 4.10.9: glm::perlin(glm::vec4(x / 16.f, y / 16.f, glm::vec2(0.5f)), glm::vec4(2.0f));
  958. ### <a name="section4_11"></a> 4.11. GLM\_GTC\_packing
  959. Convert scalar and vector types to and from packed formats, saving space at the cost of precision. However, packing a value into a format that it was previously unpacked from is guaranteed to be lossless.
  960. `<glm/gtc/packing.hpp>` need to be included to use these features.
  961. ### <a name="section4_12"></a> 4.12. GLM\_GTC\_quaternion
  962. Quaternions and operations upon thereof.
  963. `<glm/gtc/quaternion.hpp>` need to be included to use these features.
  964. ### <a name="section4_13"></a> 4.13. GLM\_GTC\_random
  965. Probability distributions in up to four dimensions.
  966. `<glm/gtc/random.hpp>` need to be included to use these features.
  967. ![](/doc/manual/random-linearrand.png)
  968. Figure 4.13.1: glm::vec4(glm::linearRand(glm::vec2(-1), glm::vec2(1)), 0, 1);
  969. ![](/doc/manual/random-circularrand.png)
  970. Figure 4.13.2: glm::vec4(glm::circularRand(1.0f), 0, 1);
  971. ![](/doc/manual/random-sphericalrand.png)
  972. Figure 4.13.3: glm::vec4(glm::sphericalRand(1.0f), 1);
  973. ![](/doc/manual/random-diskrand.png)
  974. Figure 4.13.4: glm::vec4(glm::diskRand(1.0f), 0, 1);
  975. ![](/doc/manual/random-ballrand.png)
  976. Figure 4.13.5: glm::vec4(glm::ballRand(1.0f), 1);
  977. ![](/doc/manual/random-gaussrand.png)
  978. Figure 4.13.6: glm::vec4(glm::gaussRand(glm::vec3(0), glm::vec3(1)), 1);
  979. ### <a name="section4_14"></a> 4.14. GLM\_GTC\_reciprocal
  980. Reciprocal trigonometric functions (e.g. secant, cosecant, tangent).
  981. `<glm/gtc/reciprocal.hpp>` need to be included to use the features of this extension.
  982. ### <a name="section4_15"></a> 4.15. GLM\_GTC\_round
  983. Various rounding operations and common special cases thereof.
  984. `<glm/gtc/round.hpp>` need to be included to use the features of this extension.
  985. ### <a name="section4_16"></a> 4.16. GLM\_GTC\_type\_aligned
  986. Aligned vector types.
  987. `<glm/gtc/type_aligned.hpp>` need to be included to use the features of this extension.
  988. ### <a name="section4_17"></a> 4.17. GLM\_GTC\_type\_precision
  989. Vector and matrix types with defined precisions, e.g. `i8vec4`, which is a 4D vector of signed 8-bit integers.
  990. `<glm/gtc/type\_precision.hpp>` need to be included to use the features of this extension.
  991. ### <a name="section4_18"></a> 4.18. GLM\_GTC\_type\_ptr
  992. Facilitate interactions between pointers to basic types (e.g. `float*`) and GLM types (e.g. `mat4`).
  993. This extension defines an overloaded function, `glm::value_ptr`, which returns a pointer to the memory layout of any GLM vector or matrix (`vec3`, `mat4`, etc.). Matrix types store their values in column-major order. This is useful for uploading data to matrices or for copying data to buffer objects.
  994. ```cpp
  995. // GLM_GTC_type_ptr provides a safe solution:
  996. #include <glm/glm.hpp>
  997. #include <glm/gtc/type_ptr.hpp>
  998. void foo()
  999. {
  1000. glm::vec4 v(0.0f);
  1001. glm::mat4 m(1.0f);
  1002. ...
  1003. glVertex3fv(glm::value_ptr(v))
  1004. glLoadMatrixfv(glm::value_ptr(m));
  1005. }
  1006. // Another solution, this one inspired by the STL:
  1007. #include <glm/glm.hpp>
  1008. void foo()
  1009. {
  1010. glm::vec4 v(0.0f);
  1011. glm::mat4 m(1.0f);
  1012. ...
  1013. glVertex3fv(&v[0]);
  1014. glLoadMatrixfv(&m[0][0]);
  1015. }
  1016. ```
  1017. *Note: It would be possible to implement [`glVertex3fv`](http://www.opengl.org/sdk/docs/man2/xhtml/glVertex.xml)(glm::vec3(0)) in C++ with the appropriate cast operator that would result as an
  1018. implicit cast in this example. However cast operators may produce programs running with unexpected behaviours without build error or any form of notification. *
  1019. `<glm/gtc/type_ptr.hpp>` need to be included to use these features.
  1020. ### <a name="section4_19"></a> 4.19. GLM\_GTC\_ulp
  1021. Measure a function's accuracy given a reference implementation of it. This extension works on floating-point data and provides results in [ULP](http://ljk.imag.fr/membres/Carine.Lucas/TPScilab/JMMuller/ulp-toms.pdf).
  1022. `<glm/gtc/ulp.hpp>` need to be included to use these features.
  1023. ### <a name="section4_20"></a> 4.20. GLM\_GTC\_vec1
  1024. Add \*vec1 types.
  1025. `<glm/gtc/vec1.hpp>` need to be included to use these features.
  1026. ---
  1027. <div style="page-break-after: always;"> </div>
  1028. ## <a name="section5"></a> 5. OpenGL interoperability
  1029. ### <a name="section5_1"></a> 5.1. GLM replacements for deprecated OpenGL functions
  1030. OpenGL 3.1 specification has deprecated some features that have been removed from OpenGL 3.2 core profile specification. GLM provides some replacement functions.
  1031. [***glRotate{f, d}:***](https://www.opengl.org/sdk/docs/man2/xhtml/glRotate.xml)
  1032. ```cpp
  1033. glm::mat4 glm::rotate(glm::mat4 const& m, float angle, glm::vec3 const& axis);
  1034. glm::dmat4 glm::rotate(glm::dmat4 const& m, double angle, glm::dvec3 const& axis);
  1035. ```
  1036. From `GLM_GTC_matrix_transform` extension: &lt;glm/gtc/matrix\_transform.hpp&gt;
  1037. [***glScale{f, d}:***](http://www.opengl.org/sdk/docs/man2/xhtml/glScale.xml)
  1038. ```cpp
  1039. glm::mat4 glm::scale(glm::mat4 const& m, glm::vec3 const& factors);
  1040. glm::dmat4 glm::scale(glm::dmat4 const& m, glm::dvec3 const& factors);
  1041. ```
  1042. From `GLM_GTC_matrix_transform` extension: &lt;glm/gtc/matrix\_transform.hpp&gt;
  1043. [***glTranslate{f, d}:***](https://www.opengl.org/sdk/docs/man2/xhtml/glTranslate.xml)
  1044. ```cpp
  1045. glm::mat4 glm::translate(glm::mat4 const& m, glm::vec3 const& translation);
  1046. glm::dmat4 glm::translate(glm::dmat4 const& m, glm::dvec3 const& translation);
  1047. ```
  1048. From `GLM_GTC_matrix_transform` extension: &lt;glm/gtc/matrix\_transform.hpp&gt;
  1049. [***glLoadIdentity:***](https://www.opengl.org/sdk/docs/man2/xhtml/glLoadIdentity.xml)
  1050. ```cpp
  1051. glm::mat4(1.0) or glm::mat4();
  1052. glm::dmat4(1.0) or glm::dmat4();
  1053. ```
  1054. From GLM core library: `<glm/glm.hpp>`
  1055. [***glMultMatrix{f, d}:***](https://www.opengl.org/sdk/docs/man2/xhtml/glMultMatrix.xml)
  1056. ```cpp
  1057. glm::mat4() * glm::mat4();
  1058. glm::dmat4() * glm::dmat4();
  1059. ```
  1060. From GLM core library: `<glm/glm.hpp>`
  1061. [***glLoadTransposeMatrix{f, d}:***](https://www.opengl.org/sdk/docs/man2/xhtml/glLoadTransposeMatrix.xml)
  1062. ```cpp
  1063. glm::transpose(glm::mat4());
  1064. glm::transpose(glm::dmat4());
  1065. ```
  1066. From GLM core library: `<glm/glm.hpp>`
  1067. [***glMultTransposeMatrix{f, d}:***](https://www.opengl.org/sdk/docs/man2/xhtml/glMultTransposeMatrix.xml)
  1068. ```cpp
  1069. glm::mat4() * glm::transpose(glm::mat4());
  1070. glm::dmat4() * glm::transpose(glm::dmat4());
  1071. ```
  1072. From GLM core library: `<glm/glm.hpp>`
  1073. [***glFrustum:***](http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml)
  1074. ```cpp
  1075. glm::mat4 glm::frustum(float left, float right, float bottom, float top, float zNear, float zFar);
  1076. glm::dmat4 glm::frustum(double left, double right, double bottom, double top, double zNear, double zFar);
  1077. ```
  1078. From `GLM_GTC_matrix_transform` extension: `<glm/gtc/matrix_transform.hpp>`
  1079. [***glOrtho:***](https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml)
  1080. ```cpp
  1081. glm::mat4 glm::ortho(float left, float right, float bottom, float top, float zNear, float zFar);
  1082. glm::dmat4 glm::ortho(double left, double right, double bottom, double top, double zNear, double zFar);
  1083. ```
  1084. From `GLM_GTC_matrix_transform` extension: `<glm/gtc/matrix_transform.hpp>`
  1085. ### <a name="section5_2"></a> 5.2. GLM replacements for GLU functions
  1086. [***gluLookAt:***](https://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml)
  1087. ```cpp
  1088. glm::mat4 glm::lookAt(glm::vec3 const& eye, glm::vec3 const& center, glm::vec3 const& up);
  1089. glm::dmat4 glm::lookAt(glm::dvec3 const& eye, glm::dvec3 const& center, glm::dvec3 const& up);
  1090. ```
  1091. From `GLM_GTC_matrix_transform` extension: `<glm/gtc/matrix_transform.hpp>`
  1092. [***gluOrtho2D:***](https://www.opengl.org/sdk/docs/man2/xhtml/gluOrtho2D.xml)
  1093. ```cpp
  1094. glm::mat4 glm::ortho(float left, float right, float bottom, float top);
  1095. glm::dmat4 glm::ortho(double left, double right, double bottom, double top);
  1096. ```
  1097. From `GLM_GTC_matrix_transform` extension: `<glm/gtc/matrix_transform.hpp>`
  1098. [***gluPerspective:***](https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml)
  1099. ```cpp
  1100. glm::mat4 perspective(float fovy, float aspect, float zNear, float zFar);
  1101. glm::dmat4 perspective(double fovy, double aspect, double zNear, double zFar);
  1102. ```
  1103. Note that in GLM, fovy is expressed in radians, not degrees.
  1104. From `GLM_GTC_matrix_transform` extension: `<glm/gtc/matrix_transform.hpp>`
  1105. [***gluPickMatrix:***](https://www.opengl.org/sdk/docs/man2/xhtml/gluPickMatrix.xml)
  1106. ```cpp
  1107. glm::mat4 pickMatrix(glm::vec2 const& center, glm::vec2 const& delta, glm::ivec4 const& viewport);
  1108. glm::dmat4 pickMatrix(glm::dvec2 const& center, glm::dvec2 const& delta, glm::ivec4 const& viewport);
  1109. ```
  1110. From `GLM_GTC_matrix_transform` extension: `<glm/gtc/matrix_transform.hpp>`
  1111. [***gluProject:***](http://www.opengl.org/sdk/docs/man2/xhtml/gluProject.xml)
  1112. ```cpp
  1113. glm::vec3 project(glm::vec3 const& obj, glm::mat4 const& model, glm::mat4 const& proj, glm::ivec4 const& viewport);
  1114. glm::dvec3 project(glm::dvec3 const& obj, glm::dmat4 const& model, glm::dmat4 const& proj, glm::ivec4 const& viewport);
  1115. ```
  1116. From `GLM_GTC_matrix_transform` extension: `<glm/gtc/matrix_transform.hpp>`
  1117. [***gluUnProject:***](https://www.opengl.org/sdk/docs/man2/xhtml/gluUnProject.xml)
  1118. ```cpp
  1119. glm::vec3 unProject(glm::vec3 const& win, glm::mat4 const& model, glm::mat4 const& proj, glm::ivec4 const& viewport);
  1120. glm::dvec3 unProject(glm::dvec3 const& win, glm::dmat4 const& model, glm::dmat4 const& proj, glm::ivec4 const& viewport);
  1121. ```
  1122. From `GLM_GTC_matrix_transform` extension: `<glm/gtc/matrix_transform.hpp>`
  1123. ---
  1124. <div style="page-break-after: always;"> </div>
  1125. ## <a name="section6"></a> 6. Known issues
  1126. This section reports GLSL features that GLM can't accurately emulate due to language restrictions.
  1127. ### <a name="section6_1"></a> 6.1. not function
  1128. The GLSL function 'not' is a keyword in C++. To prevent name collisions and ensure a consistent API, the name `not\_` (note the underscore) is used instead.
  1129. ### <a name="section6_2"></a> 6.2. Precision qualifiers support
  1130. GLM supports GLSL precision qualifiers through prefixes instead of qualifiers. For example, GLM exposes \verb|lowp_vec4|, \verb|mediump_vec4| and \verb|highp_vec4| as variations of \verb|vec4|.
  1131. Similarly to GLSL, GLM precision qualifiers are used to trade precision of operations in term of [ULPs](http://en.wikipedia.org/wiki/Unit_in_the_last_place) for better performance. By default, all the types use high precision.
  1132. ```cpp
  1133. // Using precision qualifier in GLSL:
  1134. ivec3 foo(in vec4 v)
  1135. {
  1136. highp vec4 a = v;
  1137. mediump vec4 b = a;
  1138. lowp ivec3 c = ivec3(b);
  1139. return c;
  1140. }
  1141. // Using precision qualifier in GLM:
  1142. #include <glm/glm.hpp>
  1143. ivec3 foo(const vec4 & v)
  1144. {
  1145. highp_vec4 a = v;
  1146. medium_vec4 b = a;
  1147. lowp_ivec3 c = glm::ivec3(b);
  1148. return c;
  1149. }
  1150. ```
  1151. ---
  1152. <div style="page-break-after: always;"> </div>
  1153. ## <a name="section7"></a> 7. FAQ
  1154. ### <a name="section7_1"></a> 7.1 Why GLM follows GLSL specification and conventions?
  1155. Following GLSL conventions is a really strict policy of GLM. It has been designed following the idea that everyone does its own math library with his own conventions. The idea is that brilliant developers (the OpenGL ARB) worked together and agreed to make GLSL. Following GLSL conventions
  1156. is a way to find consensus. Moreover, basically when a developer knows GLSL, he knows GLM.
  1157. ### <a name="section7_2"></a> 7.2. Does GLM run GLSL program?
  1158. No, GLM is a C++ implementation of a subset of GLSL.
  1159. ### <a name="section7_3"></a> 7.3. Does a GLSL compiler build GLM codes?
  1160. No, this is not what GLM attends to do.
  1161. ### <a name="section7_4"></a> 7.4. Should I use ‘GTX’ extensions?
  1162. GTX extensions are qualified to be experimental extensions. In GLM this means that these extensions might change from version to version without any restriction. In practice, it doesn’t really change except time to
  1163. time. GTC extensions are stabled, tested and perfectly reliable in time. Many GTX extensions extend GTC extensions and provide a way to explore features and implementations and APIs and then are promoted to GTC
  1164. extensions. This is fairly the way OpenGL features are developed; through extensions.
  1165. Stating with GLM 0.9.9, to use experimental extensions, an application must define GLM_ENABLE_EXPERIMENTAL.
  1166. ### <a name="section7_5"></a> 7.5. Where can I ask my questions?
  1167. A good place is [stackoverflow](http://stackoverflow.com/search?q=GLM) using the GLM tag.
  1168. ### <a name="section7_6"></a> 7.6. Where can I find the documentation of extensions?
  1169. The Doxygen generated documentation includes a complete list of all extensions available. Explore this [*API documentation*](http://glm.g-truc.net/html/index.html) to get a complete
  1170. view of all GLM capabilities!
  1171. ### <a name="section7_7"></a> 7.7. Should I use ‘using namespace glm;’?
  1172. NO! Chances are that if using namespace glm; is called, especially in a header file, name collisions will happen as GLM is based on GLSL which uses common tokens for types and functions. Avoiding using namespace
  1173. glm; will a higher compatibility with third party library and SDKs.
  1174. ### <a name="section7_8"></a> 7.8. Is GLM fast?
  1175. GLM is mainly designed to be convenient and that's why it is written against the GLSL specification.
  1176. Following the Pareto principle where 20% of the code consumes 80% of the execution time, GLM operates perfectly on the 80% of the code that consumes 20% of the performances. Furthermore, thanks to the lowp,
  1177. mediump and highp qualifiers, GLM provides approximations which trade precision for performance. Finally, GLM can automatically produce SIMD optimized code for functions of its implementation.
  1178. However, on performance critical code paths, we should expect that dedicated algorithms should be written to reach peak performance.
  1179. ### <a name="section7_9"></a> 7.9. When I build with Visual C++ with /W4 warning level, I have warnings...
  1180. You should not have any warnings even in `/W4` mode. However, if you expect such level for your code, then you should ask for the same level to the compiler by at least disabling the Visual C++ language extensions
  1181. (`/Za`) which generates warnings when used. If these extensions are enabled, then GLM will take advantage of them and the compiler will generate warnings.
  1182. ### <a name="section7_10"></a> 7.10. Why some GLM functions can crash because of division by zero?
  1183. GLM functions crashing is the result of a domain error. Such behavior follows the precedent set by C and C++'s standard library. For example, it’s a domain error to pass a null vector (all zeroes) to glm::normalize function, or to pass a negative number into std::sqrt.
  1184. ### <a name="section7_11"></a> 7.11. What unit for angles is used in GLM?
  1185. GLSL is using radians but GLU is using degrees to express angles. This has caused GLM to use inconsistent units for angles. Starting with GLM 0.9.6, all GLM functions are using radians. For more information, follow
  1186. the [link](http://www.g-truc.net/post-0693.html#menu).
  1187. ### <a name="section7_12"></a> 7.12. Windows headers cause build errors...
  1188. Some Windows headers define min and max as macros which may cause compatibility with third party libraries such as GLM.
  1189. It is highly recommended to [`define NOMINMAX`](http://stackoverflow.com/questions/4913922/possible-problems-with-nominmax-on-visual-c) before including Windows headers to workaround this issue.
  1190. To workaround the incompatibility with these macros, GLM will systematically undef these macros if they are defined.
  1191. ### <a name="section7_13"></a> 7.13. Constant expressions support
  1192. GLM has some C++ <a href="http://en.cppreference.com/w/cpp/language/constexpr">constant expressions</a> support. However, GLM automatically detects the use of SIMD instruction sets through compiler arguments to populate its implementation with SIMD intrinsics.
  1193. Unfortunately, GCC and Clang doesn't support SIMD instrinsics as constant expressions. To allow constant expressions on all vectors and matrices types, define `GLM_FORCE_PURE` before including GLM headers.
  1194. ---
  1195. <div style="page-break-after: always;"> </div>
  1196. ## <a name="section8"></a> 8. Code samples
  1197. This series of samples only shows various GLM features without consideration of any sort.
  1198. ### <a name="section8_1"></a> 8.1. Compute a triangle normal
  1199. ```cpp
  1200. #include <glm/glm.hpp> // vec3 normalize cross
  1201. glm::vec3 computeNormal(glm::vec3 const& a, glm::vec3 const& b, glm::vec3 const& c)
  1202. {
  1203. return glm::normalize(glm::cross(c - a, b - a));
  1204. }
  1205. // A much faster but less accurate alternative:
  1206. #include <glm/glm.hpp> // vec3 cross
  1207. #include <glm/gtx/fast_square_root.hpp> // fastNormalize
  1208. glm::vec3 computeNormal(glm::vec3 const& a, glm::vec3 const& b, glm::vec3 const& c)
  1209. {
  1210. return glm::fastNormalize(glm::cross(c - a, b - a));
  1211. }
  1212. ```
  1213. ### <a name="section8_2"></a> 8.2. Matrix transform
  1214. ```cpp
  1215. #include <glm/glm.hpp> // vec3, vec4, ivec4, mat4
  1216. #include <glm/gtc/matrix_transform.hpp> // translate, rotate, scale, perspective
  1217. #include <glm/gtc/type_ptr.hpp> // value_ptr
  1218. void setUniformMVP(GLuint Location, glm::vec3 const& Translate, glm::vec3 const& Rotate)
  1219. {
  1220. glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f);
  1221. glm::mat4 ViewTranslate = glm::translate(
  1222. glm::mat4(1.0f), Translate);
  1223. glm::mat4 ViewRotateX = glm::rotate(
  1224. ViewTranslate, Rotate.y, glm::vec3(-1.0f, 0.0f, 0.0f));
  1225. glm::mat4 View = glm::rotate(ViewRotateX,
  1226. Rotate.x, glm::vec3(0.0f, 1.0f, 0.0f));
  1227. glm::mat4 Model = glm::scale(
  1228. glm::mat4(1.0f), glm::vec3(0.5f));
  1229. glm::mat4 MVP = Projection * View * Model;
  1230. glUniformMatrix4fv(Location, 1, GL_FALSE, glm::value_ptr(MVP));
  1231. }
  1232. ```
  1233. ### <a name="section8_3"></a> 8.3. Vector types
  1234. ```cpp
  1235. #include <glm/glm.hpp> // vec2
  1236. #include <glm/gtc/type_precision.hpp> // hvec2, i8vec2, i32vec2
  1237. std::size_t const VertexCount = 4;
  1238. // Float quad geometry
  1239. std::size_t const PositionSizeF32 = VertexCount * sizeof(glm::vec2);
  1240. glm::vec2 const PositionDataF32[VertexCount] =
  1241. {
  1242. glm::vec2(-1.0f,-1.0f),
  1243. glm::vec2( 1.0f,-1.0f),
  1244. glm::vec2( 1.0f, 1.0f),
  1245. glm::vec2(-1.0f, 1.0f)
  1246. };
  1247. // Half-float quad geometry
  1248. std::size_t const PositionSizeF16 = VertexCount * sizeof(glm::hvec2);
  1249. glm::hvec2 const PositionDataF16[VertexCount] =
  1250. {
  1251. glm::hvec2(-1.0f, -1.0f),
  1252. glm::hvec2( 1.0f, -1.0f),
  1253. glm::hvec2( 1.0f, 1.0f),
  1254. glm::hvec2(-1.0f, 1.0f)
  1255. };
  1256. // 8 bits signed integer quad geometry
  1257. std::size_t const PositionSizeI8 = VertexCount * sizeof(glm::i8vec2);
  1258. glm::i8vec2 const PositionDataI8[VertexCount] =
  1259. {
  1260. glm::i8vec2(-1,-1),
  1261. glm::i8vec2( 1,-1),
  1262. glm::i8vec2( 1, 1),
  1263. glm::i8vec2(-1, 1)
  1264. };
  1265. // 32 bits signed integer quad geometry
  1266. std::size_t const PositionSizeI32 = VertexCount * sizeof(glm::i32vec2);
  1267. glm::i32vec2 const PositionDataI32[VertexCount] =
  1268. {
  1269. glm::i32vec2(-1,-1),
  1270. glm::i32vec2( 1,-1),
  1271. glm::i32vec2( 1, 1),
  1272. glm::i32vec2(-1, 1)
  1273. };
  1274. ```
  1275. ### <a name="section8_4"></a> 8.4. Lighting
  1276. ```cpp
  1277. #include <glm/glm.hpp> // vec3 normalize reflect dot pow
  1278. #include <glm/gtc/random.hpp> // ballRand
  1279. // vecRand3, generate a random and equiprobable normalized vec3
  1280. glm::vec3 lighting(intersection const& Intersection, material const& Material, light const& Light, glm::vec3 const& View)
  1281. {
  1282. glm::vec3 Color = glm::vec3(0.0f);
  1283. glm::vec3 LightVertor = glm::normalize(
  1284. Light.position() - Intersection.globalPosition() +
  1285. glm::ballRand(0.0f, Light.inaccuracy());
  1286. if(!shadow(Intersection.globalPosition(), Light.position(), LightVertor))
  1287. {
  1288. float Diffuse = glm::dot(Intersection.normal(), LightVector);
  1289. if(Diffuse &lt;= 0.0f)
  1290. return Color;
  1291. if(Material.isDiffuse())
  1292. Color += Light.color() * Material.diffuse() * Diffuse;
  1293. if(Material.isSpecular())
  1294. {
  1295. glm::vec3 Reflect = glm::reflect(-LightVector, Intersection.normal());
  1296. float Dot = glm::dot(Reflect, View);
  1297. float Base = Dot &gt; 0.0f ? Dot : 0.0f;
  1298. float Specular = glm::pow(Base, Material.exponent());
  1299. Color += Material.specular() \* Specular;
  1300. }
  1301. }
  1302. return Color;
  1303. }
  1304. ```
  1305. ---
  1306. <div style="page-break-after: always;"> </div>
  1307. ## <a name="section9"></a> 9. Contributing to GLM
  1308. ### <a name="section9_1"></a> 9.1. Submitting bug reports
  1309. Bug should be reported on Github using the [issue page](https://github.com/g-truc/glm/issues).
  1310. A minimal code to reproduce the issue will help.
  1311. Additional, bugs can be configuration specific. We can report the configuration by defining `GLM_FORCE_MESSAGES` before including GLM headers then build and copy paste the build messages GLM will output.
  1312. ```cpp
  1313. #define GLM_FORCE_MESSAGES
  1314. #include <glm/glm.hpp>
  1315. ```
  1316. An example of build messages generated by GLM:
  1317. ```
  1318. GLM: 0.9.9.1
  1319. GLM: C++ 17 with extensions
  1320. GLM: GCC compiler detected"
  1321. GLM: x86 64 bits with AVX instruction set build target
  1322. GLM: Linux platform detected
  1323. GLM: GLM_FORCE_SWIZZLE is undefined. swizzling functions or operators are disabled.
  1324. GLM: GLM_FORCE_SIZE_T_LENGTH is undefined. .length() returns a glm::length_t, a typedef of int following GLSL.
  1325. GLM: GLM_FORCE_UNRESTRICTED_GENTYPE is undefined. Follows strictly GLSL on valid function genTypes.
  1326. GLM: GLM_FORCE_DEPTH_ZERO_TO_ONE is undefined. Using negative one to one depth clip space.
  1327. GLM: GLM_FORCE_LEFT_HANDED is undefined. Using right handed coordinate system.
  1328. ```
  1329. ### <a name="section9_2"></a> 9.2. Contributing to GLM with pull request
  1330. This tutorial will show us how to successfully contribute a bug-fix to GLM using GitHub's Pull Request workflow.
  1331. We will be typing git commands in the Terminal. Mac and Linux users may have git pre-installed. You can download git from [here](http://git-scm.com/downloads).
  1332. The tutorial assumes you have some basic understanding of git concepts - repositories, branches, commits, etc. Explaining it all from scratch is beyond the scope of this tutorial. Some good links to learn git basics are: [Link 1](http://git-scm.com/book/en/Getting-Started-Git-Basics), [Link 2](https://www.atlassian.com/git/tutorial/git-basics)
  1333. #### Step 1: Setup our GLM Fork
  1334. We will make our changes in our own copy of the GLM sitory. On the GLM GitHub repo and we press the Fork button.
  1335. We need to download a copy of our fork to our local machine. In the terminal, type:
  1336. ```
  1337. >>> git clone <our-repository-fork-git-url>
  1338. ```
  1339. This will clone our fork repository into the current folder.
  1340. We can find our repository git url on the Github reposotory page. The url looks like this: `https://github.com/<our-username>/<repository-name>.git`
  1341. #### Step 2: Synchronizing our fork
  1342. We can use the following command to add `upstream` (original project repository) as a remote repository so that we can fetch the latest GLM commits into our branch and keep our forked copy is synchronized.
  1343. ```
  1344. >>> git remote add upstream https://github.com/processing/processing.git
  1345. ```
  1346. To synchronize our fork to the latest commit in the GLM repository, we can use the following command:
  1347. ```
  1348. >>> git fetch upstream
  1349. ```
  1350. Then, we can merge the remote master branch to our current branch:
  1351. ```
  1352. >>> git merge upstream/master
  1353. ```
  1354. Now our local copy of our fork has been synchronized. However, the fork's copy is not updated on GitHub's servers yet. To do that, use:
  1355. ```
  1356. >>> git push origin master
  1357. ```
  1358. #### Step 3: Modifying our GLM Fork
  1359. Our fork is now setup and we are ready to modify GLM to fix a bug.
  1360. It's a good practice to make changes in our fork in a separate branch than the master branch because we can submit only one pull request per branch.
  1361. Before creating a new branch, it's best to synchronize our fork and then create a new branch from the latest master branch.
  1362. If we are not on the master branch, we should switch to it using:
  1363. ```
  1364. >>> git checkout master
  1365. ```
  1366. To create a new branch called `bugifx`, we use:
  1367. ```
  1368. git branch bugfix
  1369. ```
  1370. Once the code changes for the fix is done, we need to commit the changes:
  1371. ```
  1372. >>> git commit -m "Resolve the issue that caused problem with a specific fix #432"
  1373. ```
  1374. The commit message should be as specific as possible and finished by the bug number in the [GLM GitHub issue page](https://github.com/g-truc/glm/issues)
  1375. Finally, we need to push our changes in our branch to our GitHub fork using:
  1376. ```
  1377. >>> git push origin bugfix
  1378. ```
  1379. Some things to keep in mind for a pull request:
  1380. * Keep it minimal: Try to make the minimum required changes to fix the issue. If we have added any debugging code, we should remove it.
  1381. * A fix at a time: The pull request should deal with one issue at a time only, unless two issue are so interlinked they must be fixed together.
  1382. * Write a test: GLM is largely unit tests. Unit tests are in `glm/test` directory. We should also add tests for the fixes we provide to ensure future regression doesn't happen.
  1383. * No whitespace changes: Avoid unnecessary formatting or whitespace changes in other parts of the code. Be careful with auto-format options in the code editor which can cause wide scale formatting changes.
  1384. * Follow [GLM Code Style](#section9_3) for consistency.
  1385. * Tests passes: Make sure GLM build and tests don't fail because of the changes.
  1386. #### Step 4: Submitting a Pull Request
  1387. We need to submit a pull request from the `bugfix` branch to GLM's master branch.
  1388. On the fork github page, we can click on the *Pull Request* button. Then we can describe our pull request. Finally we press *Send Pull Request*.
  1389. Please be patient and give them some time to go through it.
  1390. The pull request review may suggest additional changes. So we can make those changes in our branch, and push those changes to our fork repository. Our pull request will always include the latest changes in our branch on GitHub, so we don't need to resubmit the pull request.
  1391. Once your changes have been accepted, a project maintainer will merge our pull request.
  1392. We are grateful to the users for their time and effort in contributing fixes.
  1393. ### <a name="section9_3"></a> 9.3. Coding style
  1394. #### Indentation
  1395. Always tabs. Never spaces.
  1396. #### Spacing
  1397. No space after if. Use if(blah) not if (blah). Example if/else block:
  1398. ```cpp
  1399. if(blah)
  1400. {
  1401. // yes like this
  1402. }
  1403. else
  1404. {
  1405. // something besides
  1406. }
  1407. ```
  1408. Single line if blocks:
  1409. ```cpp
  1410. if(blah)
  1411. // yes like this
  1412. else
  1413. // something besides
  1414. ```
  1415. No spaces inside parens:
  1416. ```cpp
  1417. if (blah) // No
  1418. if( blah ) // No
  1419. if ( blah ) // No
  1420. if(blah) // Yes
  1421. ```
  1422. Use spaces before/after commas:
  1423. ```cpp
  1424. someFunction(apple,bear,cat); // No
  1425. someFunction(apple, bear, cat); // Yes
  1426. ```
  1427. Use spaces before/after use of `+, -, *, /, %, >>, <<, |, &, ^, ||, &&` operators:
  1428. ```cpp
  1429. vec4 v = a + b;
  1430. ```
  1431. #### Blank lines
  1432. One blank line after the function blocks.
  1433. #### Comments
  1434. Always one space after the // in single line comments
  1435. One space before // at the end of a line (that has code as well)
  1436. Try to use // comments inside functions, to make it easier to remove a whole block via /* */
  1437. #### Cases
  1438. ```cpp
  1439. #define GLM_MY_DEFINE 76
  1440. class myClass
  1441. {};
  1442. myClass const MyClass;
  1443. namespace glm{ // glm namespace is for public code
  1444. namespace detail // glm::detail namespace is for implementation detail
  1445. {
  1446. float myFunction(vec2 const& V)
  1447. {
  1448. return V.x + V.y;
  1449. }
  1450. float myFunction(vec2 const* const V)
  1451. {
  1452. return V->x + V->y;
  1453. }
  1454. }//namespace detail
  1455. }//namespace glm
  1456. ```
  1457. ---
  1458. <div style="page-break-after: always;"> </div>
  1459. ## <a name="section10"></a> 10. References
  1460. ### <a name="section10_1"></a> 10.1. OpenGL specifications
  1461. * OpenGL 4.3 core specification
  1462. * [GLSL 4.30 specification](http://www.opengl.org/registry/doc/GLSLangSpec.4.30.7.diff.pdf)
  1463. ![](media/image21.png){width="2.859722222222222in" height="1.6083333333333334in"}- [*GLU 1.3 specification*](http://www.opengl.org/documentation/specs/glu/glu1_3.pdf)
  1464. ### <a name="section10_2"></a> 10.2. External links
  1465. * [GLM on stackoverflow](http://stackoverflow.com/search?q=GLM)
  1466. ### <a name="section10_3"></a> 10.3. Projects using GLM
  1467. ***[Leo’s Fortune](http://www.leosfortune.com/)***
  1468. Leo’s Fortune is a platform adventure game where you hunt down the cunning and mysterious thief that stole your gold. Available on PS4, Xbox One, PC, Mac, iOS and Android.
  1469. Beautifully hand-crafted levels bring the story of Leo to life in this epic adventure.
  1470. “I just returned home to find all my gold has been stolen! For some devious purpose, the thief has dropped pieces of my gold like breadcrumbs through the woods.”
  1471. “Despite this pickle of a trap, I am left with no choice but to follow the trail.”
  1472. “Whatever lies ahead, I must recover my fortune.” -Leopold
  1473. ![](/doc/manual/references-leosfortune.jpeg)
  1474. ![](/doc/manual/references-leosfortune2.jpg)
  1475. [***OpenGL 4.0 Shading Language Cookbook***](http://www.packtpub.com/opengl-4-0-shading-language-cookbook/book?tag=rk/opengl4-abr1/0811)
  1476. A set of recipes that demonstrates a wide of techniques for producing high-quality, real-time 3D graphics with GLSL 4.0, such as:
  1477. * Using GLSL 4.0 to implement lighting and shading techniques.
  1478. * Using the new features of GLSL 4.0 including tessellation and geometry shaders.
  1479. * Using textures in GLSL as part of a wide variety of techniques from basic texture mapping to deferred shading.
  1480. Simple, easy-to-follow examples with GLSL source code are provided, as well as a basic description of the theory behind each technique.
  1481. ![](/doc/manual/references-glsl4book.jpg)
  1482. [***Outerra***](http://outerra.com/)
  1483. A 3D planetary engine for seamless planet rendering from space down to the surface. Can use arbitrary resolution of elevation data, refining it to centimetre resolution using fractal algorithms.
  1484. ![](/doc/manual/references-outerra1.jpg)
  1485. ![](/doc/manual/references-outerra2.jpg)
  1486. ![](/doc/manual/references-outerra3.jpg)
  1487. ![](/doc/manual/references-outerra4.jpg)
  1488. [***Falcor***](https://github.com/NVIDIA/Falcor)
  1489. Real-time rendering research framework by NVIDIA.
  1490. [***Cinder***](https://libcinder.org/)
  1491. Cinder is a free and open source library for professional-quality creative coding in C++.
  1492. Cinder is a C++ library for programming with aesthetic intent - the sort of development often called creative coding. This includes domains like graphics, audio, video, and computational geometry. Cinder is cross-platform, with official support for OS X, Windows, iOS, and WinRT.
  1493. Cinder is production-proven, powerful enough to be the primary tool for professionals, but still suitable for learning and experimentation. Cinder is released under the [2-Clause BSD License](http://opensource.org/licenses/BSD-2-Clause).
  1494. ![](/doc/manual/references-cinder.png)
  1495. [***opencloth***](http://code.google.com/p/opencloth/)
  1496. A collection of source codes implementing cloth simulation algorithms in OpenGL.
  1497. Simple, easy-to-follow examples with GLSL source code, as well as a basic description of the theory behind each technique.
  1498. ![](/doc/manual/references-opencloth1.png)
  1499. ![](/doc/manual/references-opencloth3.png)
  1500. [***LibreOffice***](https://www.libreoffice.org/)
  1501. LibreOffice includes several applications that make it the most powerful Free and Open Source office suite on the market.
  1502. [***Are you using GLM in a project?***](mailto:glm@g-truc.net)
  1503. ### <a name="section10_4"></a> 10.4. Tutorials using GLM
  1504. * [Sascha Willems' Vulkan examples](https://github.com/SaschaWillems/Vulkan), Examples and demos for the new Vulkan API
  1505. * [VKTS](https://github.com/McNopper/Vulkan) Vulkan examples using VulKan ToolS (VKTS)
  1506. * [*The OpenGL Samples Pack*](http://www.g-truc.net/project-0026.html#menu), samples that show how to set up all the different new features
  1507. * [*Learning Modern 3D Graphics programming*](http://www.arcsynthesis.org/gltut/), a great OpenGL tutorial using GLM by Jason L. McKesson
  1508. * [*Morten Nobel-Jørgensen’s*](http://blog.nobel-joergensen.com/2011/04/02/glm-brilliant-math-library-for-opengl/) review and use an [*OpenGL renderer*](https://github.com/mortennobel/RenderE)
  1509. * [*Swiftless’ OpenGL tutorial*](http://www.swiftless.com/opengltuts.html) using GLM by Donald Urquhart
  1510. * [*Rastergrid*](http://rastergrid.com/blog/), many technical articles with companion programs using GLM by Daniel Rákos\
  1511. * [*OpenGL Tutorial*](http://www.opengl-tutorial.org), tutorials for OpenGL 3.1 and later
  1512. * [*OpenGL Programming on Wikibooks*](http://en.wikibooks.org/wiki/OpenGL_Programming): For beginners who are discovering OpenGL.
  1513. * [*3D Game Engine Programming*](http://3dgep.com/): Learning the latest 3D Game Engine Programming techniques.
  1514. * [Game Tutorials](http://www.gametutorials.com/opengl-4-matrices-and-glm/), graphics and game programming.
  1515. * [open.gl](https://open.gl/), OpenGL tutorial
  1516. * [c-jump](http://www.c-jump.com/bcc/common/Talk3/Math/GLM/GLM.html), GLM tutorial
  1517. * [Learn OpenGL](http://learnopengl.com/), OpenGL tutorial
  1518. * [***Are you using GLM in a tutorial?***](mailto:glm@g-truc.net)
  1519. ### <a name="section10_5"></a> 10.5. Equivalent for other languages
  1520. * [*cglm*](https://github.com/recp/cglm): OpenGL Mathematics (glm) for C.
  1521. * [*GlmSharp*](https://github.com/Philip-Trettner/GlmSharp): Open-source semi-generated GLM-flavored math library for .NET/C\#.
  1522. * [glm-js](https://github.com/humbletim/glm-js): JavaScript adaptation of the OpenGL Mathematics (GLM) C++ library interfaces
  1523. * [JVM OpenGL Mathematics (GLM)](https://github.com/kotlin-graphics/glm): written in Kotlin, Java compatible
  1524. * [JGLM](https://github.com/jroyalty/jglm) - Java OpenGL Mathematics Library
  1525. * [SwiftGL Math Library](https://github.com/SwiftGL/Math/blob/master/Sources/glm.swift) GLM for Swift
  1526. * [glm-go](https://github.com/jbowtie/glm-go): Simple linear algebra library similar in spirit to GLM
  1527. * [openll](https://github.com/Polkm/openll): Lua bindings for OpenGL, GLM, GLFW, OpenAL, SOIL and PhysicsFS
  1528. * [glm-rs](https://github.com/dche/glm-rs): GLSL mathematics for Rust programming language
  1529. * [glmpython](https://github.com/Queatz/glmpython): GLM math library for Python
  1530. ### <a name="section10_6"></a> 10.6. Alternatives to GLM
  1531. * [*CML*](http://cmldev.net/): The CML (Configurable Math Library) is a free C++ math library for games and graphics.
  1532. * [*Eigen*](http://eigen.tuxfamily.org/): A more heavy weight math library for general linear algebra in C++.
  1533. * [*glhlib*](http://glhlib.sourceforge.net/): A much more than glu C library.
  1534. * Are you using or developing an alternative library to GLM?
  1535. ### <a name="section10_7"></a> 10.7. Acknowledgements
  1536. GLM is developed and maintained by [*Christophe Riccio*](http://www.g-truc.net) but many contributors have made this project what it is.
  1537. Special thanks to:
  1538. * Ashima Arts and Stefan Gustavson for their work on [*webgl-noise*](https://github.com/ashima/webgl-noise) which has been used for GLM noises implementation.
  1539. * [*Arthur Winters*](http://athile.net/library/wiki/index.php?title=Athile_Technologies) for the C++11 and Visual C++ swizzle operators implementation and tests.
  1540. * Joshua Smith and Christoph Schied for the discussions and the experiments around the swizzle operators implementation issues.
  1541. * Guillaume Chevallereau for providing and maintaining the [*nightlight build system*](http://my.cdash.org/index.php?project=GLM).
  1542. * Ghenadii Ursachi for GLM\_GTX\_matrix\_interpolation implementation.
  1543. * Mathieu Roumillac for providing some implementation ideas.
  1544. * [*Grant James*](http://www.zeuscmd.com/) for the implementation of all combination of none-squared matrix products.
  1545. * Jesse Talavera-Greenberg for his work on the manual amount other things.
  1546. * All the GLM users that have report bugs and hence help GLM to become a great library!