Happy to introduce new 3d math library for C

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: GitHub - recp/cglm: 📽 Highly Optimized Graphics Math (glm) for C
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).

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 :slight_smile:

EDIT:

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.

The quaternion api has been fully updated with new features and tests. Now it uses [x y z w] order. Please read the docs, and README before upgrade library version. If your codes depend on previous order ([w x y z]) then new cglm version will break your codes.

Also alignment is now optional. You can see all critical changes in README (see “Note for previous versions” section) this will help to switch to new version.

After implemented Vulkan in my render engine then I’ll update cglm to support Vulkan coordinate system (NDC), currently it is waiting in TODO list.

There may some bugs in some functions (especially in new ones), if you catch them please report by creating an issue or you can fix it by creating a PR :wink:

Nice!Thanks for this info, guys!


There are no solved problems; there are only problems that are more or less solved.
All the best,Uabran Diceus

[QUOTE=recpas;1291925]EDIT:

The quaternion api has been fully updated with new features and tests. Now it uses [x y z w] order. Please read the docs, and README before upgrade library version. If your codes depend on previous order ([w x y z]) then new cglm version will break your codes.

Also alignment is now optional. You can see all critical changes in README (see “Note for previous versions” section) this will help to switch to new version.

After implemented Vulkan in my render engine then I’ll update cglm to support Vulkan coordinate system (NDC), currently it is waiting in TODO list.

There may some bugs in some functions (especially in new ones), if you catch them please report by creating an issue or you can fix it by creating a PR ;)[/QUOTE]

Hey, can you post example code for setting up the camera with these functions, since they replace all that stuff (glMatrixMode, gluPerspective, gluLookAt etc.)
and put some random objects on the scene? It would be extremely helpful, since I’m struggling hard for my college project. Thanks in advance!