ObjectivelyGPU
Object oriented graphics framework for SDL3 and C
Loading...
Searching...
No Matches
Mathlib.h File Reference
#include <math.h>
#include <stdbool.h>
#include <string.h>

Go to the source code of this file.

Data Structures

union  vec2
 Two-component single-precision vector. Component layout matches GLSL vec2. More...
 
union  vec3
 Three-component single-precision vector. Component layout matches GLSL vec3. More...
 
union  vec4
 Four-component single-precision vector. Component layout matches GLSL vec4. More...
 
union  mat4
 Column-major 4×4 single-precision matrix. Layout matches GLSL mat4. More...
 

Macros

#define M_PI   3.14159265358979323846
 

Functions

static float float_radians (float degrees)
 
static float float_degrees (float radians)
 
static float float_clamp (float f, float min, float max)
 
static float float_saturate (float f)
 
static float float_lerp (float a, float b, float t)
 
static float float_smoothstep (float edge0, float edge1, float f)
 
static float float_sign (float f)
 
static vec2 vec2_new (float x, float y)
 
static vec2 vec2_zero (void)
 
static vec2 vec2_one (void)
 
static vec2 vec2_add (vec2 a, vec2 b)
 
static vec2 vec2_sub (vec2 a, vec2 b)
 
static vec2 vec2_mul (vec2 a, vec2 b)
 
static vec2 vec2_scale (vec2 v, float s)
 
static vec2 vec2_negate (vec2 v)
 
static float vec2_dot (vec2 a, vec2 b)
 
static float vec2_length_squared (vec2 v)
 
static float vec2_length (vec2 v)
 
static float vec2_distance (vec2 a, vec2 b)
 
static vec2 vec2_normalize (vec2 v)
 
static vec2 vec2_min (vec2 a, vec2 b)
 
static vec2 vec2_max (vec2 a, vec2 b)
 
static vec2 vec2_lerp (vec2 a, vec2 b, float t)
 
static bool vec2_equal (vec2 a, vec2 b)
 
static vec3 vec3_new (float x, float y, float z)
 
static vec3 vec3_zero (void)
 
static vec3 vec3_one (void)
 
static vec3 vec3_add (vec3 a, vec3 b)
 
static vec3 vec3_sub (vec3 a, vec3 b)
 
static vec3 vec3_mul (vec3 a, vec3 b)
 
static vec3 vec3_scale (vec3 v, float s)
 
static vec3 vec3_negate (vec3 v)
 
static float vec3_dot (vec3 a, vec3 b)
 
static vec3 vec3_cross (vec3 a, vec3 b)
 
static float vec3_length_squared (vec3 v)
 
static float vec3_length (vec3 v)
 
static float vec3_distance (vec3 a, vec3 b)
 
static vec3 vec3_normalize (vec3 v)
 
static vec3 vec3_min (vec3 a, vec3 b)
 
static vec3 vec3_max (vec3 a, vec3 b)
 
static vec3 vec3_lerp (vec3 a, vec3 b, float t)
 
static vec3 vec3_fma (vec3 v, vec3 add, float s)
 
static vec3 vec3_reflect (vec3 v, vec3 n)
 
static bool vec3_equal (vec3 a, vec3 b)
 
static vec4 vec4_new (float x, float y, float z, float w)
 
static vec4 vec4_zero (void)
 
static vec4 vec4_one (void)
 
static vec4 vec4_add (vec4 a, vec4 b)
 
static vec4 vec4_sub (vec4 a, vec4 b)
 
static vec4 vec4_mul (vec4 a, vec4 b)
 
static vec4 vec4_scale (vec4 v, float s)
 
static vec4 vec4_negate (vec4 v)
 
static float vec4_dot (vec4 a, vec4 b)
 
static float vec4_length_squared (vec4 v)
 
static float vec4_length (vec4 v)
 
static float vec4_distance (vec4 a, vec4 b)
 
static vec4 vec4_normalize (vec4 v)
 
static vec4 vec4_min (vec4 a, vec4 b)
 
static vec4 vec4_max (vec4 a, vec4 b)
 
static vec4 vec4_lerp (vec4 a, vec4 b, float t)
 
static bool vec4_equal (vec4 a, vec4 b)
 
