Files

95 lines
1.8 KiB
C

#ifndef LINEAR_ALG_H
#define LINEAR_ALG_H
#include <stdint.h>
#define PI 3.14159265358979323846264
typedef struct {
float x, y, z;
} vec3_t;
typedef struct {
float x, y, z, w;
} vec4_t;
typedef struct {
vec4_t x, y, z, w;
} mat4_t;
typedef struct {
uint16_t x, y, z, w;
} u16_vec4_t;
// clang-format off
#define MAT4_ID \
(mat4_t) { \
{ 1, 0, 0, 0 }, \
{ 0, 1, 0, 0 }, \
{ 0, 0, 1, 0 }, \
{ 0, 0, 0, 1 }, \
}
#define MAT4_SCALE(sf) \
(mat4_t) { \
{ sf, 0, 0, 0 }, \
{ 0, sf, 0, 0 }, \
{ 0, 0, sf, 0 }, \
{ 0, 0, 0, 1 }, \
}
#define MAT4_TRANSLATE(x, y, z) \
(mat4_t) { \
{ 1, 0, 0, 0 }, \
{ 0, 1, 0, 0 }, \
{ 0, 0, 1, 0 }, \
{ x, y, z, 1 }, \
}
#define MAT4_ROTATE_X(a) \
(mat4_t) { \
{ 1, 0, 0, 0 }, \
{ 0, cosf(a), sinf(a), 0 }, \
{ 0, -sinf(a), cosf(a), 0 }, \
{ 0, 0, 0, 1 }, \
}
#define MAT4_ROTATE_Y(a) \
(mat4_t) { \
{ cosf(a), 0, -sinf(a), 0 }, \
{ 0, 1, 0, 0 }, \
{ sinf(a), 0, cosf(a), 0 }, \
{ 0, 0, 0, 1 }, \
}
#define MAT4_ROTATE_Z(a) \
(mat4_t) { \
{ cosf(a), sinf(a), 0, 0 }, \
{ -sinf(a), cosf(a), 0, 0 }, \
{ 0, 0, 1, 0 }, \
{ 0, 0, 0, 1 }, \
}
// clang-format on
vec3_t vec3_add(vec3_t v, vec3_t u);
vec3_t vec3_sub(vec3_t v, vec3_t u);
vec3_t vec3_scale(float s, vec3_t v);
float vec3_len(vec3_t v);
vec3_t vec3_norm(vec3_t v);
float vec3_dot(vec3_t v, vec3_t u);
vec3_t vec3_cross(vec3_t v, vec3_t u);
vec4_t vec4_add(vec4_t v, vec4_t u);
vec4_t vec4_scale(float s, vec4_t v);
float vec4_dot(vec4_t v, vec4_t u);
mat4_t mat4_mul(mat4_t m, mat4_t n);
mat4_t mat4_lookat(vec3_t camera, vec3_t target, vec3_t up);
mat4_t mat4_perspective(float aspect, float fov, float near, float far);
mat4_t mat4_orthographic(float width, float height, float near, float far);
mat4_t mat4_translate_vec3(vec3_t v);
mat4_t mat4_scale_vec3(vec3_t v);
mat4_t mat4_rotate_quaternion(vec4_t q);
#endif