Dreambox C/C++ SDK v1.0.0
C/C++ SDK for creating Dreambox games
Loading...
Searching...
No Matches
db_math.h File Reference
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  Vec2
 A two-component vector. More...
 
struct  Vec3
 A three-component vector. More...
 
struct  Vec4
 A four-component vector. More...
 
struct  Quaternion
 A quaternion. More...
 
struct  Mat4
 A 4x4 matrix. More...
 

Macros

#define PI   3.14159265359f
 
#define DEG_2_RAD   (PI / 180.0f)
 
#define RAD_2_DEG   (180.0f / PI)
 
#define MAT4_IDENTITY
 
#define QUATERNION_IDENTITY    (Quaternion) { 0.0f, 0.0f, 0.0f, 1.0f }
 

Functions

float vec2_length (Vec2 v)
 Get the length of a vector. More...
 
float vec3_length (Vec3 v)
 Get the length of a vector. More...
 
float vec2_lengthSq (Vec2 v)
 Get the squared length of a vector. More...
 
float vec3_lengthSq (Vec3 v)
 Get the squared length of a vector. More...
 
float vec2_distance (Vec2 lhs, Vec2 rhs)
 Get the distance between two vectors. More...
 
float vec3_distance (Vec3 lhs, Vec3 rhs)
 Get the distance between two vectors. More...
 
float vec2_distanceSq (Vec2 lhs, Vec2 rhs)
 Get the squared distance between two vectors. More...
 
float vec3_distanceSq (Vec3 lhs, Vec3 rhs)
 Get the squared distance between two vectors. More...
 
float clamp (float value, float min, float max)
 Clamp the value between a minimum and maximum. More...
 
float lerp (float lhs, float rhs, float t)
 Linearly interpolate between two values. More...
 
Vec2 vec2_lerp (Vec2 lhs, Vec2 rhs, float t)
 Linearly interpolate between two values. More...
 
Vec3 vec3_lerp (Vec3 lhs, Vec3 rhs, float t)
 Linearly interpolate between two values. More...
 
Vec4 vec4_lerp (Vec4 lhs, Vec4 rhs, float t)
 Linearly interpolate between two values. More...
 
Vec2 vec2_mul (Vec2 lhs, Vec2 rhs)
 Componentwise-mutiply two vectors. More...
 
Vec3 vec3_mul (Vec3 lhs, Vec3 rhs)
 Componentwise-mutiply two vectors. More...
 
Vec4 vec4_mul (Vec4 lhs, Vec4 rhs)
 Componentwise-mutiply two vectors. More...
 
Vec2 vec2_add (Vec2 lhs, Vec2 rhs)
 Add two vectors. More...
 
Vec3 vec3_add (Vec3 lhs, Vec3 rhs)
 Add two vectors. More...
 
Vec4 vec4_add (Vec4 lhs, Vec4 rhs)
 Add two vectors. More...
 
Vec2 vec2_sub (Vec2 lhs, Vec2 rhs)
 Subtract two vectors. More...
 
Vec3 vec3_sub (Vec3 lhs, Vec3 rhs)
 Subtract two vectors. More...
 
Vec4 vec4_sub (Vec4 lhs, Vec4 rhs)
 Subtract two vectors. More...
 
Vec2 vec2_div (Vec2 lhs, Vec2 rhs)
 Divide two vectors. More...
 
Vec3 vec3_div (Vec3 lhs, Vec3 rhs)
 Divide two vectors. More...
 
Vec4 vec4_div (Vec4 lhs, Vec4 rhs)
 Divide two vectors. More...
 
float vec2_dot (Vec2 lhs, Vec2 rhs)
 Compute the dot product of the given vectors. More...
 
float vec3_dot (Vec3 lhs, Vec3 rhs)
 Compute the dot product of the given vectors. More...
 
float vec4_dot (Vec4 lhs, Vec4 rhs)
 Compute the dot product of the given vectors. More...
 
Vec3 vec3_cross (Vec3 lhs, Vec3 rhs)
 Compute the cross product of the given vectors. More...
 
void vec2_normalize (Vec2 *v)
 Normalize the given vector. More...
 
void vec3_normalize (Vec3 *v)
 Normalize the given vector. More...
 
void vec4_normalize (Vec4 *v)
 Normalize the given vector. More...
 
void quat_normalize (Quaternion *q)
 Normalize the given quaternion. More...
 
void quat_invert (Quaternion *q)
 Invert the given quaternion. More...
 
Vec3 vec3_transformQuat (Quaternion q, Vec3 v)
 Rotate a vector by a quaternion. More...
 
Quaternion quat_mul (Quaternion lhs, Quaternion rhs)
 Multiply two quaternions together. More...
 