static mat4 mat4_new (const float src[16])
 
static mat4 mat4_from_cols (vec4 c0, vec4 c1, vec4 c2, vec4 c3)
 
static mat4 mat4_identity (void)
 
static mat4 mat4_mul (mat4 a, mat4 b)
 
static mat4 mat4_translation (vec3 t)
 
static mat4 mat4_rotation (float degrees, vec3 axis)
 
static mat4 mat4_scale3 (vec3 s)
 
static mat4 mat4_scale (float s)
 
static mat4 mat4_perspective (float fovy, float aspect, float znear, float zfar)
 
static mat4 mat4_ortho (float left, float right, float bottom, float top, float znear, float zfar)
 
static mat4 mat4_look_at (vec3 eye, vec3 center, vec3 up)
 
static mat4 mat4_inverse (mat4 a)
 
static vec3 mat4_transform (mat4 m, vec3 v)
 
static vec4 mat4_transform4 (mat4 m, vec4 v)
 

Macro Definition Documentation

◆ M_PI

#define M_PI   3.14159265358979323846

Function Documentation

◆ float_clamp()

static float float_clamp ( float  f,
float  min,
float  max 
)
inlinestatic
Returns
f clamped to [min, max].

◆ float_degrees()

static float float_degrees ( float  radians)
inlinestatic
Returns
radians converted to degrees.

◆ float_lerp()

static float float_lerp ( float  a,
float  b,
float  t 
)
inlinestatic
Returns
The linear interpolation of a and b by t.

◆ float_radians()

static float float_radians ( float  degrees)
inlinestatic
Returns
degrees converted to radians.

◆ float_saturate()

static float float_saturate ( float  f)
inlinestatic
Returns
f clamped to [0, 1].

◆ float_sign()

static float float_sign ( float  f)
inlinestatic
Returns
The sign of f: 1, -1, or 0.

◆ float_smoothstep()

static float float_smoothstep ( float  edge0,
float  edge1,
float  f 
)
inlinestatic
Returns
The smooth Hermite interpolation of f in [edge0, edge1].

◆ mat4_from_cols()

static mat4 mat4_from_cols ( vec4  c0,
vec4  c1,
vec4  c2,
vec4  c3 
)
inlinestatic
Returns
A mat4 from four column vectors.

◆ mat4_identity()

static mat4 mat4_identity ( void  )
inlinestatic
Returns
The identity matrix.

◆ mat4_inverse()

static mat4 mat4_inverse ( mat4  a)
inlinestatic
Returns
The inverse of m. Returns the identity if m is singular.

◆ mat4_look_at()

static mat4 mat4_look_at ( vec3  eye,
vec3  center,
vec3  up 
)
inlinestatic
Returns
A right-handed view matrix looking from eye toward center, with up as the up vector.

◆ mat4_mul()

static mat4 mat4_mul ( mat4  a,
mat4  b 
)
inlinestatic
Returns
The product a * b.

◆ mat4_new()

static mat4 mat4_new ( const float  src[16])
inlinestatic
Returns
A mat4 from a column-major flat array of 16 floats.

◆ mat4_ortho()

static mat4 mat4_ortho ( float  left,
float  right,
float  bottom,
float  top,
float  znear,
float  zfar 
)
inlinestatic
Returns
A right-handed orthographic projection matrix.

◆ mat4_perspective()

static mat4 mat4_perspective ( float  fovy,
float  aspect,
float  znear,
float  zfar 
)
inlinestatic
Returns
A right-handed perspective projection matrix (depth maps to [-1, 1]).
Parameters
fovyVertical field-of-view in degrees.
aspectViewport width / height.
znearNear clip distance (positive).
zfarFar clip distance (positive).

◆ mat4_rotation()

static mat4 mat4_rotation ( float  degrees,
vec3  axis 
)
inlinestatic
Returns
A rotation matrix for degrees around the (normalized) axis.

◆ mat4_scale()

static mat4 mat4_scale ( float  s)
inlinestatic
Returns
A uniform scale matrix.

◆ mat4_scale3()

static mat4 mat4_scale3 ( vec3  s)
inlinestatic
Returns
A non-uniform scale matrix.

◆ mat4_transform()

