cglm Mathematics Library

repository·master·Indexed 25 days ago

https://github.com/recp/cglm

A highly optimized 2D and 3D mathematics library for C designed for graphics programming. It provides SIMD-optimized, allocation-free functions compatible with OpenGL-style column-major layouts. cglm supports multiple usage patterns, including a header-only mode, a Struct API for ergonomic returns, and a linked API. It includes specialized tools for affine transformations, 2D AABB manipulation, and configuration options for clipspace and handedness to support APIs like Vulkan and Metal.

Tokens
31.6K
Snippets
41
Records
270
Agent score
83%

What's inside cglm

  1. Overview of cglm

    master

    cglm is an optimized 3D mathematics library written in C99 (and compatible with C89). It is designed as a C-compatible alternative to the original OpenGL Mathematics (glm) library.

    Key technical detail: cglm stores matrices in column-major order. While row-major support is considered for future optional implementation, current usage assumes column-major layout.

  2. Overview of cglm features

    master

    cglm is a C mathematics library providing OpenGL-style math operations. Key features include:

    • Optimizations: Supports both scalar and SIMD (SSE, AVX, NEON, WASM, etc.) optimizations.
    • API Styles: Offers both Array API and Struct API.
    • Matrix Operations: General purpose matrix operations (mat4, mat3), chained matrix multiplication (square only), and matrix decomposition (extracting rotation and scaling factors).
    • Vector Operations: General purpose vector operations (cross, dot, rotate, proj, angle, etc.).
    • Transformations: Affine transformations, optimized affine transform matrices (mul, rigid-body inverse), and camera (lookat) functions.
    • Projections & Clipspaces: Projections (ortho, perspective) with options for different clipspaces (e.g., Left Handed, Zero-to-One; default is right-handed negative-one).
    • Rotation & Quaternions: Quaternions, Euler angles (yaw-pitch-roll) to matrix conversion, and Euler angle extraction.
    • Geometry & Culling: Frustum operations (extracting planes and corners), Bounding Boxes (AABB in Frustum/culling, crop, merge), 2D bounding boxes, and Bounding Spheres.
    • Projection/Unprojection: Project and unproject functions.
    • Curves & Interpolation: Easing functions, curves, and curve interpolation helpers (SMC, deCasteljau...).
    • Ray Casting: Ray intersection helpers.
    • Metal Integration: Helpers to convert cglm types to Apple's simd library for passing types to Metal without redundant packing.
  3. Use vec4 operations in cglm

    master
    The vec4 functionality is provided by the cglm/vec4.h header. It includes a variety of macros for initialization and duplication, as well as functions for vector arithmetic, geometric operations, and transformations on 4-component vectors.
  4. Understand the 2D AABB representation in cglm

    master

    In cglm, a 2D Axis-Aligned Bounding Box (AABB) is represented as a two-dimensional array of vec2s.

    • aabb[0] represents the min point.
    • aabb[1] represents the max point.

    If you are using a different data structure (like a custom struct), you must convert it to this vec2[2] format before calling AABB functions and convert it back afterward.

  5. Understand cglm types and memory layout

    master

    cglm uses the glm prefix for all functions. Types are implemented as arrays rather than structs to ensure they are homogeneous and can be passed directly to OpenGL without casting or using a value_ptr function.

    Because types are arrays, you must use subscript notation (e.g., vec[0], vec[1]) to access elements instead of struct members like vec.x or vec.y. Matrices are arrays of vectors, so elements are accessed via matrix[row][column].

  6. Understand the AABB (Axis-Aligned Bounding Box) representation

    master

    In cglm, an Axis-Aligned Bounding Box (AABB) is represented as a two-dimensional array of vec3.

    • box[0] represents the min point.
    • box[1] represents the max point.

    If you are using a different data structure (like a custom struct), you must convert it to this vec3[2] format before calling cglm box functions and convert it back afterward.

  7. Use mat4x2 in cglm

    master

    The mat4x2 type represents a 4x2 matrix. It is stored in memory such that the elements are organized by rows:

    column 1column 2column 3column 4
    row 1m00m10m20m30
    row 2m01m11m21m31

    Include the header cglm/mat4x2.h to use these functions.

  8. Build cglm using Unix (Autotools)

    master

    Use Autotools for Unix-based systems. This method installs pkg-config files, allowing you to use pkg-config --cflags cglm and pkg-config --libs cglm.

    Build Steps:

    1. Run sh autogen.sh.
    2. Run ./configure.
    3. Run make.
    4. (Optional) Run make check to run tests.
    5. (Optional) Run sudo make install.

    Note on pkg-config path: If your files are installed in a non-standard prefix, you can use ./configure --with-pkgconfigdir=/your/path or set the PKG_CONFIG_PATH environment variable.

    $ sh autogen.sh
    $ ./configure
    $ make
    $ make check # [Optional]
    $ [sudo] make install # [Optional]
  9. Integrate cglm into a CMake project

    master

    You can integrate cglm into your CMake project in two ways:

    1. As a header-only library

    This requires no building or installation of cglm. Use the cglm_headers target.

    2. As a compiled library

    This links against the compiled cglm library using the cglm target.

    In both cases, you must use add_subdirectory pointing to the cglm source directory.

    # Header-only integration
    cmake_minimum_required(VERSION 3.8.2)
    project(<Your Project Name>)
    
    add_executable(${PROJECT_NAME} src/main.c)
    target_link_libraries(${PROJECT_NAME} PRIVATE cglm_headers)
    
    add_subdirectory(external/cglm/ EXCLUDE_FROM_ALL)
    
    # --- OR ---
    
    # Compiled library integration
    cmake_minimum_required(VERSION 3.8.2)
    project(<Your Project Name>)
    
    add_executable(${PROJECT_NAME} src/main.c)
    target_link_libraries(${PROJECT_NAME} PRIVATE cglm)
    
    add_subdirectory(external/cglm/)
  10. Use the Array API

    master
    The Array API is the foundation for most other APIs in cglm. It uses raw arrays as types and functions take these arrays as arguments. To use this API, include the cglm/cglm.h header. Functions in this API use the glm_ prefix.