Quaternion quat_fromEuler (Vec3 eulerAngles)
 Construct a quaternion from rotation around each axis. More...
 
Vec4 vec4_transform (Mat4 mat, Vec4 vec)
 Transform a single vector with a matrix without the aid of the SIMD matrix unit. More...
 
Mat4 mat4_translate (Vec3 translation)
 Construct a matrix from the given translation. More...
 
Mat4 mat4_scale (Vec3 scale)
 Construct a matrix from the given scale. More...
 
Mat4 mat4_rotation (Quaternion rotation)
 Construct a matrix from the given rotation. More...
 
Mat4 mat4_projectionOrtho (float left, float right, float top, float bottom, float near, float far)
 Construct an orthographic projection matrix. More...
 
Mat4 mat4_projectionOrthoAspect (float aspectRatio, float scale, float near, float far)
 Construct an orthographic projection matrix. More...
 
Mat4 mat4_projectionPerspective (float aspectRatio, float fieldOfView, float near, float far)
 Construct a perspective projection matrix. More...
 
Mat4 mat4_mul (Mat4 lhs, Mat4 rhs)
 Multiply two matrices together manually without the aid of the SIMD matrix unit. More...
 
void mat4_loadIdentitySIMD ()
 Load identity matrix into the SIMD matrix register. More...
 
void mat4_loadSIMD (const Mat4 *mat)
 Load a matrix into the SIMD matrix register. More...
 
void mat4_storeSIMD (Mat4 *mat)
 Store the value in the SIMD matrix register to the given matrix. More...
 
void mat4_mulSIMD (const Mat4 *mat)
 Multiply internal SIMD matrix register with the given matrix. More...
 
void mat4_transformSIMD (const Vec4 *invec, Vec4 *outvec, uint32_t count, uint32_t stride)
 Transform a list of input Vec4s using the SIMD matrix unit. More...
 

Macro Definition Documentation

◆ DEG_2_RAD

#define DEG_2_RAD   (PI / 180.0f)

Definition at line 11 of file db_math.h.

◆ MAT4_IDENTITY

#define MAT4_IDENTITY
Value:
(Mat4) \
{ \
1.0f, 0.0f, 0.0f, 0.0f, \
0.0f, 1.0f, 0.0f, 0.0f, \
0.0f, 0.0f, 1.0f, 0.0f, \
0.0f, 0.0f, 0.0f, 1.0f \
}
A 4x4 matrix.
Definition: db_math.h:61

Definition at line 14 of file db_math.h.

◆ PI

#define PI   3.14159265359f

Definition at line 10 of file db_math.h.

◆ QUATERNION_IDENTITY

#define QUATERNION_IDENTITY    (Quaternion) { 0.0f, 0.0f, 0.0f, 1.0f }

Definition at line 23 of file db_math.h.

◆ RAD_2_DEG

#define RAD_2_DEG   (180.0f / PI)

Definition at line 12 of file db_math.h.

Function Documentation

◆ clamp()

float clamp ( float  value,
float  min,
float  max 
)

Clamp the value between a minimum and maximum.

Parameters
value
min
max
Returns
A value clamped between min and max

◆ lerp()

float lerp ( float  lhs,
float  rhs,
float  t 
)

Linearly interpolate between two values.

Parameters
lhs
rhs
t
Returns
A value between lhs and rhs

◆ mat4_loadIdentitySIMD()

void mat4_loadIdentitySIMD ( )

Load identity matrix into the SIMD matrix register.

◆ mat4_loadSIMD()

void mat4_loadSIMD ( const Mat4 mat)

Load a matrix into the SIMD matrix register.

Parameters
matThe matrix to load

◆ mat4_mul()

Mat4 mat4_mul ( Mat4  lhs,
Mat4  rhs 
)

Multiply two matrices together manually without the aid of the SIMD matrix unit.

Parameters
lhsMatrix on the lefthand side
rhsMatrix on the righthand side
Returns
The multiplied matrix result

◆ mat4_mulSIMD()

void mat4_mulSIMD ( const Mat4 mat)

Multiply internal SIMD matrix register with the given matrix.

Parameters
matThe matrix to multiply into the SIMD matrix register

◆ mat4_projectionOrtho()

Mat4 mat4_projectionOrtho ( float  left,
float  right,
float  top,
float  bottom,
float  near,
float  far 
)

Construct an orthographic projection matrix.

Parameters
leftThe left side of the frustum
rightThe right side of the frustum
topThe top side of the frustum
bottomThe bottom side of the frustum
nearThe near clip distance
farThe far clip distance
Returns
An orthographic projection matrix

◆ mat4_projectionOrthoAspect()