static vec3 mat4_transform ( mat4  m,
vec3  v 
)
inlinestatic
Returns
The vec3 point v transformed by matrix m (w=1, perspective divide applied).

◆ mat4_transform4()

static vec4 mat4_transform4 ( mat4  m,
vec4  v 
)
inlinestatic
Returns
The vec4 vector v transformed by matrix m.

◆ mat4_translation()

static mat4 mat4_translation ( vec3  t)
inlinestatic
Returns
A translation matrix for vector t.

◆ vec2_add()

static vec2 vec2_add ( vec2  a,
vec2  b 
)
inlinestatic
Returns
The sum a + b.

◆ vec2_distance()

static float vec2_distance ( vec2  a,
vec2  b 
)
inlinestatic
Returns
The distance between a and b.

◆ vec2_dot()

static float vec2_dot ( vec2  a,
vec2  b 
)
inlinestatic
Returns
The dot product a · b.

◆ vec2_equal()

static bool vec2_equal ( vec2  a,
vec2  b 
)
inlinestatic
Returns
True if a and b are exactly equal.

◆ vec2_length()

static float vec2_length ( vec2  v)
inlinestatic
Returns
The length of v.

◆ vec2_length_squared()

static float vec2_length_squared ( vec2  v)
inlinestatic
Returns
The squared length of v.

◆ vec2_lerp()

static vec2 vec2_lerp ( vec2  a,
vec2  b,
float  t 
)
inlinestatic
Returns
The linear interpolation of a and b by t.

◆ vec2_max()

static vec2 vec2_max ( vec2  a,
vec2  b 
)
inlinestatic
Returns
The component-wise maximum of a and b.

◆ vec2_min()

static vec2 vec2_min ( vec2  a,
vec2  b 
)
inlinestatic
Returns
The component-wise minimum of a and b.

◆ vec2_mul()

static vec2 vec2_mul ( vec2  a,
vec2  b 
)
inlinestatic
Returns
The component-wise product a * b.

◆ vec2_negate()

static vec2 vec2_negate ( vec2  v)
inlinestatic
Returns
The negation of v.

◆ vec2_new()

static vec2 vec2_new ( float  x,
float  y 
)
inlinestatic
Returns
A vec2 with the given components.

◆ vec2_normalize()

static vec2 vec2_normalize ( vec2  v)
inlinestatic
Returns
The unit vector of v, or v if zero-length.

◆ vec2_one()

static vec2 vec2_one ( void  )
inlinestatic
Returns
The ones vector (1, 1).

◆ vec2_scale()

static vec2 vec2_scale ( vec2  v,
float  s 
)
inlinestatic
Returns
v scaled by s.

◆ vec2_sub()

static vec2 vec2_sub ( vec2  a,
vec2  b 
)
inlinestatic
Returns
The difference a - b.

◆ vec2_zero()

static vec2 vec2_zero ( void  )
inlinestatic
Returns
The zero vector (0, 0).

◆ vec3_add()

static vec3 vec3_add ( vec3  a,
vec3  b 
)
inlinestatic
Returns
The sum a + b.

◆ vec3_cross()

static vec3 vec3_cross ( vec3  a,
vec3  b 
)
inlinestatic
Returns
The cross product a × b.

◆ vec3_distance()

static float vec3_distance ( vec3  a,
vec3  b 
)
inlinestatic
Returns
The distance between a and b.

◆ vec3_dot()

static float vec3_dot ( vec3  a,
vec3  b 
)
inlinestatic
Returns
The dot product a · b.

◆ vec3_equal()

static bool vec3_equal ( vec3  a,
vec3  b 
)
inlinestatic
Returns
True if a and b are exactly equal.

◆ vec3_fma()

static vec3 vec3_fma ( vec3  v,
vec3  add,
float  s 
)
inlinestatic
Returns
v + (add * s). Fused multiply-add — useful for accumulation without a temporary.

◆ vec3_length()

static float vec3_length ( vec3  v)
inlinestatic
Returns
The length of v.

◆ vec3_length_squared()

static float vec3_length_squared ( vec3  v)
inlinestatic
Returns
The squared length of v.

◆ vec3_lerp()

static vec3 vec3_lerp ( vec3  a,
vec3  b,
float  t 
)
inlinestatic
Returns
The linear interpolation of a and b by t.

