You are on page 1of 36

Manual

Version 0.9.8
10 September 2016

Christophe Riccio
glm@g-truc.net

The Happy Bunny License (Modified MIT License)

Copyright (c) 2005 - 2016 G-Truc Creation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

Restrictions: By making use of the Software for military purposes, you choose to make a Bunny unhappy.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

The MIT License

Copyright (c) 2005 - 2016 G-Truc Creation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

GLM_GTC_BITFIELD 15 4. GLM REPLACEMENTS FOR DEPRECATED OPENGL FUNCTIONS 25 5.2. PREPROCESSOR OPTIONS 11 3.12.8.9.2.19. GLM_GTC_MATRIX_INVERSE 16 4.5.1. DISABLING DEFAULT CONSTRUCTOR INITIALIZATION 13 3.18. GLM_GTC_NOISE 16 4. GLM REPLACEMENTS FOR GLU FUNCTIONS 26 6.9. FORCE INLINE 12 3.20. FASTER PROGRAM COMPILATION 7 1.11.17. GLM_GTC_TYPE_ALIGNED 22 4.7. GLM_GTC_INTEGER 16 4. SETUP 7 1. VECTOR AND MATRIX STATIC SIZE 13 3. GLM_GTC_VEC1 24 5. USE SAMPLE OF GLM CORE 7 1.1. GLM_GTC_EPSILON 15 4. DEFAULT PRECISION 11 3. GLM_GTC_ROUND 22 4. REQUIRE EXPLICIT CONVERSIONS 14 3.7. GLM_GTC_TYPE_PTR 23 4. GLM_GTC_FUNCTIONS 15 4. GLM_GTC_TYPE_PRECISION 22 4. GLM_GTC_MATRIX_INTEGER 16 4.3.4. GLM_GTC_MATRIX_TRANSFORM 16 4.3.8.4.2. COMPILE-TIME MESSAGE SYSTEM 11 3. GLM_GTC_CONSTANTS 15 4. GLM_GTC_ULP 24 4.13. C++ LANGUAGE DETECTION 12 3.15. KNOWN ISSUES 28 . GLM_GTC_QUATERNION 20 4.1.16. GLM_GTC_PACKING 19 4.14.3.2. SIMD SUPPORT 12 3. DEFAULT C++98 IMPLEMENTATION 9 2. DEPENDENCIES 8 2.Table of Contents INTRODUCTION 6 1.5.10. GETTING STARTED 7 1.6. GLM_GTC_MATRIX_ACCESS 16 4.2. STABLE EXTENSIONS 15 4. SWIZZLE OPERATORS 9 2. REMOVING GENTYPE RESTRICTION 14 4. ANONYMOUS UNION MEMBER IMPLEMENTATION 9 3.4.21.1. OPENGL INTEROPERABILITY 25 5.6.1. GLM_GTC_COLOR_SPACE 15 4. GLM_GTC_RANDOM 20 4. GLM_GTC_RECIPROCAL 22 4.

’? 29 7. CODE SAMPLES 31 8.5. REFERENCES 34 9. FAQ 29 7.7.3. GLM DEVELOPMENT 34 9.3. WHERE CAN I FIND THE DOCUMENTATION OF EXTENSIONS? 29 7.1 WHY GLM FOLLOWS GLSL SPECIFICATION AND CONVENTIONS? 29 7. SHOULD I USE ‘GTX’ EXTENSIONS? 29 7.9. PROJECTS USING GLM 34 9.2.4. LIGHTING 32 9.1. OPENGL TUTORIALS USING GLM 35 9. WHERE CAN I ASK MY QUESTIONS? 29 7. ALTERNATIVES TO GLM 36 9. ACKNOWLEDGEMENTS 36 .2. DOES GLM RUN GLSL PROGRAM? 29 7. WHY SOME GLM FUNCTIONS CAN CRASH BECAUSE OF DIVISION BY ZERO? 30 7. SHOULD I USE ‘USING NAMESPACE GLM.5. PRECISION QUALIFIERS SUPPORT 28 7.1..1.10.8. DOES A GLSL COMPILER BUILD GLM CODES? 29 7. EQUIVALENT FOR OTHER LANGUAGES 36 9.4.6. 30 7. MATRIX TRANSFORM 31 8.3. WHEN I BUILD WITH VISUAL C++ WITH /W4 WARNING LEVEL. WHAT UNIT FOR ANGLES IS USED IN GLM? 30 8.4.6.2. VECTOR TYPES 31 8. COMPUTE A TRIANGLE NORMAL 31 8.11.2..8.7.6. I HAVE WARNINGS. IS GLM FAST? 29 7. EXTERNAL LINKS 34 9. NOT FUNCTION 28 6. OPENGL SPECIFICATIONS 34 9.

.. quaternions. he knows GLM as well which makes it really easy to use. random numbers. GLM is written in C++98 but can take advantage of C++11 when supported by the compiler. data packing. This library works perfectly with OpenGL but it also ensures interoperability with other third party libraries and SDK. including this manual. It is a good candidate for software rendering (raytracing / rasterisation).0 and higher (experimental) .GCC 4.LLVM 3. noise.2 and higher . etc.net.0 and higher .Visual C++ 2010 and higher . provides extended capabilities: matrix transformations. physic simulations and any development context that requires a simple and convenient mathematics library. GLM provides classes and functions designed and implemented with the same naming conventions and functionalities than GLSL so that when a programmer knows GLSL. An extension system. Any feedback is welcome at glm@g-truc. It is a platform independent library with no dependence and it officially supports the following compilers: .0 and higher .. This project isn't limited to GLSL features.Apple Clang 4.Intel C++ Composer XE 2013 and higher .Any conform C++98 or C++11 compiler The source code and the documentation. image processing. based on the GLSL extension conventions.CUDA 4. Thanks for contributing to the project by submitting reports for bugs and feature requests. are licensed under the Happy Bunny License (Modified MIT) and the MIT License.Introduction OpenGL Mathematics (GLM) is a C++ mathematics library for graphics C++ programs based on the OpenGL Shading Language (GLSL) specifications.

