Dreambox C/C++ SDK v1.0.0
C/C++ SDK for creating Dreambox games
Loading...
Searching...
No Matches
db_math.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#ifdef __cplusplus
6extern "C"
7{
8#endif
9
10#define PI 3.14159265359f
11#define DEG_2_RAD (PI / 180.0f)
12#define RAD_2_DEG (180.0f / PI)
13
14#define MAT4_IDENTITY \
15 (Mat4) \
16 { \
17 1.0f, 0.0f, 0.0f, 0.0f, \
18 0.0f, 1.0f, 0.0f, 0.0f, \
19 0.0f, 0.0f, 1.0f, 0.0f, \
20 0.0f, 0.0f, 0.0f, 1.0f \
21 }
22
23#define QUATERNION_IDENTITY \
24 (Quaternion) { 0.0f, 0.0f, 0.0f, 1.0f }
25
27typedef struct
28{
29 float x;
30 float y;
31} Vec2;
32
34typedef struct
35{
36 float x;
37 float y;
38 float z;
39} Vec3;
40
42typedef struct
43{
44 float x;
45 float y;
46 float z;
47 float w;
48} Vec4;
49
51typedef struct
52{
53 float x;
54 float y;
55 float z;
56 float w;
58
60typedef struct
61{
62 float m[4][4];
63} Mat4;
64
69
74
79
84
89float vec2_distance(Vec2 lhs, Vec2 rhs);
90
95float vec3_distance(Vec3 lhs, Vec3 rhs);
96
101float vec2_distanceSq(Vec2 lhs, Vec2 rhs);
102
107float vec3_distanceSq(Vec3 lhs, Vec3 rhs);
108
114float clamp(float value, float min, float max);
115
121float lerp(float lhs, float rhs, float t);
122
128Vec2 vec2_lerp(Vec2 lhs, Vec2 rhs, float t);
129
135Vec3 vec3_lerp(Vec3 lhs, Vec3 rhs, float t);
136
142Vec4 vec4_lerp(Vec4 lhs, Vec4 rhs, float t);
143
149
155
161
167
173
179
185
191
197
203
209
215
220float vec2_dot(Vec2 lhs, Vec2 rhs);
221
226float vec3_dot(Vec3 lhs, Vec3 rhs);
227
232float vec4_dot(Vec4 lhs, Vec4 rhs);
233
239
243
247
251
255
259
265
271
276
282
287
292
297
306Mat4 mat4_projectionOrtho(float left, float right, float top, float bottom, float near, float far);
307
314Mat4 mat4_projectionOrthoAspect(float aspectRatio, float scale, float near, float far);
315
322Mat4 mat4_projectionPerspective(float aspectRatio, float fieldOfView, float near, float far);
323
329
332
335extern void mat4_loadSIMD(const Mat4 *mat);
336
339extern void mat4_storeSIMD(Mat4 *mat);
340
343extern void mat4_mulSIMD(const Mat4 *mat);
344
350extern void mat4_transformSIMD(const Vec4 *invec, Vec4 *outvec, uint32_t count, uint32_t stride);
351
352#ifdef __cplusplus
353}
354#endif
float vec3_distance(Vec3 lhs, Vec3 rhs)
Get the distance between two vectors.
Mat4 mat4_projectionPerspective(float aspectRatio, float fieldOfView, float near, float far)
Construct a perspective projection matrix.
Vec3 vec3_mul(Vec3 lhs, Vec3 rhs)
Componentwise-mutiply two vectors.
float vec2_lengthSq(Vec2 v)
Get the squared length of a vector.
Vec3 vec3_cross(Vec3 lhs, Vec3 rhs)
Compute the cross product of the given vectors.
Quaternion quat_mul(Quaternion lhs, Quaternion rhs)
Multiply two quaternions together.
Vec2 vec2_mul(Vec2 lhs, Vec2 rhs)
Componentwise-mutiply two vectors.
float vec3_distanceSq(Vec3 lhs, Vec3 rhs)
Get the squared distance between two vectors.
void mat4_loadSIMD(const Mat4 *mat)
Load a matrix into the SIMD matrix register.
Vec3 vec3_lerp(Vec3 lhs, Vec3 rhs, float t)
Linearly interpolate between two values.
Vec2 vec2_lerp(Vec2 lhs, Vec2 rhs, float t)
Linearly interpolate between two values.
Vec4 vec4_lerp(Vec4 lhs, Vec4 rhs, float t)
Linearly interpolate between two values.
float vec2_distance(Vec2 lhs, Vec2 rhs)
Get the distance between two vectors.
float vec4_dot(Vec4 lhs, Vec4 rhs)
Compute the dot product of the given vectors.
void vec4_normalize(Vec4 *v)
Normalize the given vector.
Vec4 vec4_mul(Vec4 lhs, Vec4 rhs)
Componentwise-mutiply two vectors.
Vec4 vec4_sub(Vec4 lhs, Vec4 rhs)
Subtract two vectors.
Mat4 mat4_translate(Vec3 translation)
Construct a matrix from the given translation.
Mat4 mat4_projectionOrtho(float left, float right, float top, float bottom, float near, float far)
Construct an orthographic projection matrix.
float clamp(float value, float min, float max)
Clamp the value between a minimum and maximum.
Vec2 vec2_div(Vec2 lhs, Vec2 rhs)
Divide two vectors.
float vec2_dot(Vec2 lhs, Vec2 rhs)
Compute the dot product of the given vectors.
Vec3 vec3_add(Vec3 lhs, Vec3 rhs)
Add two vectors.
Vec4 vec4_add(Vec4 lhs, Vec4 rhs)
Add two vectors.
void quat_normalize(Quaternion *q)
Normalize the given quaternion.
float vec2_distanceSq(Vec2 lhs, Vec2 rhs)
Get the squared distance between two vectors.
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.
float vec3_length(Vec3 v)
Get the length of a vector.
float vec3_lengthSq(Vec3 v)
Get the squared length of a vector.
Vec3 vec3_transformQuat(Quaternion q, Vec3 v)
Rotate a vector by a quaternion.
Quaternion quat_fromEuler(Vec3 eulerAngles)
Construct a quaternion from rotation around each axis.
void vec3_normalize(Vec3 *v)
Normalize the given vector.
Mat4 mat4_mul(Mat4 lhs, Mat4 rhs)
Multiply two matrices together manually without the aid of the SIMD matrix unit.
void mat4_mulSIMD(const Mat4 *mat)
Multiply internal SIMD matrix register with the given matrix.
Mat4 mat4_projectionOrthoAspect(float aspectRatio, float scale, float near, float far)
Construct an orthographic projection matrix.
Vec3 vec3_div(Vec3 lhs, Vec3 rhs)
Divide two vectors.
Vec3 vec3_sub(Vec3 lhs, Vec3 rhs)
Subtract two vectors.
Vec2 vec2_sub(Vec2 lhs, Vec2 rhs)
Subtract two vectors.
void mat4_storeSIMD(Mat4 *mat)
Store the value in the SIMD matrix register to the given matrix.
Vec4 vec4_div(Vec4 lhs, Vec4 rhs)
Divide two vectors.
void vec2_normalize(Vec2 *v)
Normalize the given vector.
Vec4 vec4_transform(Mat4 mat, Vec4 vec)
Transform a single vector with a matrix without the aid of the SIMD matrix unit.
float vec2_length(Vec2 v)
Get the length of a vector.
float lerp(float lhs, float rhs, float t)
Linearly interpolate between two values.
Mat4 mat4_scale(Vec3 scale)
Construct a matrix from the given scale.
float vec3_dot(Vec3 lhs, Vec3 rhs)
Compute the dot product of the given vectors.
Mat4 mat4_rotation(Quaternion rotation)
Construct a matrix from the given rotation.
void mat4_loadIdentitySIMD()
Load identity matrix into the SIMD matrix register.
void quat_invert(Quaternion *q)
Invert the given quaternion.
Vec2 vec2_add(Vec2 lhs, Vec2 rhs)
Add two vectors.
A 4x4 matrix.
Definition: db_math.h:61
A quaternion.
Definition: db_math.h:52
float w
Definition: db_math.h:56
float y
Definition: db_math.h:54
float x
Definition: db_math.h:53
float z
Definition: db_math.h:55
A two-component vector.
Definition: db_math.h:28
float y
Definition: db_math.h:30
float x
Definition: db_math.h:29
A three-component vector.
Definition: db_math.h:35
float y
Definition: db_math.h:37
float x
Definition: db_math.h:36
float z
Definition: db_math.h:38
A four-component vector.
Definition: db_math.h:43
float w
Definition: db_math.h:47
float y
Definition: db_math.h:45
float x
Definition: db_math.h:44
float z
Definition: db_math.h:46