1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#include "mathlib.h"
#include "../../math/math.h"
#define INTERNAL_FUNCTION(T, NAME)\
T NAME(T v1, T v2) {\
T out; \
internal_##NAME(&v1, &v2, &out);\
return out;\
}
/************************************************************************/
/* Vec2 */
/************************************************************************/
Vec2 vec2(float x, float y) {
Vec2 v = {x, y};
return v;
}
INTERNAL_FUNCTION(Vec2, vec2_minus);
INTERNAL_FUNCTION(Vec2, vec2_plus);
float vec2_dot(Vec2 v1, Vec2 v2) {
return internal_vec2_dot(&v1, &v2);
}
/************************************************************************/
/* Vec3 */
/************************************************************************/
Vec3 vec3(float x, float y, float z) {
Vec3 v = {x, y, z};
return v;
}
INTERNAL_FUNCTION(Vec3, vec3_minus);
INTERNAL_FUNCTION(Vec3, vec3_plus);
INTERNAL_FUNCTION(Vec3, vec3_cross);
float vec3_dot(Vec3 v1, Vec3 v2) {
return internal_vec3_dot(&v1, &v2);
}
Vec3 vec3_lerp(Vec3 v1, Vec3 v2, float t) {
Vec3 out;
internal_vec3_lerp(&v1, &v2, t, &out);
return out;
}
Vec3 vec3_slerp(Vec3 v1, Vec3 v2, float t) {
Vec3 out;
internal_vec3_slerp(&v1, &v2, t, &out);
return out;
}
Vec3 vec3_scale(Vec3 v, float scale) {
Vec3 out;
internal_vec3_scale(&v, scale, &out);
return out;
}
Vec3 vec3_normalize(Vec3 v) {
Vec3 out;
internal_vec3_normalize(&v, &out);
return out;
}
/************************************************************************/
/* Vec4 */
/************************************************************************/
Vec4 vec4(float x, float y, float z, float w) {
Vec4 v = {x, y, z, w};
return v;
}
INTERNAL_FUNCTION(Vec4, vec4_minus);
INTERNAL_FUNCTION(Vec4, vec4_plus);
Vec4 vec4_scale(Vec4 v, float scale) {
Vec4 out;
internal_vec4_scale(&v, scale, &out);
return out;
}
Vec4 vec4_saturate(Vec4 v) {
Vec4 out;
out.x = clamp(v.x, 0, 1);
out.y = clamp(v.y, 0, 1);
out.z = clamp(v.z, 0, 1);
out.w = clamp(v.w, 0, 1);
return out;
}
/************************************************************************/
/* Matrix */
/************************************************************************/
Mat3 mat3(Vec3 c1, Vec3 c2, Vec3 c3) {
Mat3 m = {c1, c2, c3};
return m;
}
Vec3 mat3_mulvec3(Mat3 m, Vec3 v) {
Vec3 out;
internal_mat3_multvec3(&m, &v, &out);
return out;
}
Mat3 mat3_mulmat3(Mat3 m, Mat3 m2) {
Mat3 out;
internal_mat3_multmat3(&m, &m2, &out);
return out;
}
Mat4 mat4(Vec4 c1, Vec4 c2, Vec4 c3, Vec4 c4) {
Mat4 m ;
m.axisx = c1;
m.axisy = c2;
m.axisz = c3;
m.pos = c4;
return m;
}
Vec3 mat4_mulvec3(Mat4 m, Vec3 v) {
Vec4 v2 = {v.x, v.y, v.z, 0};
return mat4_mulvec4(m, v2).xyz;
}
Vec4 mat4_mulvec4(Mat4 m, Vec4 v) {
Vec4 out;
internal_mat4_mulvec4(&m, &v, &out);
return out;
}
Mat4 mat4_mulmat4(Mat4 m1, Mat4 m2) {
Mat4 out;
internal_mat4_multiply(&m1, &m2, &out);
return out;
}
|