-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_vec4.h
200 lines (168 loc) · 4.42 KB
/
math_vec4.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifndef MATH_VEC4_MATH_H
#define MATH_VEC4_MATH_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "math_inc.h"
typedef float vec4[4]; // v[0]: x, v[1]: y, v[2]: z, v[3]: w
typedef int ivec4[4]; // v[0]: x, v[1]: y, v[2]: z, v[3]: w
// ---- vec4 creation ----
#define VEC4_INIT(f) { f, f, f, f }
#define VEC4_XYZW_INIT(x, y, z, w) { x, y, z, w }
#define VEC4_X_INIT(x) { x, 0, 0, 0 }
#define VEC4_Y_INIT(y) { 0, y, 0, 0 }
#define VEC4_Z_INIT(z) { 0, 0, z, 0 }
#define VEC4_W_INIT(w) { 0, 0, 0, w }
#define VEC4(f) (vec4){ f, f, f, f }
#define VEC4_XYZW(x, y, z, w) (vec4){ x, y, z, w }
#define VEC4_X(x) (vec4){ x, 0, 0, 0 }
#define VEC4_Y(y) (vec4){ 0, y, 0, 0 }
#define VEC4_Z(z) (vec4){ 0, 0, z, 0 }
#define VEC4_W(w) (vec4){ 0, 0, 0, w }
// ---- print utils ----
#define P_VEC4(v) printf("|%s| x: %.2f, y: %.2f, z: %.2f, w: %.f\n", #v, v[0], v[1], v[2], v[3])
#define P_IL_VEC4(v) printf("|%s| x: %.2f, y: %.2f, z: %.2f, w: %.f", #v, v[0], v[1], v[2], v[3])
// ---- vec4 ----
M_INLINE void vec4_add(vec4 a, vec4 b, vec4 out)
{
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
}
M_INLINE void vec4_add_f(vec4 a, float f, vec4 out)
{
out[0] = a[0] + f;
out[1] = a[1] + f;
out[2] = a[2] + f;
out[3] = a[3] + f;
}
M_INLINE void vec4_sub(vec4 a, vec4 b, vec4 out)
{
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
out[3] = a[3] - b[3];
}
M_INLINE void vec4_sub_f(vec4 a, float f, vec4 out)
{
out[0] = a[0] - f;
out[1] = a[1] - f;
out[2] = a[2] - f;
out[3] = a[3] - f;
}
M_INLINE void vec4_mul(vec4 a, vec3 b, vec4 out)
{
out[0] = a[0] * b[0];
out[1] = a[1] * b[1];
out[2] = a[2] * b[2];
out[3] = a[3] * b[4];
}
M_INLINE void vec4_mul_f(vec4 a, float f, vec4 out)
{
out[0] = a[0] * f;
out[1] = a[1] * f;
out[2] = a[2] * f;
out[3] = a[3] * f;
}
M_INLINE float vec4_dot(vec4 a, vec4 b)
{
return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]) + (a[3] * b[3]);
}
M_INLINE float vec4_magnitude(vec4 a) // length of vec
{
return sqrtf((a[0] * a[0]) + (a[1] * a[1]) + (a[2] * a[2]) + (a[3] * a[3]));
}
M_INLINE void vec4_copy(vec4 a, vec4 out)
{
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
}
M_INLINE void vec4_negate(vec4 a, vec4 out)
{
out[0] = -a[0];
out[1] = -a[1];
out[2] = -a[2];
out[3] = -a[3];
}
M_INLINE float vec4_distance(vec4 a, vec4 b)
{
vec4 d;
vec4_sub(a, b, d);
return vec4_magnitude(d);
}
M_INLINE void vec4_clamp(vec4 a, vec4 min, vec4 max, vec4 out)
{
out[0] = CLAMP(a[0], min[0], max[0]);
out[1] = CLAMP(a[1], min[1], max[1]);
out[2] = CLAMP(a[2], min[2], max[2]);
out[3] = CLAMP(a[3], min[3], max[3]);
}
M_INLINE void vec4_clamp_f(vec4 a, vec4 min, vec4 max, vec4 out)
{
out[0] = CLAMP(a[0], min[0], max[0]);
out[1] = CLAMP(a[1], min[1], max[1]);
out[2] = CLAMP(a[2], min[2], max[2]);
out[3] = CLAMP(a[3], min[3], max[3]);
}
M_INLINE void vec4_abs(vec4 a, vec4 out)
{
out[0] = fabsf(a[0]);
out[1] = fabsf(a[1]);
out[2] = fabsf(a[2]);
out[3] = fabsf(a[3]);
}
// ---- interpolation ----
M_INLINE void vec4_lerp(vec4 start, vec4 end, float percentage, vec4 out)
{
out[0] = m_lerp(start[0], end[0], percentage);
out[1] = m_lerp(start[1], end[1], percentage);
out[2] = m_lerp(start[2], end[2], percentage);
out[3] = m_lerp(start[3], end[3], percentage);
}
M_INLINE void vec4_lerp_f(float start, float end, float percentage, vec4 out)
{
out[0] = m_lerp(start, end, percentage);
out[1] = m_lerp(start, end, percentage);
out[2] = m_lerp(start, end, percentage);
out[3] = m_lerp(start, end, percentage);
}
// ---- conditionals ----
M_INLINE bool vec4_equal(vec4 a, vec4 b)
{
// return a[0] == b[0] && a[1] == b[1] &&
// a[2] == b[2] && a[3] == b[3];
return FLOAT_EQ(a[0], b[0]) && FLOAT_EQ(a[1], b[1]) &&
FLOAT_EQ(a[2], b[2]) && FLOAT_EQ(a[3], b[3]);
}
M_INLINE bool vec4_not_equal(vec4 a, vec4 b)
{
return !vec4_equal(a, b);
}
M_INLINE bool vec4_greater(vec4 a, vec4 b)
{
return a[0] > b[0] && a[1] > b[1] &&
a[2] > b[2] && a[3] > b[3];
}
M_INLINE bool vec4_less(vec4 a, vec4 b)
{
return a[0] < b[0] && a[1] < b[1] &&
a[2] < b[2] && a[3] < b[3];
}
M_INLINE bool vec4_greater_eq(vec4 a, vec4 b)
{
return a[0] >= b[0] && a[1] >= b[1] &&
a[2] >= b[2] && a[3] >= b[3];
}
M_INLINE bool vec4_less_eq(vec4 a, vec4 b)
{
return a[0] <= b[0] && a[1] <= b[1] &&
a[2] >= b[2] && a[3] <= b[3];
}
#ifdef __cplusplus
} // extern C
#endif
#endif