C
The original glm library is for C++ only (templates, namespaces, classes...), this library targeted to C99 but currently you can use it for C89 safely by language extensions e.g __restrict
Almost all functions (inline versions) and parameters are documented inside related headers.
Complete documentation: http://cglm.readthedocs.io
glm_vec_dup -> glm_vec_copy
If you don't aware about original GLM library yet, you may also want to look at:
https://github.com/g-truc/glm
vec4
and mat4
variables must be aligned. (There will be unaligned versions later)cglm
doesn't alloc any memory on heap. So it doesn't provide any allocator. You should alloc memory for out parameters too if you pass pointer of memory location. Don't forget that vec4 (also quat/versor) and mat4 must be aligned (16-bytes), because cglm uses SIMD instructions to optimize most operations if available.
Since almost all types are arrays and C doesn't allow returning arrays, so cglm doesn't support this feature. In the future cglm may use struct for some types for this purpose.
Currently cglm uses default clip space configuration (-1, 1) for camera functions (perspective, extract corners...), in the future other clip space configurations will be supported
Like some other graphics libraries (especially OpenGL) this library use Column-Major layout to keep matrices in the memory.
In the future the library may support an option to use row-major layout, CURRENTLY if you need to row-major layout you will need to transpose it.
|
|
You have two option to call a function/operation: inline or library call (link)
Almost all functions are marked inline (always_inline) so compiler probably will inline.
To call pre-compiled version, just use glmc_
(c stands for 'call') instead of glm_
.
#include <cglm/cglm.h> /* for inline */
#include <cglm/call.h> /* for library call (this also includes cglm.h) */
mat4 rot, trans, rt;
/* ... */
glm_mul(trans, rot, rt); /* inline */
glmc_mul(trans, rot, rt); /* call from library */
Most of math functions are optimized manualy with SSE2 if available, if not? Dont worry there are non-sse versions of all operations
You can pass matrices and vectors as array to functions rather than get address.
mat4 m = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
glm_translate(m, (vec3){1.0f, 0.0f, 0.0f});
Library contains general purpose mat4 mul and inverse functions but also contains some special form (optimized) of these functions for affine transform matrices. If you want to multiply two affine transform 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]
MIT. check the LICENSE file
$ sh ./build-deps.sh # run only once (dependencies) [Optional].
$ # You can pass this step if you don't want to run `make check` for tests.
$ # cglm uses cmocka for tests and it may reqiure cmake for building it
$
$ sh autogen.sh
$ ./configure
$ make
$ make check # [Optional] (if you run `sh ./build-deps.sh`)
$ [sudo] make install
Windows related build files, project files are located in win
folder,
make sure you are inside cglm/win
folder.
Code Analysis are enabled, it may take awhile to build
$ cd win
$ .\build.bat
if msbuild
won't work (because of multi version VS) then try to build with devenv
:
$ devenv cglm.sln /Build Release
First you need install Sphinx: http://www.sphinx-doc.org/en/master/usage/installation.html
then:
$ cd docs
$ sphinx-build source build
it will compile docs into build folder, you can run index.html inside that function.
If you want to use inline versions of funcstions then; include main header
#include <cglm/cglm.h>
the header will include all headers. Then call func you want e.g. rotate vector by axis:
glm_vec_rotate(v1, glm_rad(45), (vec3){1.0f, 0.0f, 0.0f});
some functions are overloaded :) e.g you can normalize vector:
glm_vec_normalize(vec);
this will normalize vec and store normalized vector into vec
but if you will store normalized vector into another vector do this:
glm_vec_normalize_to(vec, result);
like this function you may see _to
postfix, this functions store results to another variables and save temp memory
to call pre-compiled versions include header with c
postfix, c means call. Pre-compiled versions are just wrappers.
#include <cglm/call.h>
this header will include all headers with c postfix. You need to call functions with c posfix:
glmc_vec_normalize(vec);
Function usage and parameters are documented inside related headers. You may see same parameter passed twice in some examples like this:
glm_mat4_mul(m1, m2, m1);
/* or */
glm_mat4_mul(m1, m1, m1);
the first two parameter are [in] and the last one is [out] parameter. After multiplied m1 and m2 the result is stored in m1. This is why we send m1 twice. You may store result in different matrix, this just an example.
mat4 proj, view, model, mvp;
/* init proj, view and model ... */
glm_mat4_mul(proj, view, viewProj);
glm_mat4_mul(viewProj, model, mvp);
mat4 proj, view, model, mvp;
/* init proj, view and model ... */
glm_mat4_mulN((mat4 *[]){&proj, &view, &model}, 3, mvp);
mat4 is array of vec4 and vec4 is array of floats. glUniformMatrix4fv
functions accecpts float*
as value
(last param), so you can cast mat4 to float* or you can pass first column of matrix as beginning of memory of matrix:
Option 1: Send first column
glUniformMatrix4fv(location, 1, GL_FALSE, matrix[0]);
/* array of matrices */
glUniformMatrix4fv(location, 1, GL_FALSE, matrix[0][0]);
Option 2: Cast matrix to pointer type (also valid for multiple dimensional arrays)
glUniformMatrix4fv(location, 1, GL_FALSE, (float *)matrix);
You can pass same way to another APIs e.g. Vulkan, DX...
TODO:
- [ ] Unit tests (In Progress)
- [ ] Unit tests for comparing cglm with glm results
- [x] Add version info
- [ ] Unaligned operations (e.g. glm_umat4_mul
)
- [x] Extra documentation
- [ ] ARM Neon Arch (In Progress)