hpp>: all the GLSL matrix functions <glm/packing.hpp>: all the GLSL integer functions <glm/matrix.hpp>: all the GLSL exponential functions <glm/geometry. // Header file #include <glm/fwd. dmat3 <glm/mat3x4.hpp> // Include GLM extensions #include <glm/gtc/matrix_transform.hpp> glm::mat4 transform( glm::vec2 const& Orientation. <glm/vec2.hpp>: mat2.hpp>: all the GLSL trigonometric functions <glm/vector_relational. bvec4.hpp>: mat3x2.hpp>: mat4x3. 1.hpp>: mat3. Core GLM features can be included using individual headers to allow faster user program compilations. .5 introduced <glm/fwd. ivec3 and uvec3 <glm/vec4.hpp>: mat3x4. ivec4 and uvec4 <glm/mat2x2. Likewise. 10. glm::vec3 const& Up) { glm::mat4 Proj = glm::perspective(glm::radians(45. bvec2.3. GLM 0. GLM extensions should be included only in program sources using them.hpp> #include <glm/trigonometric.hpp> // Source file #include <glm/glm. To further help compilation time.1f. a programmer only has to include <glm/glm. dmat2x4 <glm/mat3x2. ivec2 and uvec2 <glm/vec3.hpp>: all the GLSL geometry functions <glm/integer. This design may significantly increase the compile time for files that use GLM.2.hpp> 1.hpp>: vec3. bvec3.hpp>: mat2x3. glm::vec3 const& Translate. dmat4x2 <glm/mat4x3. Getting started 1. This include provides all the GLSL features implemented by GLM.hpp> #include <glm/mat4x4. Use sample of GLM core // Include GLM core features #include <glm/vec3.hpp>: vec2.hpp> #include <glm/vec4.hpp>: all the GLSL vector relational functions 1. Hence.f). Hence.hpp>: vec4.9.1.hpp>: mat4x2.33f.hpp> in his program.hpp>: all the GLSL packing functions <glm/trigonometric. To use GLM. dmat4x3 <glm/mat4x4. dmat3x2 <glm/mat3x3.hpp>: mat4. dmat4 <glm/common. dvec2. dvec3. it is important to limit GLM inclusion to header and source files that actually use it.1. dvec4.f).hpp>: mat2x4. dmat2 <glm/mat2x3.hpp>: all the GLSL common functions <glm/exponential. dmat2 <glm/mat4x2. 0.hpp> that provides forward declarations of GLM types. there is nothing to build to use it. Faster program compilation GLM is a header only library that makes a heavy usage of C++ templates. Setup GLM is a header only library. dmat2x3 <glm/mat2x4.

.hpp> is included.h or windows.h.h. return Proj * View * Model. glm::mat4 ViewRotateX = glm::rotate(ViewTranslate.0f). } 1. However. GLM will rely on its own implementation of static assert. Translate). glm::mat4 View = glm::rotate(ViewRotateX. If neither are detected. Up). Dependencies When <glm/glm.h.hpp> is included. GLM provides all the GLSL features it implements in C++. glm::mat4 Model = glm::mat4(1. glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.4.x. glu.h. Orientation.f). glcorearb. if <boost/static_assert. Up). There is no dependence with external libraries or external headers such as gl. Orientation. Boost static assert will be used all over GLM code to provide compiled time errors unless GLM is built with a C++ 11 compiler in which case static_assert.y. gl3.

