Core GLM features can be included using individual headers to allow faster user program compilations.
Core GLM features can be included using individual headers to allow faster user program compilations.
* <glm/vec2.hpp>: vec2, bvec2, dvec2, ivec2 and uvec2
```cpp
* <glm/vec3.hpp>: vec3, bvec3, dvec3, ivec3 and uvec3
#include<glm/vec2.hpp> // vec2, bvec2, dvec2, ivec2 and uvec2
* <glm/vec4.hpp>: vec4, bvec4, dvec4, ivec4 and uvec4
#include<glm/vec3.hpp> // vec3, bvec3, dvec3, ivec3 and uvec3
* <glm/mat2x2.hpp>: mat2, dmat2
#include<glm/vec4.hpp> // vec4, bvec4, dvec4, ivec4 and uvec4
* <glm/mat2x3.hpp>: mat2x3, dmat2x3
#include<glm/mat2x2.hpp> // mat2, dmat2
* <glm/mat2x4.hpp>: mat2x4, dmat2x4
#include<glm/mat2x3.hpp> // mat2x3, dmat2x3
* <glm/mat3x2.hpp>: mat3x2, dmat3x2
#include<glm/mat2x4.hpp> // mat2x4, dmat2x4
* <glm/mat3x3.hpp>: mat3, dmat3
#include<glm/mat3x2.hpp> // mat3x2, dmat3x2
* <glm/mat3x4.hpp>: mat3x4, dmat2
#include<glm/mat3x3.hpp> // mat3, dmat3
* <glm/mat4x2.hpp>: mat4x2, dmat4x2
#include<glm/mat3x4.hpp> // mat3x4, dmat2
* <glm/mat4x3.hpp>: mat4x3, dmat4x3
#include<glm/mat4x2.hpp> // mat4x2, dmat4x2
* <glm/mat4x4.hpp>: mat4, dmat4
#include<glm/mat4x3.hpp> // mat4x3, dmat4x3
* <glm/common.hpp>: all the GLSL common functions
#include<glm/mat4x4.hpp> // mat4, dmat4
* <glm/exponential.hpp>: all the GLSL exponential functions
#include<glm/common.hpp> // all the GLSL common functions
* <glm/geometry.hpp>: all the GLSL geometry functions
#include<glm/exponential.hpp> // all the GLSL exponential functions
* <glm/integer.hpp>: all the GLSL integer functions
#include<glm/geometry.hpp> // all the GLSL geometry functions
* <glm/matrix.hpp>: all the GLSL matrix functions
#include<glm/integer.hpp> // all the GLSL integer functions
* <glm/packing.hpp>: all the GLSL packing functions
#include<glm/matrix.hpp> // all the GLSL matrix functions
* <glm/trigonometric.hpp>: all the GLSL trigonometric functions
#include<glm/packing.hpp> // all the GLSL packing functions
* <glm/vector\_relational.hpp>: all the GLSL vector relational functions
#include<glm/trigonometric.hpp> // all the GLSL trigonometric functions
#include<glm/vector\_relational.hpp> // all the GLSL vector relational functions
```
### 1.2. Faster program compilation
### 1.2. Faster program compilation
@ -140,8 +142,7 @@ GLM is a header only library that makes a heavy usage of C++ templates.
This design may significantly increase the compile time for files that use GLM. Hence, it is important to limit GLM inclusion to header and source files that actually use it. Likewise, GLM extensions should be
This design may significantly increase the compile time for files that use GLM. Hence, it is important to limit GLM inclusion to header and source files that actually use it. Likewise, GLM extensions should be
included only in program sources using them.
included only in program sources using them.
To further help compilation time, GLM 0.9.5 introduced
To further help compilation time, GLM 0.9.5 introduced <glm/fwd.hpp> that provides forward declarations of GLM types.
<glm/fwd.hpp> that provides forward declarations of GLM types.
```cpp
```cpp
// Header file
// Header file
@ -298,66 +299,46 @@ functions so that the programmer must convert a swizzle operators to a
vector type or call the () operator on a swizzle objects to pass it to
vector type or call the () operator on a swizzle objects to pass it to
// We need to cast the swizzle operator into glm::vec4
----------------------
// With by using a constructor
glm::vec4 ClampedB = glm::clamp(glm::vec4(Color.rgba), 0.f, 1.f); // OK
In C++, it is not possible to implement GLSL default precision (GLSL
// Or by using the () operator
4.10 specification section 4.5.3) using GLSL syntax.
glm::vec4 ClampedC = glm::clamp(Color.rgba(), 0.f, 1.f); // OK
...
}
```
------------------------
## 3. Preprocessor options
precision mediump int;
precision highp float;
### 3.1. Default precision
------------------------
------------------------
To use the default precision functionality, GLM provides some defines
In C++, it is not possible to implement GLSL default precision (GLSL 4.10 specification section 4.5.3) using GLSL syntax.
that need to add before any include of glm.hpp:
----------------------------------------
```cpp
\#define GLM\_PRECISION\_MEDIUMP\_INT;
precision mediump int;
precision highp float;
```
\#define GLM\_PRECISION\_HIGHP\_FLOAT;
To use the default precision functionality, GLM provides some defines that need to add before any include of glm.hpp:
\#include <glm/glm.hpp>
```cpp
----------------------------------------
#define GLM_PRECISION_MEDIUMP_INT
----------------------------------------
#define GLM_PRECISION_HIGHP_FLOAT
#include<glm/glm.hpp>
```
Available defines for floating point types (glm::vec\*, glm::mat\*):
Available defines for floating point types (glm::vec\*, glm::mat\*):
@ -374,53 +355,36 @@ GLM\_PRECISION\_HIGHP\_DOUBLE: High precision (default)
Available defines for signed integer types (glm::ivec\*):
Available defines for signed integer types (glm::ivec\*):
GLM\_PRECISION\_LOWP\_INT: Low precision
GLM\_PRECISION\_LOWP\_INT: Low precision
GLM\_PRECISION\_MEDIUMP\_INT: Medium precision
GLM\_PRECISION\_MEDIUMP\_INT: Medium precision
GLM\_PRECISION\_HIGHP\_INT: High precision (default)
GLM\_PRECISION\_HIGHP\_INT: High precision (default)
Available defines for unsigned integer types (glm::uvec\*):
Available defines for unsigned integer types (glm::uvec\*):
GLM\_PRECISION\_LOWP\_UINT: Low precision
GLM\_PRECISION\_LOWP\_UINT: Low precision
GLM\_PRECISION\_MEDIUMP\_UINT: Medium precision
GLM\_PRECISION\_MEDIUMP\_UINT: Medium precision
GLM\_PRECISION\_HIGHP\_UINT: High precision (default)
GLM\_PRECISION\_HIGHP\_UINT: High precision (default)
3.2. Compile-time message system {#compile-time-message-system .HeadingB}
### 3.2. Compile-time message system {#compile-time-message-system .HeadingB}
--------------------------------
GLM includes a notification system which can display some information at
build time:
- Platform: Windows, Linux, Native Client, QNX, etc.
- Compiler: Visual C++, Clang, GCC, ICC, etc.
- Build model: 32bits or 64 bits
- C++ version: C++98, C++11, MS extensions, etc.
- Architecture: x86, SSE, AVX, etc.
- Included extensions
GLM includes a notification system which can display some information at build time:
- etc.
* Platform: Windows, Linux, Native Client, QNX, etc.
* Compiler: Visual C++, Clang, GCC, ICC, etc.
* Build model: 32bits or 64 bits
* C++ version: C++98, C++11, MS extensions, etc.
* Architecture: x86, SSE, AVX, etc.
* Included extensions
* etc.
This system is disabled by default. To enable this system, define
This system is disabled by default. To enable this system, define GLM\_FORCE\_MESSAGES before any inclusion of <glm/glm.hpp>. The messages are generated only by compiler supporting \#program message and
GLM\_FORCE\_MESSAGES before any inclusion of <glm/glm.hpp>. The
messages are generated only by compiler supporting \#program message and
only once per project build.
only once per project build.
-------------------------------
```cpp
\#define GLM\_FORCE\_MESSAGES
#define GLM_FORCE_MESSAGES
#include<glm/glm.hpp>
\#include <glm/glm.hpp>
```
-------------------------------
-------------------------------
3.3. C++ language detection {#c-language-detection .HeadingB}
### 3.3. C++ language detection
---------------------------
GLM will automatically take advantage of compilers’ language extensions
GLM will automatically take advantage of compilers’ language extensions
when enabled. To increase cross platform compatibility and to avoid
when enabled. To increase cross platform compatibility and to avoid
@ -428,128 +392,96 @@ compiler extensions, a programmer can define GLM\_FORCE\_CXX98 before
any inclusion of <glm/glm.hpp> to restrict the language feature
any inclusion of <glm/glm.hpp> to restrict the language feature
set C++98:
set C++98:
-------------------------------
```cpp
\#define GLM\_FORCE\_CXX98
#define GLM_FORCE_CXX98
#include<glm/glm.hpp>
\#include <glm/glm.hpp>
```
-------------------------------
-------------------------------
For C++11 and C++14, equivalent defines are available:
For C++11 and C++14, equivalent defines are available:
GLM\_FORCE\_CXX11, GLM\_FORCE\_CXX14.
GLM\_FORCE\_CXX11, GLM\_FORCE\_CXX14.
-------------------------------
```cpp
\#define GLM\_FORCE\_CXX11
#define GLM_FORCE_CXX11
#include<glm/glm.hpp>
\#include <glm/glm.hpp>
// If the compiler doesn’t support C++11, compiler errors will happen.
-------------------------------
```
-------------------------------
GLM\_FORCE\_CXX14 overrides GLM\_FORCE\_CXX11 and GLM\_FORCE\_CXX11
GLM\_FORCE\_CXX14 overrides GLM\_FORCE\_CXX11 and GLM\_FORCE\_CXX11
overrides GLM\_FORCE\_CXX98 defines.
overrides GLM\_FORCE\_CXX98 defines.
3.4. SIMD support {#simd-support .HeadingB}
### 3.4. SIMD support
-----------------
GLM provides some SIMD optimizations based on [compiler
GLM provides some SIMD optimizations based on [compiler intrinsics](https://msdn.microsoft.com/en-us/library/26td21ds.aspx).
Additionally, GLM provides a low level SIMD API in glm/simd directory
Additionally, GLM provides a low level SIMD API in glm/simd directory
for users who are really interested in writing fast algorithms.
for users who are really interested in writing fast algorithms.
3.5. Force inline {#force-inline .HeadingB}
### 3.5. Force inline
-----------------
To push further the software performance, a programmer can define
To push further the software performance, a programmer can define
GLM\_FORCE\_INLINE before any inclusion of <glm/glm.hpp> to force
GLM\_FORCE\_INLINE before any inclusion of <glm/glm.hpp> to force
the compiler to inline GLM code.
the compiler to inline GLM code.
-------------------------------
```cpp
\#define GLM\_FORCE\_INLINE
#define GLM_FORCE_INLINE
#include<glm/glm.hpp>
\#include <glm/glm.hpp>
```
-------------------------------
-------------------------------
3.6. Vector and matrix static size {#vector-and-matrix-static-size .HeadingB}
----------------------------------
GLSL supports the member function .length() for all vector and matrix
types.
-------------------------------
\#include <glm/glm.hpp>
void foo(vec4 const & v)
{
> int Length = v.length();
>
> …
}
-------------------------------
-------------------------------
This function returns a int however this function typically interacts
### 3.6. Vector and matrix static size
with STL size\_t based code. GLM provides GLM\_FORCE\_SIZE\_T\_LENGTH
pre-processor option so that member functions length() return a size\_t.
Additionally, GLM defines the type glm::length\_t to identify length()
GLSL supports the member function .length() for all vector and matrix types.
returned type, independently from GLM\_FORCE\_SIZE\_T\_LENGTH.
--------------------------------------
```cpp
\#define GLM\_FORCE\_SIZE\_T\_LENGTH
#include<glm/glm.hpp>
\#include <glm/glm.hpp>
void foo(vec4 const & v)
{
int Length = v.length();
...
}
```
void foo(vec4 const & v)
This function returns a int however this function typically interacts with STL size\_t based code. GLM provides GLM\_FORCE\_SIZE\_T\_LENGTH pre-processor option so that member functions length() return a size\_t.
{
Additionally, GLM defines the type glm::length\_t to identify length() returned type, independently from GLM\_FORCE\_SIZE\_T\_LENGTH.
GLSL supports implicit conversions of vector and matrix types. For
GLSL supports implicit conversions of vector and matrix types. For example, an ivec4 can be implicitly converted into vec4.
example, an ivec4 can be implicitly converted into vec4.
Often, this behaviour is not desirable but following the spirit of the
Often, this behaviour is not desirable but following the spirit of the library, this behavior is supported in GLM. However, GLM 0.9.6 introduced the define GLM\_FORCE\_EXPLICIT\_CTOR to require explicit
library, this behavior is supported in GLM. However, GLM 0.9.6
introduced the define GLM\_FORCE\_EXPLICIT\_CTOR to require explicit
#include "half.hpp" // Define “half” class with equivalent behavior than “float”
int foo()
typedef glm::tvec4<half> my_hvec4;
```
{
However, defining GLM\_FORCE\_UNRESTRICTED\_GENTYPE is not compatible with GLM\_FORCE\_SWIZZLE and will generate a compilation error if both are defined at the same time.
glm::vec4 Position = glm::vec4(glm:: vec3(0.0f), 1.0f);
## 4. Stable extensions
glm::mat4 Model = glm::translate(
GLM extends the core GLSL feature set with extensions. These extensions include: quaternion, transformation, spline, matrix inverse, color spaces, etc.
> glm::mat4(1.0f), glm::vec3(1.0f));
To include an extension, we only need to include the dedicated header file. Once included, the features are added to the GLM namespace.
glm::vec4 Transformed = Model \* Position;
```cpp
#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
…
int foo()
{
glm::vec4 Position = glm::vec4(glm:: vec3(0.0f), 1.0f);
glm::mat4 Model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));