Mat4 mat4_projectionOrthoAspect ( float  aspectRatio,
float  scale,
float  near,
float  far 
)

Construct an orthographic projection matrix.

Parameters
aspectRatioThe aspect ratio of the frustum (width/height)
scaleThe height of the frustum
nearThe near clip distance
farThe far clip distance
Returns
An orthographic projection matrix centered at origin

◆ mat4_projectionPerspective()

Mat4 mat4_projectionPerspective ( float  aspectRatio,
float  fieldOfView,
float  near,
float  far 
)

Construct a perspective projection matrix.

Parameters
aspectRatioThe aspect ratio of the frustum (width/height)
fieldOfViewThe vertical field of view in radians
nearThe near clip distance
farThe far clip distance
Returns
A perspective projection matrix centered at origin

◆ mat4_rotation()

Mat4 mat4_rotation ( Quaternion  rotation)

Construct a matrix from the given rotation.

Parameters
rotationThe rotation
Returns
A rotation matrix

◆ mat4_scale()

Mat4 mat4_scale ( Vec3  scale)

Construct a matrix from the given scale.

Parameters
scaleThe scale
Returns
A scale matrix

◆ mat4_storeSIMD()

void mat4_storeSIMD ( Mat4 mat)

Store the value in the SIMD matrix register to the given matrix.

Parameters
matThe matrix to store the value into

◆ mat4_transformSIMD()

void mat4_transformSIMD ( const Vec4 invec,
Vec4 outvec,
uint32_t  count,
uint32_t  stride 
)

Transform a list of input Vec4s using the SIMD matrix unit.

Parameters
invecPointer to list of input vectors to transform
outvecPointer to list of output vectors to write
countNumber of vectors to transform
strideNumber of bytes between each vector (0 implies tightly packed array of Vec4)

◆ mat4_translate()

Mat4 mat4_translate ( Vec3  translation)

Construct a matrix from the given translation.

Parameters
translationThe translation
Returns
A translation matrix

◆ quat_fromEuler()

Quaternion quat_fromEuler ( Vec3  eulerAngles)

Construct a quaternion from rotation around each axis.

Parameters
eulerAnglesRotation around each axis in radians
Returns
An equivalent quaternion

◆ quat_invert()

void quat_invert ( Quaternion q)

Invert the given quaternion.

Parameters
qPointer to the quaternion to invert

◆ quat_mul()

Quaternion quat_mul ( Quaternion  lhs,
Quaternion  rhs 
)

Multiply two quaternions together.

Parameters
lhsQuaternion on the lefthand side
rhsQuaternion on the righthand side
Returns
The multiplied result

◆ quat_normalize()

void quat_normalize ( Quaternion q)

Normalize the given quaternion.

Parameters
qThe quaternion to normalize

◆ vec2_add()

Vec2 vec2_add ( Vec2  lhs,
Vec2  rhs 
)

Add two vectors.

Parameters
lhs
rhs
Returns
The sum of both vectors

◆ vec2_distance()

float vec2_distance ( Vec2  lhs,
Vec2  rhs 
)

Get the distance between two vectors.

Parameters
lhs
rhs
Returns
The distance between the given vectors

◆ vec2_distanceSq()

float vec2_distanceSq ( Vec2  lhs,
Vec2  rhs 
)

Get the squared distance between two vectors.

Parameters
lhs
rhs
Returns
The squared distance between the given vectors

◆ vec2_div()

Vec2 vec2_div ( Vec2  lhs,
Vec2  rhs 
)

Divide two vectors.

Parameters
lhs
rhs
Returns
The quotient of both vectors

◆ vec2_dot()

float vec2_dot ( Vec2  lhs,
Vec2  rhs 
)

Compute the dot product of the given vectors.

Parameters
lhsThe vector on the lefthand side
rhsThe vector on the righthand side
Returns
The dot product of the two vectors

◆ vec2_length()

float vec2_length ( Vec2  v)

Get the length of a vector.

Parameters
v
Returns
The length of the vector

◆ vec2_lengthSq()

float vec2_lengthSq ( Vec2  v)

Get the squared length of a vector.

Parameters
v
Returns
The squared length of the vector

◆ vec2_lerp()

Vec2 vec2_lerp ( Vec2  lhs,
Vec2  rhs,
float  t 
)

Linearly interpolate between two values.

Parameters
lhs
rhs
t
Returns
A value between lhs and rhs

◆ vec2_mul()

Vec2 vec2_mul ( Vec2  lhs,
Vec2  rhs 
)

Componentwise-mutiply two vectors.

Parameters
lhs
rhs
Returns
The product of both vectors

◆ vec2_normalize()

void vec2_normalize ( Vec2 v)

Normalize the given vector.

Parameters
vThe vector to normalize

