Jump to content
  • Advertisement
Sign in to follow this  

linmath.h - a lean C99 library for linear math, aimed at graphics programming

This topic is 689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



I'm glad to proudly present you the library, that I've used with OpenGL to program a small 3D scene for my university course of computer graphics.

I love C99, so I've decided to write scene on it. But then I faced with a probleme: there's no known handy libraries to do 3D linear math for C. All libraries that I found in these days were at least awkward - how about 3 functions calls just for intermediate computations: to create temporary matrix, to compute with it and then delete it?

Except one. After all I still found a library called linmath.h by datenwolf. It was small, easy and handy enough, but it supported only column-major matrices order (to fit better into the processor cache) and had some quirks here and there. So I decided to fork it, fix all those issues and add some additional features.


What original linmath.h supports:

  • vec2, vec3, vec4: 2-, 3- and 4-element vectors respectively.
  • mat4x4: 4x4 matrices in a column-major order.
  • quat: quaternions.
  • All types above are typedef's for plain C arrays, so you can handle them on a function stack.

My fork additionaly have:

  • Optional row-major order for matrices (enabled by LINMATH_H_ROW_MAJOR define).
  • FIxed all the quirks I found like typos: "1." instead of "1.f".
  • Reflection function now defined for all vector types, not only for vec3 and vec4.
  • New functions: mat4x4_set_row() and mat4x4_set_col().
  • Union wrappers types for vectors to provide Cpp-like element access, for example: vec2.i.x and (if supported by compiler) vec2.x, instead of vec2[0].
  • Copy function for vector types, as mat4x4 have.
  • Some useful macros: cast plain vectors to union wrappers, function arguments builders etc.
  • Will be glad to add something else, dependent on your needs.

Link to the GitHub repo: https://github.com/cher-nov/linmath.h


See library source code - it's actually in just one C-header file - to understand how to use and how it works.

Any feedback is highly appreciated. GitHub stars are highly appreciated too. :)


Hope it will be useful for someone.


Edited by BlackDoomer

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!