đź“˝ Highly Optimized 2D / 3D Graphics Math (glm) for C
C
A highly optimized 2D|3D math library. Also known as OpenGL Mathematics (glm) for C. cglm provides fast and ergonomic math functions to ease graphics programming. It is community friendly – feel free to report any bugs and issues you face.
If you're using C++, you might want to check out GLM
All functions and their parameters are documented above their declaration inside their corresponding headers.
Alternatively, you can read the complete documentation here.
cglm can be used in it’s entirety as a header-only library simply by including cglm/cglm.h
. If you wish to link against it instead, it can be built using one of the supported build systems. Detailed information about building on individual platforms and build systems along with the instructions for building the documentation can be found in BUILDING.md.
Include the cglm/cglm.h
header and use functions with the glm_
prefix.
#include "cglm/cglm.h"
// ...
vec2 vector;
glm_vec2_zero(vector);
Include cglm/struct.h
and use glms_
.
#include "cglm/struct.h"
// ...
vec2s vector = glms_vec2_zero();
Include cglm/call.h
and use glmc_
.
#include "cglm/call.h"
// ...
vec2 vector;
glmc_vec2_zero(vector);
While cglm by default aligns what’s necessary, it is possible to disable this by defining CGLM_ALL_UNALIGNED
. If you’re targeting machines with any kind of SIMD support, make sure that all vec4
, mat4
and mat2
arguments you pass to cglm functions are aligned to prevent unexpected crashes, alternatively use the unaligned versions if present.
The struct API works as follows (note the s
suffix on types, glms_
prefix on functions and GLMS_
on constants):
#include <cglm/struct.h>
mat4s mat = GLMS_MAT4_IDENTITY_INIT;
mat4s inv = glms_mat4_inv(mat);
Struct functions generally take parameters by copy and return the results rather than taking pointers and writing to out parameters. That means your variables can usually be const
, if you’re into that.
The types used are actually unions that allow access to the same data in multiple ways. One of these involves anonymous structures available since C11. MSVC supports them in earlier versions out of the box and GCC/Clang as well if you enable -fms-extensions
.
To explicitly enable anonymous structures #define CGLM_USE_ANONYMOUS_STRUCT 1
, or 0
to disable them.
For backwards compatibility, you can also #define CGLM_NO_ANONYMOUS_STRUCT
to disable them. If you don’t specify explicitly, cglm will attempt a best guess based on your compiler and C version.
_dup
(duplicate) functions were renamed to _copy
. For instance: glm_vec_dup
-> glm_vec3_copy
.glm_simd_
functions are renamed to glmm_
.vec3
and mat3
types are not aligned by default.CGLM_FORCE_DEPTH_ZERO_TO_ONE
and CGLM_FORCE_LEFT_HANDED
flags are provided to control clip depth and handedness. This makes it easier to incorporate cglm into projects using graphics APIs such as Vulkan or Metal. See https://cglm.readthedocs.io/en/latest/opt.html#clipspace-option-s
Like other graphics libraries (especially OpenGL), cglm uses column-major layout to keep matrices in memory.
While we might support row-major matrices in the future, currently if you need your matrices to be in row-major layout you have to transpose them.
|
cglm contains general purpose mat4 product and inverse functions but also provides optimized versions for affine transformations. If you want to multiply two affine transformation matrices you can use glm_mul instead of glm_mat4_mul and glm_inv_tr (ROT + TR) instead glm_mat4_inv.
/* multiplication */
mat4 modelMat;
glm_mul(T, R, modelMat);
/* othonormal rot + tr matrix inverse (rigid-body) */
glm_inv_tr(modelMat);
This project exists thanks to all the people who contribute. [Contribute]
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]