◆ vec3_max()

static vec3 vec3_max ( vec3  a,
vec3  b 
)
inlinestatic
Returns
The component-wise maximum of a and b.

◆ vec3_min()

static vec3 vec3_min ( vec3  a,
vec3  b 
)
inlinestatic
Returns
The component-wise minimum of a and b.

◆ vec3_mul()

static vec3 vec3_mul ( vec3  a,
vec3  b 
)
inlinestatic
Returns
The component-wise product a * b.

◆ vec3_negate()

static vec3 vec3_negate ( vec3  v)
inlinestatic
Returns
The negation of v.

◆ vec3_new()

static vec3 vec3_new ( float  x,
float  y,
float  z 
)
inlinestatic
Returns
A vec3 with the given components.

◆ vec3_normalize()

static vec3 vec3_normalize ( vec3  v)
inlinestatic
Returns
The unit vector of v, or v if zero-length.

◆ vec3_one()

static vec3 vec3_one ( void  )
inlinestatic
Returns
The ones vector (1, 1, 1).

◆ vec3_reflect()

static vec3 vec3_reflect ( vec3  v,
vec3  n 
)
inlinestatic
Returns
The reflection of v about normal n. n must be normalized.

◆ vec3_scale()

static vec3 vec3_scale ( vec3  v,
float  s 
)
inlinestatic
Returns
v scaled by s.

◆ vec3_sub()

static vec3 vec3_sub ( vec3  a,
vec3  b 
)
inlinestatic
Returns
The difference a - b.

◆ vec3_zero()

static vec3 vec3_zero ( void  )
inlinestatic
Returns
The zero vector (0, 0, 0).

◆ vec4_add()

static vec4 vec4_add ( vec4  a,
vec4  b 
)
inlinestatic
Returns
The sum a + b.

◆ vec4_distance()

static float vec4_distance ( vec4  a,
vec4  b 
)
inlinestatic
Returns
The distance between a and b.

◆ vec4_dot()

static float vec4_dot ( vec4  a,
vec4  b 
)
inlinestatic
Returns
The dot product a · b.

◆ vec4_equal()

static bool vec4_equal ( vec4  a,
vec4  b 
)
inlinestatic
Returns
True if a and b are exactly equal.

◆ vec4_length()

static float vec4_length ( vec4  v)
inlinestatic
Returns
The length of v.

◆ vec4_length_squared()

static float vec4_length_squared ( vec4  v)
inlinestatic
Returns
The squared length of v.

◆ vec4_lerp()

static vec4 vec4_lerp ( vec4  a,
vec4  b,
float  t 
)
inlinestatic
Returns
The linear interpolation of a and b by t.

◆ vec4_max()

static vec4 vec4_max ( vec4  a,
vec4  b 
)
inlinestatic
Returns
The component-wise maximum of a and b.

◆ vec4_min()

static vec4 vec4_min ( vec4  a,
vec4  b 
)
inlinestatic
Returns
The component-wise minimum of a and b.

◆ vec4_mul()

static vec4 vec4_mul ( vec4  a,
vec4  b 
)
inlinestatic
Returns
The component-wise product a * b.

◆ vec4_negate()

static vec4 vec4_negate ( vec4  v)
inlinestatic
Returns
The negation of v.

◆ vec4_new()

static vec4 vec4_new ( float  x,
float  y,
float  z,
float  w 
)
inlinestatic
Returns
A vec4 with the given components.

◆ vec4_normalize()

static vec4 vec4_normalize ( vec4  v)
inlinestatic
Returns
The unit vector of v, or v if zero-length.

◆ vec4_one()

static vec4 vec4_one ( void  )
inlinestatic
Returns
The ones vector (1, 1, 1, 1).

◆ vec4_scale()

static vec4 vec4_scale ( vec4  v,
float  s 
)
inlinestatic
Returns
v scaled by s.

◆ vec4_sub()

static vec4 vec4_sub ( vec4  a,
vec4  b 
)
inlinestatic
Returns
The difference a - b.

◆ vec4_zero()

static vec4 vec4_zero ( void  )
inlinestatic
Returns
The zero vector (0, 0, 0, 0).