LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024-2025 ETMSoftware
math_3d.h File Reference

Go to the source code of this file.

Data Structures

struct  vec3_t
 
union  mat4_t
 

Functions

mat4_t m4_rotation (float angle_in_rad, vec3_t axis)
 
mat4_t m4_ortho_RH (float left, float right, float bottom, float top, float back, float front)
 
mat4_t m4_perspective_RH (float vertical_field_of_view_in_deg, float aspect_ratio, float near_view_distance, float far_view_distance)
 
mat4_t m4_look_at_RH (vec3_t from, vec3_t to, vec3_t up)
 
mat4_t m4_frustum (float left, float right, float bottom, float top, float near, float far)
 
mat4_t m4_invert_affine (mat4_t matrix)
 
vec3_t m4_mul_pos (mat4_t matrix, vec3_t position)
 
vec3_t m4_mul_dir (mat4_t matrix, vec3_t direction)
 
void m4_print (mat4_t matrix)
 
void m4_printp (mat4_t matrix, int width, int precision)
 
void m4_fprint (FILE *stream, mat4_t matrix)
 
void m4_fprintp (FILE *stream, mat4_t matrix, int width, int precision)
 
void m4_print2 (mat4_t matrix, const char *line_start)
 
void m4_printp2 (mat4_t matrix, int width, int precision, const char *line_start)
 
void m4_fprint2 (FILE *stream, mat4_t matrix, const char *line_start)
 
void m4_fprintp2 (FILE *stream, mat4_t matrix, int width, int precision, const char *line_start)
 

Detailed Description

Math 3D v1.0 - by Stephan Soller steph.nosp@m.an.s.nosp@m.oller.nosp@m.@hel.nosp@m.ionwe.nosp@m.b.de and Tobias Malmsheimer

Licensed under the MIT license

See: https://github.com/arkanis/single-header-file-c-libs/blob/master/math_3d.h


A few (mainly cosmetic) changes by Emmanuel Thomas-Maurin manu@.nosp@m.etms.nosp@m.oftwa.nosp@m.re.n.nosp@m.et

  • m4_frustum() added
  • Some new matrix info functions
  • New matrix funcs specifying RH/LH - everything here was originally RH (All matrices are column-major-order)
  • Now:
    • m4_ortho() -> m4_ortho_RH()
    • m4_perspective() -> m4_perspective_RH()
    • m4_look_at() -> m4_look_at_RH()
  • Not implemented:
    • m4_ortho_LH (float left, float right, float bottom, float top, float back, float front)
    • m4_perspective_LH (float vertical_field_of_view_in_deg, float aspect_ratio, float near_view_distance, float far_view_distance)
    • m4_look_at_LH (vec3_t from, vec3_t to, vec3_t up)
  • Indentation and coding style

Last change on 2025-02-22


Math 3D v1.0 By Stephan Soller steph.nosp@m.an.s.nosp@m.oller.nosp@m.@hel.nosp@m.ionwe.nosp@m.b.de and Tobias Malmsheimer Licensed under the MIT license

Math 3D is a compact C99 library meant to be used with OpenGL. It provides basic 3D vector and 4x4 matrix operations as well as functions to create transformation and projection matrices. The OpenGL binary layout is used so you can just upload vectors and matrices into shaders and work with them without any conversions.

It's an stb style single header file library. Define MATH_3D_IMPLEMENTATION before you include this file in one C file to create the implementation.

QUICK NOTES

  • If not explicitly stated by a parameter name all angles are in radians.
  • The matrices use column-major indices. This is the same as in OpenGL and GLSL. See the matrix documentation below for details.
  • Matrices are passed by value. This is probably a bit inefficient but simplifies code quite a bit. Most operations will be inlined by the compiler anyway so the difference shouldn't matter that much. A matrix fits into 4 of the 16 SSE2 registers anyway. If profiling shows significant slowdowns the matrix type might change but ease of use is more important than every last percent of performance.
  • When combining matrices with multiplication the effects apply right to left. This is the convention used in mathematics and OpenGL. Source: https://en.wikipedia.org/wiki/Transformation_matrix#Composing_and_inverting_transformations Direct3D does it differently.
  • The m4_mul_pos() and m4_mul_dir() functions do a correct perspective divide (division by w) when necessary. This is a bit slower but ensures that the functions will properly work with projection matrices. If profiling shows this is a bottleneck special functions without perspective division can be added. But the normal multiplications should avoid any surprises.
  • The library consistently uses a right-handed coordinate system. The old glOrtho() broke that rule and m4_ortho() has be slightly modified so you can always think of right-handed cubes that are projected into OpenGLs normalized device coordinates.
  • Special care has been taken to document all complex operations and important sources. Most code is covered by test cases that have been manually calculated and checked on the whiteboard. Since indices and math code is prone to be confusing we used pair programming to avoid mistakes.

FURTHER IDEAS

These are ideas for future work on the library. They're implemented as soon as there is a proper use case and we can find good names for them.

  • bool v3_is_null(vec3_t v, float epsilon) To check if the length of a vector is smaller than epsilon.
  • vec3_t v3_length_default(vec3_t v, float default_length, float epsilon) Returns default_length if the length of v is smaller than epsilon. Otherwise same as v3_length().
  • vec3_t v3_norm_default(vec3_t v, vec3_t default_vector, float epsilon) Returns default_vector if the length of v is smaller than epsilon. Otherwise the same as v3_norm().
  • mat4_t m4_invert(mat4_t matrix) Matrix inversion that works with arbitrary matrices. m4_invert_affine() can already invert translation, rotation, scaling, mirroring, reflection and shearing matrices. So a general inversion might only be useful to invert projection matrices for picking. But with orthographic and perspective projection it's probably simpler to calculate the ray into the scene directly based on the screen coordinates.

VERSION HISTORY

v1.0 2016-02-15 Initial release