GLM (OpenGL Mathematics)

repository·master·Indexed 43 days ago

https://github.com/icaven/glm

A header-only C++ mathematics library that implements the OpenGL Shading Language (GLSL) specification. It provides GLSL-compatible types and functions for graphics programming, including vectors, matrices, and quaternions, and is suitable for software rendering, image processing, and physics simulations. Supports C++98 and C++11, with specific configuration macros for SIMD optimizations, data layout, and precision.

Tokens
13.1K
Snippets
33
Records
80
Agent score
56%

What's inside GLM

  1. Overview of OpenGL Mathematics (GLM)

    master

    GLM (OpenGL Mathematics) is a header-only C++ mathematics library designed for graphics software. It is based on the OpenGL Shading Language (GLSL) specifications, meaning it uses the same naming conventions and functionality as GLSL. This allows developers familiar with GLSL to use the same mathematical logic in C++.

    Key features include:

    • GLSL Compatibility: Core types and functions mirror GLSL.
    • Extension System: Provides additional capabilities like matrix transformations, quaternions, data packing, random numbers, and noise, following GLSL extension conventions.
    • Versatility: While optimized for OpenGL, it is suitable for software rendering (raytracing/rasterisation), image processing, and physics simulations.
    • Zero Dependencies: A platform-independent, header-only library.
  2. Use GLM precision qualifiers

    master

    GLM supports GLSL precision qualifiers (lowp, mediump, highp) through type prefixes instead of qualifiers. This allows trading precision for performance. By default, all types use high precision.

    To use them, use the following naming convention:

    • lowp_vec4 / lowp_ivec3 etc.
    • mediump_vec4 / mediump_ivec3 etc.
    • highp_vec4 / highp_ivec3 etc.
    #include <glm/glm.hpp>
    
    ivec3 foo(const vec4 & v)
    {
        highp_vec4 a = v;
        medium_vec4 b = a;
        lowp_ivec3 c = glm::ivec3(b);
        return c;
    }
  3. How GLM extensions work

    master

    GLM extends its core GLSL feature set through dedicated extension headers. To use an extension, you must include its specific header file. Once included, the extension's features are added directly to the glm:: namespace. Including an extension also automatically includes all its dependent core functionalities and other required extensions.

    #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));
    
        glm::vec4 Transformed = Model * Position;
        return 0;
    }
  4. Optimize GLM performance with GLM_FORCE_INTRINSICS

    master

    By default, GLM prioritizes constexpr support, which can disable certain SIMD optimizations. To enable the SIMD instruction code path for better performance, define the GLM_FORCE_INTRINSICS macro.

    Warning: Enabling this may disable constexpr support for certain operations.

  5. Avoid using 'namespace glm'

    master
    Do not use using namespace glm;. Because GLM is based on GLSL, it uses many common tokens for types and functions. Using the namespace globally increases the risk of name collisions with other third-party libraries or SDKs.
  6. Use uninitialized GLM types for performance

    master
    In GLM 0.9.6.0, you can use the uninitialize feature to explicitly skip the initialization of a GLM type. This is useful for performance-critical code where you intend to overwrite the values immediately and want to avoid the overhead of default initialization.
  7. Enable experimental features with GLM_ENABLE_EXPERIMENTAL

    master

    Some GLM features are considered experimental and are not enabled by default. To use these features, you must define the GLM_ENABLE_EXPERIMENTAL macro in your project.

    Note: Using this macro may expose APIs that are subject to change in future releases.

  8. Understand GLM core characteristics

    master
    GLM is a header-only library designed to follow the GLSL specification. Because it is header-only, you do not need to compile a separate library binary; you simply include the relevant headers in your C++ project. It provides mathematical types (vectors, matrices, quaternions) and functions that closely match those found in GLSL.
  9. Use GLM_FORCE_ALIGNED_GENTYPES for SIMD without constexpr

    master
    If you need aligned types for SIMD instructions but cannot use GLM_FORCE_INTRINSICS (e.g., because you require constexpr support elsewhere), you can define GLM_FORCE_ALIGNED_GENTYPES. This enables aligned types but does not enable the SIMD instruction code path and will disable constexpr support.
  10. Enable swizzle operators with GLM_FORCE_SWIZZLE

    master

    To enable GLSL-like swizzling (e.g., v.xyz(), v.rgba), define GLM_FORCE_SWIZZLE.

    Warning: Enabling swizzling significantly increases binary size and compilation time.

    Implementation Modes:

    1. Standard C++98 (R-value only): Swizzles are implemented as member functions (e.g., v.xyz()). These return a copy and cannot be used as L-values (you cannot assign to them).
    2. Language Extensions (L-value support): On compilers supporting anonymous struct unions (Visual C++, GCC, Clang), swizzles can be used as L-values (e.g., v.bgra = ...). Note that these return implementation-specific objects that must be explicitly converted via constructors or operator() when used as function arguments.
    #define GLM_FORCE_SWIZZLE
    #include <glm/glm.hpp>
    
    using namespace glm;
    vec4 Color = vec4(1.0f, 0.5f, 0.0f, 1.0f);
    
    // Explicit conversion required for function arguments
    vec4 ClampedB = clamp(vec4(Color.rgba), 0.f, 1.f); 
  11. Replace deprecated OpenGL functions with GLM

    master

    GLM provides replacements for functions deprecated in the OpenGL 3.1 specification. Most of these are located in the GLM_GTC_matrix_transform extension.

    OpenGL FunctionGLM ReplacementHeader
    glRotateglm::rotate<glm/gtc/matrix_transform.hpp>
    glScaleglm::scale<glm/gtc/matrix_transform.hpp>
    glTranslateglm::translate<glm/gtc/matrix_transform.hpp>
    glLoadIdentityglm::mat4(1.0) or glm::mat4()<glm/glm.hpp>
    glMultMatrixmat * mat operator<glm/glm.hpp>
    glLoadTransposeMatrixglm::transpose(mat)<glm/glm.hpp>
    glMultTransposeMatrixmat * glm::transpose(mat)<glm/glm.hpp>
    glFrustumglm::frustum<glm/gtc/matrix_transform.hpp>
    glOrthoglm::ortho<glm/gtc/matrix_transform.hpp>