0f). Swizzle operators are disabled by default.hpp> void foo() { glm::vec3 A(1. This implementation is only enabled when the language extension is enabled and GLM_SWIZZLE is defined. vec4 A.. glm::vec3 PositionB = PositionXYZ. 1.5f. #define GLM_SWIZZLE #include <glm/glm.2. 0. Swizzle operators A common feature of shader languages like GLSL is the swizzle operators.5f). glm::vec4 TexcoordSTPQ = TexcoordST. Those allow selecting multiple components of a vector and change their order.0f).5f.hpp>.x”.0f.0f. 0..xx. 2. Default C++98 implementation The C++98 implementation exposes the R-value swizzle operators as member functions of vector types. Vec3 C = A. 0. “variable. … } Swizzle operators return a copy of the component values hence they can’t be used as L-values to change the value of the variables. “variable. B. Enabling swizzle operators will massively increase the size of compiled files and the compilation time. swizzle operators can be both R-values and L-values. vector components can be accessed using “xyzw”. GLM supports a subset of this functionality as described in the following sub-sections. .2.1. A.yx = A.0f). B = A.0f. which is a non-standard language extension. With GLSL. … glm::vec3 PositionA(1.0f.5f.0f. glm::vec3 ColorBGR = ColorRGBA.0f. 0. vec2 B.xyz() * 2. “rgba” or “stpq”.zxyy” form respectively a scalar.hpp> void foo() { glm::vec4 ColorRGBA(1.0f. a three components vector and a four components vector. 0. To enable them GLM_SWIZZLE must be defined before any inclusion of <glm/glm.xzy” and “variable. 1.0f. .wy.0f).bgr(). // A is not modified! … } 2.bgr() = glm::vec3(2.bgr.stst(). 0. For example. // /!\ No compiler error but A is not affected // This code modify the components of an anonymous copy. … glm::vec2 TexcoordST(1. Finally. but it enables a very powerful implementation of swizzle operators on Windows supporting both L-value swizzle operators and a syntax that doesn’t require parentheses in some cases. #define GLM_FORCE_SWIZZLE #include <glm/glm. 1. Anonymous union member implementation Visual C++ supports anonymous structures in union.5f. 0. 1.

// l-value: glm::vec4 ColorBGRA = ColorRGBA.hpp> void foo() { glm::vec4 Color(1. Color.0f. 0.f.0f. // Both l-value and r-value ColorRGBA.f.rgba(). 1.bgra = ColorRGBA.rgba is not a vector type. … // Generates compiler errors.f. … } Anonymous union member swizzle operators don’t return vector types (glm::vec2. 0.rgba. 1. 1. 0. #define GLM_FORCE_SWIZZLE #include <glm/glm.0f.bgra = ColorRGBA.rgba). // r-value: ColorRGBA.0f. // OK // Or by using the () operator glm::vec4 ClampedC = glm::clamp(Color.0f). Unfortunately. 0. glm::vec4 ClampedA = glm::clamp(Color. 0.5f.rgba. those can’t be interpreted by GLM 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 another C++ functions.f).#define GLM_FORCE_SWIZZLE #include <glm/glm. // ERROR // We need to cast the swizzle operator into glm::vec4 // With by using a constructor glm::vec4 ClampedB = glm::clamp(glm::vec4(Color. // OK … } .0f).f). glm::vec3 and glm::vec4) but implementation specific objects that can be automatically interpreted by other swizzle operators and vector constructors.bgra. 0. 0. 1.hpp> void foo() { glm::vec4 ColorRGBA(1.f). 1.5f.

Compile-time message system GLM includes a notification system which can display some information at build time: . precision mediump int.3. MS extensions. etc. C++11. Native Client. Default precision In C++. .hpp> Available defines for floating point types (glm::vec*.1.5. #include <glm/glm. #define GLM_PRECISION_HIGHP_FLOAT.Platform: Windows.etc. SSE. AVX. Clang.3) using GLSL syntax. glm::dmat*): GLM_PRECISION_LOWP_DOUBLE:Low precision GLM_PRECISION_MEDIUMP_DOUBLE: Medium precision GLM_PRECISION_HIGHP_DOUBLE: High precision (default) Available defines for signed integer types (glm::ivec*): GLM_PRECISION_LOWP_INT:Low precision GLM_PRECISION_MEDIUMP_INT: Medium precision GLM_PRECISION_HIGHP_INT: High precision (default) Available defines for unsigned integer types (glm::uvec*): GLM_PRECISION_LOWP_UINT:Low precision GLM_PRECISION_MEDIUMP_UINT: Medium precision GLM_PRECISION_HIGHP_UINT: High precision (default) 3. ICC. precision highp float.hpp: #define GLM_PRECISION_MEDIUMP_INT. etc. etc.Compiler: Visual C++.10 specification section 4. Linux. Preprocessor options 3.Architecture: x86. . it is not possible to implement GLSL default precision (GLSL 4. glm::mat*): GLM_PRECISION_LOWP_FLOAT:Low precision GLM_PRECISION_MEDIUMP_FLOAT: Medium precision GLM_PRECISION_HIGHP_FLOAT: High precision (default) Available defines for floating point types (glm::dvec*. . etc. GCC.Included extensions . GLM provides some defines that need to add before any include of glm.Build model: 32bits or 64 bits . . .C++ version : C++98.2. To use the default precision functionality. QNX.

#define GLM_FORCE_MESSAGES #include <glm/glm. GLM_FORCE_AVX. 3. GLM_FORCE_SSSE3.hpp> to force the compiler to inline GLM code. GLM_FORCE_SSE42.hpp> // GLM code will be compiled using pure C++ code #define GLM_FORCE_AVX2 #include <glm/glm. For example.hpp>. To enable this system. a programmer can define GLM_FORCE_CXX98 before any inclusion of <glm/glm. Force inline To push further the software performance. .4.hpp> 3. GLM_FORCE_AVX2 or GLM_FORCE_AVX512. SIMD support GLM provides some SIMD optimizations based on compiler intrinsics. // compiler errors will happen. GLM_FORCE_CXX14. These optimizations will be automatically thanks to compiler arguments. GLM will detect this argument and generate code using AVX instructions automatically when available.hpp> For C++11 and C++14. GLM_FORCE_SSE3. 3. The messages are generated only by compiler supporting #program message and only once per project build. define GLM_FORCE_MESSAGES before any inclusion of <glm/glm.This system is disabled by default.hpp> // If the compiler doesn’t support AVX2 instrinsics. GLM_FORCE_SSE41. It’s possible to avoid the instruction set detection by forcing the use of a specific instruction set with one of the fallowing define: GLM_FORCE_SSE2.hpp> GLM_FORCE_CXX14 overrides GLM_FORCE_CXX11 and GLM_FORCE_CXX11 overrides GLM_FORCE_CXX98 defines. C++ language detection GLM will automatically take advantage of compilers’ language extensions when enabled. if a program is compiled with Visual Studio using /arch:AVX. a programmer can define GLM_FORCE_INLINE before any inclusion of <glm/glm. #define GLM_FORCE_CXX11 #include <glm/glm.5. equivalent defines are available: GLM_FORCE_CXX11.hpp> to restrict the language feature set C++98: #define GLM_FORCE_CXX98 #include <glm/glm. Additionally. #define GLM_FORCE_PURE #include <glm/glm. To increase cross platform compatibility and to avoid compiler extensions. The use of intrinsic functions by GLM implementation can be avoided using the define GLM_FORCE_PURE before any inclusion of GLM headers.3. GLM provides a low level SIMD API in glm/simd directory for users who are really interested in writing fast algorithms.

0.length() for all vector and matrix types.0f.hpp> void foo() { .hpp> or other GLM include. GLM allows to explicitly not initialize a variable: #include <glm/glm. GLM provides GLM_FORCE_SIZE_T_LENGTH pre-processor option so that member functions length() return a size_t.0f) … } GLM behavior using GLM_FORCE_NO_CTOR_INIT: #define GLM_FORCE_NO_CTOR_INIT #include <glm/glm.7. Vector and matrix static size GLSL supports the member function .hpp> void foo(vec4 const & v) { int Length = v.#define GLM_FORCE_INLINE #include <glm/glm.hpp> void foo(vec4 const & v) { glm::size_t Length = v.hpp> void foo() { glm::vec4 v. … } This function returns a int however this function typically interacts with STL size_t based code. This behavior can be disable at compilation time by define GLM_FORCE_NO_CTOR_INIT before any inclusion of <glm/glm. Additionally. independently from GLM_FORCE_SIZE_T_LENGTH. GLM default behavior: #include <glm/glm. Disabling default constructor initialization By default and following GLSL specifications. 0. … } 3.hpp> void foo() { glm::vec4 v. #include <glm/glm. vector and matrix default constructors initialize the components to zero. // v is (0.length(). // v is fill with garbage … } Alternatively.hpp> 3.0f. GLM defines the type glm::length_t to identify length() returned type.length().6.0f. This is a reliable behavior but initialization has a cost and it’s not always necessary. #define GLM_FORCE_SIZE_T_LENGTH #include <glm/glm. 0.

9.hpp> typedef glm::tvec4<float> my_fvec4.9. // Implicit conversion. OK glm::vec4 c = a. . Often.hpp> void foo() { glm::ivec4 a. matrix and quaternion types: #include <glm/glm. an ivec4 can be implicitly converted into vec4. However. this behavior is supported in GLM. GLM 0. // Explicit conversion. … glm::vec4 b(a). glm::vec4 v(glm::uninitialize).hpp> void foo() { glm::ivec4 a. implicit conversions are not allowed: #define GLM_FORCE_EXPLICIT_CTOR #include <glm/glm. ERROR … } 3. … glm::vec4 b(a).9. // Explicit conversion. // Implicit conversion. OK … } With GLM_FORCE_EXPLICIT_CTOR define.8.hpp" // Define “half” class with equivalent behavior than “float” typedef glm::tvec4<half> my_hvec4.hpp> #include "half. … } 3. this behaviour is not desirable but following the spirit of the library.8 introduced GLM_FORCE_UNRESTRICTED_GENTYPE define to relax this restriction: #define GLM_FORCE_UNRESTRICTED_GENTYPE #include <glm/glm. 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. Require explicit conversions GLSL supports implicit conversions of vector and matrix types. Removing genType restriction By default GLM only supports basic types as genType for vector. For example.6 introduced the define GLM_FORCE_EXPLICIT_CTOR to require explicit conversion for GLM types. However. OK glm::vec4 c = a. GLM 0. #include <glm/glm.

These extensions include: quaternion. glm::vec4 Transformed = Model * Position. 4.hpp> need to be included to use these features.0f).4.2. color spaces.hpp> need to be included to use these features. Stable extensions GLM extends the core GLSL feature set with extensions. … return 0.3. . 4. etc. } When an extension is included.0f). #include <glm/glm.hpp> need to be included to use these features. 4. <glm/gtc/constants.0f)). glm::mat4 Model = glm::translate( glm::mat4(1. GLM_GTC_color_space Conversion between linear RGB to sRGB and sRGB to linear RGB. <glm/gtc/functions. 1. matrix inverse.hpp> need to be included to use these features. transformation. glm::vec3(1. we only need to include the dedicated header file.0f).4. <glm/gtc/epsilon.hpp> #include <glm/gtc/matrix_transform. the features are added to the GLM namespace. 4.1. GLM_GTC_constants Provide a list of built-in constants. To include an extension.hpp> int foo() { glm::vec4 Position = glm::vec4(glm:: vec3(0. all the dependent core functionalities and extensions will be included as well. <glm/gtc/bitfield.hpp> need to be included to use these features. 4. spline. Once included. GLM_GTC_bitfield Fast bitfield operations on scalar and vector variables. GLM_GTC_epsilon Approximate equal and not equal comparisons with selectable epsilon. GLM_GTC_functions Useful functions. <glm/gtc/color_space.5.

hpp> need to be included to use these features. <glm/gtc/matrix_transform.7. GLM_GTC_matrix_inverse Define additional matrix inverting functions.6.hpp> need to be included to use these features. <glm/gtc/matrix_access. etc) are designed to expect.8. 4. 4. The matrices generated by this extension use standard OpenGL fixed-function conventions. Inverse and determinant functions are not supported for these types. GLM_GTC_noise Define 2D.11. GLM_GTC_integer Provide integer variants of GLM core functions. <glm/gtc/noise.hpp> need to be included to use these features. the lookAt function generates a transform from world space into the specific eye space that the projective matrix functions (perspective. ortho.hpp> need to be included to use these features. 3D and 4D procedural noise functions.hpp> need to be included to use these features. GLM_GTC_matrix_integer Provide integer matrix types.hpp> need to be included to use these features. 4. <glm/gtc/matrix_integer.9. GLM_GTC_matrix_transform Define functions that generate common transformation matrices. . 4. The OpenGL compatibility specifications define the particular layout of this eye space. GLM_GTC_matrix_access Define functions to access rows or columns of a matrix easily. 4.10.4. <glm/gtc/integer. For example. <glm/gtc/matrix_inverse.

f. y / 16.3: glm::simplex(glm::vec4(x / 16. .f.f)). Figure 4.11. Figure 4.f.11. Figure 4. 0.11.5f)).f.2: glm::simplex(glm::vec3(x / 16. y / 16.f. y / 16. 0.1: glm::simplex(glm::vec2(x / 16.5f.5f)). 0.

f. y / 16. Figure 4. 0. 0.5f. .f.f)).11. Figure 4. y / 16.5f)).11.11.5: glm::perlin(glm::vec3(x / 16.4: glm::perlin(glm::vec2(x / 16.5f))).6: glm::perlin(glm::vec4(x / 16.f. Figure 4.f. 0. y / 16.f.

glm::vec2(2. The use of packing functions will results in precision lost. GLM_GTC_packing Convert scalar and vector types to packed formats.0f)).11.f. Figure 4.f. . glm::vec4(2.12. <glm/gtc/packing. 4.8: glm::perlin(glm::vec3(x / 16. y / 16. glm::vec3(2.5f)). This extension can also unpack packed data to the original format.hpp> need to be included to use these features. y / 16.0f)).11.9: glm::perlin(glm::vec4(x / 16. However.f. 0.5f). glm::vec2(0.7: glm::perlin(glm::vec2(x / 16. Figure 4. y / 16.f.f. Figure 4.0f)). the extension guarantee that packing a value previously unpacked from the same format will be perform loselessly.f).11.

GLM_GTC_random Generate random number from various distribution methods.1: glm::vec4(glm::linearRand(glm::vec2(-1). Figure 4. 1).hpp> need to be included to use these features. 1).0f). Figure 4. <glm/gtc/random. GLM_GTC_quaternion Define a quaternion type and several quaternion operations. 0.13.14.14. .14.hpp> need to be included to use these features.2: glm::vec4(glm::circularRand(1. glm::vec2(1)).4. <glm/gtc/quaternion. 4. 0.

Figure 4.0f). 1).5: glm::vec4(glm::ballRand(1.14.0f). Figure 4. 1).4: glm::vec4(glm::diskRand(1. .0f).14. 0. 1).14.Figure 4.3: glm::vec4(glm::sphericalRand(1.

4. cosecant.hpp> need to be included to use these functionalities.hpp> need to be included to use these functionalities.6: glm::vec4(glm::gaussRand(glm::vec3(0). 4.15. i8vec4: vector of 4 signed integer of 8 bits. 1).18. <glm/gtc/round. Figure 4. <glm/gtc/reciprocal. 4. GLM_GTC_type_precision Add vector and matrix types with defined precisions. This extension adds defines to set the default precision of each class of types added: Available defines for signed 8-bit integer types (glm::i8vec*): GLM_PRECISION_LOWP_INT8:Low precision GLM_PRECISION_MEDIUMP_INT8: Medium precision GLM_PRECISION_HIGHP_INT8: High precision (default) Available defines for unsigned 8-bit integer types (glm::u8vec*): GLM_PRECISION_LOWP_UINT8:Low precision GLM_PRECISION_MEDIUMP_UINT8: Medium precision GLM_PRECISION_HIGHP_UINT8: High precision (default) Available defines for signed 16-bit integer types (glm::i16vec*): .hpp> need to be included to use these functionalities. Eg.17. etc. <glm/gtc/type_aligned.16. cotangent. GLM_GTC_round Rounding operation on power of two and multiple values.14. glm::vec3(1)). GLM_GTC_reciprocal Provide hyperbolic functions: secant. 4. GLM_GTC_type_aligned Aligned vector types.

glm::f32quat): GLM_PRECISION_LOWP_FLOAT32:Low precision GLM_PRECISION_MEDIUMP_FLOAT32: Medium precision GLM_PRECISION_HIGHP_FLOAT32: High precision (default) Available defines for 64-bit floating-point types (glm::f64vec*. glm::f64mat*. which takes any of the core template types (vec3. 4. etc. mat4. This extension defines an overloaded function. glm::f64quat): GLM_PRECISION_LOWP_FLOAT64:Low precision GLM_PRECISION_MEDIUMP_FLOAT64: Medium precision GLM_PRECISION_HIGHP_FLOAT64: High precision (default) <glm/gtc/type_precision.GLM_PRECISION_LOWP_INT16:Low precision GLM_PRECISION_MEDIUMP_INT16: Medium precision GLM_PRECISION_HIGHP_INT16: High precision (default) Available defines for unsigned 16-bit integer types (glm::u16vec*): GLM_PRECISION_LOWP_UINT16:Low precision GLM_PRECISION_MEDIUMP_UINT16: Medium precision GLM_PRECISION_HIGHP_UINT16: High precision (default) Available defines for signed 32-bit integer types (glm::i32vec*): GLM_PRECISION_LOWP_INT32:Low precision GLM_PRECISION_MEDIUMP_INT32: Medium precision GLM_PRECISION_HIGHP_INT32: High precision (default) Available defines for unsigned 32-bit integer types (glm::u32vec*): GLM_PRECISION_LOWP_UINT32:Low precision GLM_PRECISION_MEDIUMP_UINT32: Medium precision GLM_PRECISION_HIGHP_UINT32: High precision (default) Available defines for signed 64-bit integer types (glm::i64vec*): GLM_PRECISION_LOWP_INT64:Low precision GLM_PRECISION_MEDIUMP_INT64: Medium precision GLM_PRECISION_HIGHP_INT64: High precision (default) Available defines for unsigned 64-bit integer types (glm::u64vec*): GLM_PRECISION_LOWP_UINT64:Low precision GLM_PRECISION_MEDIUMP_UINT64: Medium precision GLM_PRECISION_HIGHP_UINT64: High precision (default) Available defines for 32-bit floating-point types (glm::f32vec*.).hpp> need to be included to use these functionalities. matrix types. glm::value_ptr. . It returns a pointer to the memory layout of the object. Matrix types store their values in column-major order.19. GLM_GTC_type_ptr Handle the interaction between pointers and vector. glm::f32mat*.

hpp> need to be included to use these features.hpp> void foo() { glm::vec4 v(0.This is useful for uploading data to matrices or copying data to buffer objects. GLM_GTC_ulp Allow the measurement of the accuracy of a function against a reference implementation. <glm/gtc/vec1.0f). .0f). 4.hpp> #include <glm/gtc/type_ptr. glVertex3fv(glm::value_ptr(v)) glLoadMatrixfv(glm::value_ptr(m)).hpp> need to be included to use these features.. glm::mat4 m(1. <glm/gtc/type_ptr. This extension works on floating-point data and provides results in ULP.hpp> void foo() { glm::vec4 v(0. However cast operators may produce programs running with unexpected behaviours without build error or any form of notification. } // Another solution inspired by STL: #include <glm/glm. } Note: It would be possible to implement glVertex3fv(glm::vec3(0)) in C++ with the appropriate cast operator that would result as an implicit cast in this example.0f). <glm/gtc/ulp.. glLoadMatrixfv(&m[0][0]). glVertex3fv(&v[0])..20. .0f).21.. // GLM_GTC_type_ptr extension provides a safe solution: #include <glm/glm. 4. GLM_GTC_vec1 Add *vec1 types. glm::mat4 m(1. .hpp> need to be included to use these features.

From GLM core library: <glm/glm. . From GLM core library: <glm/glm. d}: glm::mat4() * glm::mat4().0) or glm::dmat4().1 specification has deprecated some features that have been removed from OpenGL 3. float right. GLM replacements for deprecated OpenGL functions OpenGL 3. d}: glm::mat4 glm::scale( glm::mat4 const & m. glm::dmat4() * glm::transpose(glm::dmat4()).hpp> glLoadIdentity: glm::mat4(1.hpp> glTranslate{f. glm::dmat4 glm::rotate( glm::dmat4 const & m. glm::dvec3 const & factors). d}: glm::mat4 glm::rotate( glm::mat4 const & m. d}: glm::transpose(glm::mat4()). glm::vec3 const & translation). double angle. glm::dmat4(1. glm::vec3 const & axis). OpenGL interoperability 5. glm::vec3 const & factors). From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. glm::dmat4 glm::translate( glm::dmat4 const & m.1. glm::dvec3 const & axis). From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. glm::dmat4 glm::scale( glm::dmat4 const & m. From GLM core library: <glm/glm.hpp> glFrustum: glm::mat4 glm::frustum( float left. glm::dmat4() * glm::dmat4().hpp> glMultTransposeMatrix{f. d}: glm::mat4() * glm::transpose(glm::mat4()).hpp> glScale{f. From GLM core library: <glm/glm. float top. glm::transpose(glm::dmat4()). float bottom. d}: glm::mat4 glm::translate( glm::mat4 const & m.2 core profile specification.5. glRotate{f. From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. glm::dvec3 const & translation).hpp> glLoadTransposeMatrix{f. float angle.hpp> glMultMatrix{f. GLM provides some replacement functions.0) or glm::mat4().

hpp> gluOrtho2D: glm::mat4 glm::ortho( float left. glm::dmat4 glm::ortho( double left.hpp> glOrtho: glm::mat4 glm::ortho( float left. glm::dvec3 const & up). double right. float right. From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. float aspect. glm::dvec2 const & delta. glm::ivec4 const & viewport). float zNear. float zFar). double top. glm::dmat4 glm::ortho( double left. double aspect. From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. glm::dvec3 const & center. From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp> gluPickMatrix: glm::mat4 pickMatrix( glm::vec2 const & center. float bottom.hpp> gluPerspective: glm::mat4 perspective( float fovy. double bottom. glm::vec2 const & delta. float right. From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. One difference between GLM and GLU is that fovy is expressed in radians in GLM instead of degrees. GLM replacements for GLU functions gluLookAt: glm::mat4 glm::lookAt( glm::vec3 const & eye. double zFar). glm::dmat4 glm::frustum( double left. glm::dmat4 perspective( double fovy. double bottom. double zNear. double top). From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. glm::ivec4 const & viewport). float top). double zNear. double zFar). float zFar).2. float zFar). float zNear. float top.hpp> gluProject: . double zNear. glm::vec3 const & up). glm::vec3 const & center. glm::dmat4 pickMatrix( glm::dvec2 const & center. double right. glm::dmat4 glm::lookAt( glm::dvec3 const & eye. double top.hpp> 5. double zFar). float zNear. float bottom. double bottom. double right.

d}vec4 const & viewport). ' '}vec4 const & viewport). glm::dmat4 const & proj. From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. ' '}vec4 const & viewport). From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform. glm::dmat4 const & model.hpp> .hpp> gluUnProject: glm::vec3 unProject( glm::vec3 const & win. glm::{i. d}vec4 const & viewport). glm::mat4 const & proj. glm::mat4 const & proj. glm::dvec3 unProject( glm::dvec3 const & win. glm::dvec3 project( glm::dvec3 const & obj. glm::{i. glm::{i.glm::vec3 project( glm::vec3 const & obj. glm::{i. glm::mat4 const & model. glm::dmat4 const & model. ' '. ' '. glm::mat4 const & model. glm::dmat4 const & proj.

6.1.2. Precision qualifiers support GLM supports GLSL precision qualifiers through prefixes instead of qualifiers. To prevent name collisions. GLM precision qualifiers are used to handle trade-off between performances and precisions of operations in term of ULPs. } . // Using precision qualifier in GLSL: ivec3 foo(in vec4 v) { highp vec4 a = v. all the types use high precision. the GLSL not function has been implemented with the name not_.6. } // Using precision qualifier in GLM: #include <glm/glm.hpp> ivec3 foo(const vec4 & v) { highp_vec4 a = v. additionally to vec4. By default. lowp ivec3 c = ivec3(b). medium_vec4 b = a. mediump_vec4 and highp_vec4 types. For example. lowp_ivec3 c = glm::ivec3(b). not function The GLSL keyword not is also a keyword in C++. ensure cross compiler support and a high API consistency. 6. mediump vec4 b = a. GLM exposes lowp_vec4. return c. Known issues This section reports the divergences of GLM with GLSL. Similarly to GLSL. return c.

7.6. Following GLSL conventions is a way to find consensus.4. In practice.org. 7. on performance critical code paths. especially in a header file. name collisions will happen as GLM is based on GLSL which uses common tokens for types and functions. mediump and highp qualifiers. GLM provides approximations which trade precision for performance.7. Moreover. 7. Where can I ask my questions? A good place is the OpenGL Toolkits forum on OpenGL. he knows GLM. The idea is that brilliant developers (the OpenGL ARB) worked together and agreed to make GLSL. GLM operates perfectly on the 80% of the code that consumes 20% of the performances. 7. Does GLM run GLSL program? No. It has been designed following the idea that everyone does its own math library with his own conventions. GTC extensions are stabled. 7. FAQ 7. GLM can automatically produce SIMD optimized code for functions of its implementation. This is fairly the way OpenGL features are developed.7. However. GLM is a C++ implementation of a subset of GLSL. Finally. will a higher compatibility with third party library and SDKs. In GLM this means that these extensions might change from version to version without any restriction. Where can I find the documentation of extensions? The Doxygen generated documentation includes a complete list of all extensions available. 7. it doesn’t really change except time to time. .5. Does a GLSL compiler build GLM codes? No.2. Is GLM fast? GLM is mainly designed to be convenient and that's why it is written against the GLSL specification. through extensions. we should expect that dedicated algorithms should be written to reach peak performance. tested and perfectly reliable in time. Avoiding using namespace glm. basically when a developer knows GLSL.’? NO! Chances are that if using namespace glm. Should I use ‘using namespace glm.3. Explore this API documentation to get a complete view of all GLM capabilities! 7.1 Why GLM follows GLSL specification and conventions? Following GLSL conventions is a really strict policy of GLM. thanks to the lowp. Furthermore. Many GTX extensions extend GTC extensions and provide a way to explore features and implementations and APIs and then are promoted to GTC extensions. Following the Pareto principle where 20% of the code consumes 80% of the execution time.8. Should I use ‘GTX’ extensions? GTX extensions are qualified to be experimental extensions. is called. this is not what GLM attends to do.

then GLM will take advantage of them and the compiler will generate warnings.9. 7.. If these extensions are enabled. Starting with GLM 0. This has caused GLM to use inconsistent units for angles. When I build with Visual C++ with /W4 warning level. Why some GLM functions can crash because of division by zero? GLM functions crashing is the result of a domain error that follows the precedent given by C and C++ libraries.11..7. For more information. I have warnings. You should not have any warnings even in /W4 mode. However. 7. follow the link.10. if you expect such level for your code. . all GLM functions are using radians. For example.9. What unit for angles is used in GLM? GLSL is using radians but GLU is using degrees to express angles. then you should ask for the same level to the compiler by at least disabling the Visual C++ language extensions (/Za) which generates warnings when used. it’s a domain error to pass a null vector to glm::normalize function.6.

0f)). Rotate.1.a. glm::vec3 const & c ) { return glm::fastNormalize(glm::cross(c .0f. } // A much faster but less accurate alternative: #include <glm/glm. b .1f. glUniformMatrix4fv(Location. glm::mat4 View = glm::rotate( ViewRotateX.0f.y. Compute a triangle normal #include <glm/glm. glm::vec3 const & b. 1. glm::vec3 const & c ) { return glm::normalize(glm::cross(c .0f. glm::vec3(0. Matrix transform // vec3. Vector types .0f.a. glm::vec3 const & Rotate ) { glm::mat4 Projection = glm::perspective(45. 100. GL_FALSE. 0. scale.0f. rotate.hpp> // vec3 normalize cross glm::vec3 computeNormal ( glm::vec3 const & a. 4. ivec4.hpp> // vec3 cross #include <glm/gtx/fast_square_root. glm::vec3(-1. glm::vec3(0.5f)). } 8. vec4. b . } 8.2.a)). 1.hpp> void setUniformMVP ( GLuint Location. glm::mat4 Model = glm::scale( glm::mat4(1.0f). glm::vec3 const & Translate. glm::vec3 const & b. 8. glm::mat4 ViewTranslate = glm::translate( glm::mat4(1. 0. glm::mat4 MVP = Projection * View * Model. glm::mat4 ViewRotateX = glm::rotate( ViewTranslate.hpp> // fastNormalize glm::vec3 computeNormal ( glm::vec3 const & a.a)). Code samples This series of samples only shows various GLM features without consideration of any sort.f). glm::value_ptr(MVP)). mat4 #include <glm/glm.0f. 0.hpp> // translate.0f).0f)). 0.x.3.0f / 3. Rotate. perspective #include <glm/gtc/matrix_transform.8. Translate).hpp> // value_ptr #include <glm/gtc/type_ptr.

glm::hvec2(-1. glm::hvec2 const PositionDataF16[VertexCount] = { glm::hvec2(-1. if(!shadow( Intersection.0f.hpp> // vecRand3 // vecRand3. glm::i8vec2 const PositionDataI8[VertexCount] = { glm::i8vec2(-1. 8.normal().Intersection. -1. if(Diffuse <= 0. if(Material.0f).isSpecular()) . glm::i32vec2( 1.0f).diffuse() * Diffuse.0f).-1).globalPosition() + glm::vecRand3(0. 1.inaccuracy()). // 8 bits signed integer quad geometry std::size_t const PositionSizeI8 = VertexCount * sizeof(glm::i8vec2). light const & Light.hpp> //vec2 #include <glm/gtc/type_precision.#include <glm/glm. glm::i8vec2( 1. LightVector).0f).0f.0f) }. generate a random and equiprobable normalized vec3 glm::vec3 lighting ( intersection const & Intersection.-1).isDiffuse()) Color += Light.globalPosition().0f. -1.4. glm::hvec2( 1.0f.0f. // 32 bits signed integer quad geometry std::size_t const PositionSizeI32 = VertexCount * sizeof(glm::i32vec2). 1).hpp> //hvec2.hpp> // vec3 normalize reflect dot pow #include <glm/gtx/random. 1).0f.position() .color() * Material. glm::hvec2( 1. 1. 1. i8vec2.0f).0f.0f. 1) }. // Half-float quad geometry std::size_t const PositionSizeF16 = VertexCount * sizeof(glm::hvec2). glm::i32vec2 const PositionDataI32[VertexCount] = { glm::i32vec2(-1.0f.0f) }.0f) return Color.-1).0f). glm::vec3 const & View ) { glm::vec3 Color = glm::vec3(0. glm::i8vec2(-1. 1) }. glm::vec3 LightVertor = glm::normalize( Light. LightVertor)) { float Diffuse = glm::dot(Intersection.-1). i32vec2 std::size_t const VertexCount = 4.position(). glm::vec2( 1. glm::i8vec2( 1.-1. glm::vec2 const PositionDataF32[VertexCount] = { glm::vec2(-1. material const & Material. glm::vec2(-1. glm::i32vec2(-1. Lighting #include <glm/glm.0f). if(Material. 1. Light. glm::vec2( 1. // Float quad geometry std::size_t const PositionSizeF32 = VertexCount * sizeof(glm::vec2). glm::i32vec2( 1. Light.-1.

View). float Specular = glm::pow(Base.0f. Material.0f ? Dot : 0.exponent()). Intersection. float Dot = glm::dot(Reflect. } } return Color. } . float Base = Dot > 0. Color += Material.specular() * Specular.normal()). { glm::vec3 Reflect = glm::reflect( -LightVector.

OpenGL specifications . iOS and Android.3. Mac.3 specification 9. How to use textures in GLSL as part of a wide variety of techniques from basic texture mapping to deferred shading.GLSL 4.OpenGL 4.0.3 core specification . Projects using GLM Leo’s Forture Leo’s Fortune is a platform adventure game where you hunt down the cunning and mysterious thief that stole your gold. External links . Xbox One.GLM on stackoverflow 9. I am left with no choice but to follow the trail.GLM website .0 including tessellation and geometry shaders.GLM HEAD snapshot .9. How to use the OpenGL Shading Language to implement lighting and shading techniques.G-Truc Creation’s page 9.2. the thief has dropped pieces of my gold like breadcrumbs through the woods. real-time 3D graphics using GLSL 4. .The OpenGL Toolkits forum to ask questions about GLM . Available on PS4. Beautifully hand-crafted levels bring the story of Leo to life in this epic adventure.GLU 1.30 specification . PC.1. “I just returned home to find all my gold has been stolen! For some devious purpose.” -Leopold OpenGL 4.0 Shading Language Cookbook A full set of recipes demonstrating simple and advanced techniques for producing high-quality. References 9. Use the new features of GLSL 4.GLM bug report and feature request .4. GLM development .” “Despite this pickle of a trap. I must recover my fortune.” “Whatever lies ahead.

1 and later .5.Outerra 3D planetary engine for seamless planet rendering from space down to the surface. with official support for OS X.Rastergrid. Cinder is cross- platform. Cinder Cinder is a free and open source library for professional-quality creative coding in C++. video. audio. and computational geometry.Swiftless’ OpenGL tutorial using GLM by Donald Urquhart . samples that show how to set up all the different new features . Cinder is a C++ library for programming with aesthetic intent .OpenGL Tutorial.Learning Modern 3D Graphics rogramming. Can use arbitrary resolution of elevation data. many technical articles with companion programs using GLM by Daniel Rákos . tutorials for OpenGL 3. opencloth A collection of source codes implementing cloth simulation algorithms in OpenGL.OpenGL Programming on Wikibooks: For beginners who are . Falcor Real-time rendering research framework by NVIDIA. Simple. OpenGL tutorials using GLM . easy-to-follow examples with GLSL source code. as well as a basic description of the theory behind each technique. a great OpenGL tutorial using GLM by Jason L. Windows. This includes domains like graphics. and WinRT. but still suitable for learning and experimentation. Are you using GLM in a project? 9.the sort of development often called creative coding. Cinder is production-proven. powerful enough to be the primary tool for professionals. Cinder is released under the 2-Clause BSD License. iOS.The OpenGL Samples Pack. McKesson . refining it to centimetre resolution using fractal algorithms.Morten Nobel-Jørgensen’s review and use an OpenGL renderer .

6.Ashima Arts and Stefan Gustavson for their work on webgl-noise which has been used for GLM noises implementation.GlmSharp: Open-source semi-generated GLM-flavored math library for . .Ghenadii Ursachi for GLM_GTX_matrix_interpolation implementation. GLM tutorial .Are you using GLM in a tutorial? 9.CML: The CML (Configurable Math Library) is a free C++ math library for games and graphics. OpenAL.glm-rs: GLSL mathematics for Rust programming language 9.JGLM .Learn OpenGL.All the GLM users that have report bugs and hence help GLM to become a great library! .8. . .glm-go: Simple linear algebra library similar in spirit to GLM .Arthur Winters for the C++11 and Visual C++ swizzle operators implementation and tests.Eigen: A more heavy weight math library for general linear algebra in C++.Java OpenGL Mathematics (GLM) . OpenGL tutorial . Acknowledgements GLM is developed and maintained by Christophe Riccio but many contributors have made this project what it is. .Guillaume Chevallereau for providing and maintaining the nightlight build system. graphics and game programming. .c-jump.Joshua Smith and Christoph Schied for the discussions and the experiments around the swizzle operator implementation issues. . OpenGL tutorial .glhlib: A much more than glu C library. Alternatives to GLM . GLFW.Mathieu Roumillac for providing some implementation ideas.7. .discovering OpenGL.Game Tutorials.Grant James for the implementation of all combination of none-squared matrix products. Equivalent for other languages .Are you using or working on an alternative library to GLM? 9. .SwiftGL Math Library . .openll: Lua bindings for OpenGL. .Java OpenGL Mathematics Library . .open.gl.NET/C#. GLM. Special thanks to: . SOIL and PhysicsFS . .3D Game Engine Programming: Learning the latest 3D Game Engine Programming techniques.glm-js: JavaScript adaptation of the OpenGL Mathematics (GLM) C++ library interfaces . . .