PDA

View Full Version : Happy to introduce new 3d math library for C



recpas
01-11-2018, 01:21 AM
I was working on a math library for C for a while and I want to let OpenGL community know about its existence.

It is called cglm here the link: http://github.com/recp/cglm
It is similar to original glm library but for C.

Most vec4/mat4 functions are optimized with SIMD (SSE, AVX, Neon ...) instructions and some of them also optimized manually. There are inline and non-inline versions of functions. I'm trying to add convenient functions for some common problems. You can see full feature list in README. It is in active development and some features may be added by time.

Quaternion part may need to be updated later. It stores quat as [w x y z] this may change to [ x y z w] later (I'm not sure) and more features will be added by time.

Also any feedbacks are welcome (always).

recpas
01-18-2018, 01:34 PM
Just added some new features:

Frustum:
- glm_frustum_planes, glm_frustum_corners, glm_frustum_center
- glm_frustum_box this gives AABB of frustum by transforming corners with given matrix
- glm_frustum_corners_at this is fantastic; it gives plane's corners which splits frustum without using any matrix. It requires existing corners so when you get 4 corners of split with this func actually you will have all 8 corners of split

Bounding Volumes:
- glm_aabb_transform transforms AABB ( vec3[2] ) with given matrix
- glm_aabb_merge merges / combines two AABB and creates new bbox
- glm_aabb_crop picks intersected part of two AABB. For instance if view frustum's bbox is larger than scene than glm_aabb_crop(frustumBox, sceneBox, dest) gives smaller bbox (scene < bbox < frustum). This could be useful for shadowmaps.
- glm_aabb_frustum checks if AABB intersects with frustum (using planes)

Vector:
- glm_vec_ortho it gives a possible orthogonal / perpendicular vector of given vector

Camera:
- glm_look it is just wrapper for glm_lookat (same as glm::lookat) except it accepts direction instead of target. Sometimes you only have direction so this saves you to find target and pass it to lookat.
- glm_look_anyup this wrapper for glm_look except it doesn't requires direction or target. It generates a perpendicular vector with glm_vec_ortho and uses it as UP vector.

Sample usage: Find shadow matrices for directional light (Simple Shadow Map), real CSM/PSSM implementation (correct or wrong) can be found on my Github profile (recp)


mat4 invViewProj;

glm_mat4_inv(cam->viewProj, invViewProj);

glm_frustum_planes(cam->viewProj, cam->frustum.planes);
glm_frustum_corners(invViewProj, cam->frustum.corners);
glm_frustum_center(cam->frustum.corners, cam->frustum.center);



mat4 view, proj;
vec3 frustumBox[2], boxInFrustum[2], finalBox[2];

glm_vec_broadcast(FLT_MAX, boxInFrustum[0]);
glm_vec_broadcast(-FLT_MAX, boxInFrustum[1]);

for (i = 0; i < objCount; i++)
glm_aabb_merge(boxInFrustum, objects[i]->bbox->world, boxInFrustum);

glm_look_anyup(cam->frustum.center, light->dir, view);
glm_frustum_box(cam->frustum.corners, view, frustumBox);

glm_aabb_transform(boxInFrustum, view, boxInFrustum);
glm_aabb_crop(frustumBox, boxInFrustum, finalBox);
glm_ortho_aabb(finalBox, proj);

glm_mat4_mul(proj, view, viewProj);


another


if (glm_aabb_frustum(object->bbox, camera->frustum.planes)) {
/* object intersects with frustum, gather and render it */
} else {
/* object is in outside of frustum */
}


These new features may need more tests, currently I'm working on COLLADA/glTF importer, renderer and viewer and I'm testing this with that projects. Testing with different projects may help to fix wrong math (if exists) and bugs more quickly, otherwise you must wait until I find, learn and fix them :)