◆ vec2_sub()

Vec2 vec2_sub ( Vec2  lhs,
Vec2  rhs 
)

Subtract two vectors.

Parameters
lhs
rhs
Returns
The difference of both vectors

◆ vec3_add()

Vec3 vec3_add ( Vec3  lhs,
Vec3  rhs 
)

Add two vectors.

Parameters
lhs
rhs
Returns
The sum of both vectors

◆ vec3_cross()

Vec3 vec3_cross ( Vec3  lhs,
Vec3  rhs 
)

Compute the cross product of the given vectors.

Parameters
lhsThe vector on the lefthand side
rhsThe vector on the righthand side
Returns
The cross product of the two vectors

◆ vec3_distance()

float vec3_distance ( Vec3  lhs,
Vec3  rhs 
)

Get the distance between two vectors.

Parameters
lhs
rhs
Returns
The distance between the given vectors

◆ vec3_distanceSq()

float vec3_distanceSq ( Vec3  lhs,
Vec3  rhs 
)

Get the squared distance between two vectors.

Parameters
lhs
rhs
Returns
The squared distance between the given vectors

◆ vec3_div()

Vec3 vec3_div ( Vec3  lhs,
Vec3  rhs 
)

Divide two vectors.

Parameters
lhs
rhs
Returns
The quotient of both vectors

◆ vec3_dot()

float vec3_dot ( Vec3  lhs,
Vec3  rhs 
)

Compute the dot product of the given vectors.

Parameters
lhsThe vector on the lefthand side
rhsThe vector on the righthand side
Returns
The dot product of the two vectors

◆ vec3_length()

float vec3_length ( Vec3  v)

Get the length of a vector.

Parameters
v
Returns
The length of the vector

◆ vec3_lengthSq()

float vec3_lengthSq ( Vec3  v)

Get the squared length of a vector.

Parameters
v
Returns
The squared length of the vector

◆ vec3_lerp()

Vec3 vec3_lerp ( Vec3  lhs,
Vec3  rhs,
float  t 
)

Linearly interpolate between two values.

Parameters
lhs
rhs
t
Returns
A value between lhs and rhs

◆ vec3_mul()

Vec3 vec3_mul ( Vec3  lhs,
Vec3  rhs 
)

Componentwise-mutiply two vectors.

Parameters
lhs
rhs
Returns
The product of both vectors

◆ vec3_normalize()

void vec3_normalize ( Vec3 v)

Normalize the given vector.

Parameters
vThe vector to normalize

◆ vec3_sub()

Vec3 vec3_sub ( Vec3  lhs,
Vec3  rhs 
)

Subtract two vectors.

Parameters
lhs
rhs
Returns
The difference of both vectors

◆ vec3_transformQuat()

Vec3 vec3_transformQuat ( Quaternion  q,
Vec3  v 
)

Rotate a vector by a quaternion.

Parameters
qThe quaternion representing a rotation
vThe vector to rotate
Returns
A rotated vector

◆ vec4_add()

Vec4 vec4_add ( Vec4  lhs,
Vec4  rhs 
)

Add two vectors.

Parameters
lhs
rhs
Returns
The sum of both vectors

◆ vec4_div()

Vec4 vec4_div ( Vec4  lhs,
Vec4  rhs 
)

Divide two vectors.

Parameters
lhs
rhs
Returns
The quotient of both vectors

◆ vec4_dot()

float vec4_dot ( Vec4  lhs,
Vec4  rhs 
)

Compute the dot product of the given vectors.

Parameters
lhsThe vector on the lefthand side
rhsThe vector on the righthand side
Returns
The dot product of the two vectors

◆ vec4_lerp()

Vec4 vec4_lerp ( Vec4  lhs,
Vec4  rhs,
float  t 
)

Linearly interpolate between two values.

Parameters
lhs
rhs
t
Returns
A value between lhs and rhs

◆ vec4_mul()

Vec4 vec4_mul ( Vec4  lhs,
Vec4  rhs 
)

Componentwise-mutiply two vectors.

Parameters
lhs
rhs
Returns
The product of both vectors

◆ vec4_normalize()

void vec4_normalize ( Vec4 v)

Normalize the given vector.

Parameters
vThe vector to normalize

◆ vec4_sub()

Vec4 vec4_sub ( Vec4  lhs,
Vec4  rhs 
)

Subtract two vectors.

Parameters
lhs
rhs
Returns
The difference of both vectors

◆ vec4_transform()

Vec4 vec4_transform ( Mat4  mat,
Vec4  vec 
)

Transform a single vector with a matrix without the aid of the SIMD matrix unit.

Parameters
matThe transform matrix
vecThe vector to transform
Returns
The transformed vector