Space Sim
Loading...
Searching...
No Matches
mathf.h
Go to the documentation of this file.
1#ifndef MATHF_H
2#define MATHF_H
3
4#include <math.h>
5#include "core/types.h"
6
7static inline vec3 vec3_normalize(vec3 v) {
8 float len = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
9 if (len == 0.0f) return v;
10 v.x /= len;
11 v.y /= len;
12 v.z /= len;
13
14 return v;
15}
16static inline vec3 ivec3_normalize(ivec3 a)
17{
18 vec3 v = {(float)a.x, (float)a.y, (float)a.z};
19 float len = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
20 if (len == 0.0f) return v;
21 v.x /= len;
22 v.y /= len;
23 v.z /= len;
24
25 return v;
26}
27static inline vec3 vec3_cross(vec3 a, vec3 b) {
28 vec3 out;
29 out.x = a.y*b.z - a.z*b.y;
30 out.y = a.z*b.x - a.x*b.z;
31 out.z = a.x*b.y - a.y*b.x;
32 return out;
33}
34
35static inline vec2 vec2_scale(vec2 a, float b)
36{
37 return (vec2){a.x * b, a.y * b};
38}
39static inline vec3 vec3_scale(vec3 a, float b)
40{
41 return (vec3){a.x * b, a.y * b, a.z * b};
42}
43static inline vec4 vec4_scale(vec4 a, float b)
44{
45 return (vec4){a.x * b, a.y * b, a.z * b, a.w * b};
46}
47
48static inline float ivec3_dist(ivec3 a, ivec3 b)
49{
50 int ix = a.x - b.x, iy = a.y - b.y, iz = a.z - b.z;
51 return sqrtf(ix * ix + iy * iy + iz * iz);
52}
53static inline ivec3 ivec3_sub(ivec3 a, ivec3 b)
54{
55 return (ivec3){a.x - b.x, a.y - b.y, a.z - b.z};
56}
57static inline vec3 vec3_sub(vec3 a, vec3 b)
58{
59 return (vec3){a.x - b.x, a.y - b.y, a.z - b.z};
60}
61static inline vec2 vec2_add(vec2 a, vec2 b)
62{
63 return (vec2){a.x + b.x, a.y + b.y};
64}
65static inline ivec3 ivec3_add(ivec3 a, ivec3 b)
66{
67 return (ivec3){a.x + b.x, a.y + b.y, a.z + b.z};
68}
69static inline vec3 ivec3_scale(ivec3 a, float b)
70{
71 return (vec3){a.x * b, a.y * b, a.z * b};
72}
73static inline vec3 vec3_add(vec3 a, vec3 b)
74{
75 return (vec3){a.x + b.x, a.y + b.y, a.z + b.z};
76}
77static inline vec4 vec4_add(vec4 a, vec4 b)
78{
79 return (vec4){a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
80}
81static inline vec4 vec4_sub(vec4 a, vec4 b)
82{
83 return (vec4){a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
84}
85static inline vec3 vec3_mul(vec3 a, vec3 b)
86{
87 return (vec3){a.x * b.x, a.y * b.y, a.z * b.z};
88}
89static inline vec4 vec4_mul(vec4 a, vec4 b)
90{
91 return (vec4){a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w};
92}
93static inline float vec3_dist2(vec3 a, vec3 b) {
94 vec3 d = vec3_sub(a, b);
95 return d.x*d.x + d.y*d.y + d.z*d.z;
96}
97static inline float vec3_dist(vec3 a, vec3 b) {
98 vec3 d = vec3_sub(a, b);
99 return fabsf(d.x) + fabsf(d.y) + fabsf(d.z);
100}
101static inline float vec3_dot(vec3 a, vec3 b) {
102 return a.x*b.x + a.y*b.y + a.z*b.z;
103}
104static inline float vec2_dot(vec2 a, vec2 b) {
105 return a.x*b.x + a.y*b.y;
106}
107static inline float vec3_length(vec3 v) {
108 return sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
109}
110
111static inline mat4 mat4_inverse(mat4 in) {
112
113 vec3 a = { in.a0, in.a1, in.a2 }; // X basis
114 vec3 b = { in.b0, in.b1, in.b2 }; // Y basis
115 vec3 c = { in.c0, in.c1, in.c2 }; // Z basis
116 vec3 d = { in.d0, in.d1, in.d2 }; // Translation
117
118 vec3 cross_bc = vec3_cross(b, c);
119 float det = vec3_dot(a, cross_bc);
120 float invDet = 1.0f / det;
121 vec3 ia = vec3_cross(b, c);
122 vec3 ib = vec3_cross(c, a);
123 vec3 ic = vec3_cross(a, b);
124
125 ia.x *= invDet; ia.y *= invDet; ia.z *= invDet;
126 ib.x *= invDet; ib.y *= invDet; ib.z *= invDet;
127 ic.x *= invDet; ic.y *= invDet; ic.z *= invDet;
128
129 vec3 id = {
130 -vec3_dot(ia, d),
131 -vec3_dot(ib, d),
132 -vec3_dot(ic, d)
133 };
134
135 mat4 out;
136 out.a0=ia.x; out.a1=ia.y; out.a2=ia.z; out.a3=0.0f;
137 out.b0=ib.x; out.b1=ib.y; out.b2=ib.z; out.b3=0.0f;
138 out.c0=ic.x; out.c1=ic.y; out.c2=ic.z; out.c3=0.0f;
139 out.d0=id.x; out.d1=id.y; out.d2=id.z; out.d3=1.0f;
140
141 return out;
142}
143static inline float lerp(float a, float b, float t) {
144 return a + (b - a) * t;
145}
146
147static inline float clamp(float a, float b, float c){
148
149}
150static inline vec4 mul_m4_v4(mat4 m, vec4 v) {
151 vec4 out;
152 out.x = m.a0 * v.x + m.b0 * v.y + m.c0 * v.z + m.d0 * v.w;
153 out.y = m.a1 * v.x + m.b1 * v.y + m.c1 * v.z + m.d1 * v.w;
154 out.z = m.a2 * v.x + m.b2 * v.y + m.c2 * v.z + m.d2 * v.w;
155 out.w = m.a3 * v.x + m.b3 * v.y + m.c3 * v.z + m.d3 * v.w;
156 return out;
157}
158
159static inline mat4 m4_mul(mat4 a, mat4 b) {
160 mat4 out;
161 vec4 b0 = { b.a0, b.a1, b.a2, b.a3 };
162 vec4 b1 = { b.b0, b.b1, b.b2, b.b3 };
163 vec4 b2 = { b.c0, b.c1, b.c2, b.c3 };
164 vec4 b3 = { b.d0, b.d1, b.d2, b.d3 };
165
166 vec4 r0 = mul_m4_v4(a, b0);
167 vec4 r1 = mul_m4_v4(a, b1);
168 vec4 r2 = mul_m4_v4(a, b2);
169 vec4 r3 = mul_m4_v4(a, b3);
170
171 out.a0 = r0.x; out.a1 = r0.y; out.a2 = r0.z; out.a3 = r0.w;
172 out.b0 = r1.x; out.b1 = r1.y; out.b2 = r1.z; out.b3 = r1.w;
173 out.c0 = r2.x; out.c1 = r2.y; out.c2 = r2.z; out.c3 = r2.w;
174 out.d0 = r3.x; out.d1 = r3.y; out.d2 = r3.z; out.d3 = r3.w;
175
176 return out;
177}
178
179static inline void mat4_mul(float out[16], const float a[16], const float b[16]) {
180 for (int row = 0; row < 4; ++row) {
181 for (int col = 0; col < 4; ++col) {
182 out[col * 4 + row] =
183 a[0 * 4 + row] * b[col * 4 + 0] +
184 a[1 * 4 + row] * b[col * 4 + 1] +
185 a[2 * 4 + row] * b[col * 4 + 2] +
186 a[3 * 4 + row] * b[col * 4 + 3];
187 }
188 }
189}
190
191#define MAX(a, b) (a > b ? (a) : (b))
192
193#define MIN(a, b) (a < b ? (a) : (b))
194
195
196static inline uint32_t expand_bits(uint32_t v)
197{
198 v = (v * 0x00010001u) & 0xFF0000FFu;
199 v = (v * 0x00000101u) & 0x0F00F00Fu;
200 v = (v * 0x00000011u) & 0xC30C30C3u;
201 v = (v * 0x00000005u) & 0x49249249u;
202 return v;
203}
204
205static inline uint32_t morton(uint32_t x, uint32_t y, uint32_t z)
206{
207 return expand_bits(x) | (expand_bits(y) << 1) | (expand_bits(z) << 2);
208}
209
210#endif
vec3 vec3_cross(vec3 a, vec3 b)
Definition mathf.h:27
float vec3_dist(vec3 a, vec3 b)
Definition mathf.h:97
float vec3_dot(vec3 a, vec3 b)
Definition mathf.h:101
float vec2_dot(vec2 a, vec2 b)
Definition mathf.h:104
ivec3 ivec3_sub(ivec3 a, ivec3 b)
Definition mathf.h:53
vec3 ivec3_scale(ivec3 a, float b)
Definition mathf.h:69
vec4 vec4_add(vec4 a, vec4 b)
Definition mathf.h:77
vec3 vec3_normalize(vec3 v)
Definition mathf.h:7
vec3 ivec3_normalize(ivec3 a)
Definition mathf.h:16
mat4 m4_mul(mat4 a, mat4 b)
Definition mathf.h:159
float ivec3_dist(ivec3 a, ivec3 b)
Definition mathf.h:48
vec3 vec3_scale(vec3 a, float b)
Definition mathf.h:39
vec4 mul_m4_v4(mat4 m, vec4 v)
Definition mathf.h:150
void mat4_mul(float out[16], const float a[16], const float b[16])
Definition mathf.h:179
vec3 vec3_mul(vec3 a, vec3 b)
Definition mathf.h:85
vec4 vec4_mul(vec4 a, vec4 b)
Definition mathf.h:89
vec2 vec2_add(vec2 a, vec2 b)
Definition mathf.h:61
vec2 vec2_scale(vec2 a, float b)
Definition mathf.h:35
vec4 vec4_sub(vec4 a, vec4 b)
Definition mathf.h:81
vec3 vec3_sub(vec3 a, vec3 b)
Definition mathf.h:57
vec3 vec3_add(vec3 a, vec3 b)
Definition mathf.h:73
float lerp(float a, float b, float t)
Definition mathf.h:143
vec4 vec4_scale(vec4 a, float b)
Definition mathf.h:43
float clamp(float a, float b, float c)
Definition mathf.h:147
ivec3 ivec3_add(ivec3 a, ivec3 b)
Definition mathf.h:65
mat4 mat4_inverse(mat4 in)
Definition mathf.h:111
float vec3_dist2(vec3 a, vec3 b)
Definition mathf.h:93
float vec3_length(vec3 v)
Definition mathf.h:107
uint expand_bits(uint v)
Definition morton.comp:59
uint morton(uint x, uint y, uint z)
Definition morton.comp:68
float x
Definition types.h:45
float a1
Definition types.h:51
float w
Definition types.h:47
float z
Definition types.h:47
float d3
Definition types.h:51
float c2
Definition types.h:51
float b0
Definition types.h:51
float c1
Definition types.h:51
float x
Definition types.h:47
float a2
Definition types.h:51
float x
Definition main.h:5
int y
Definition types.h:48
float b3
Definition types.h:51
float y
Definition types.h:45
float y
Definition types.h:47
float y
Definition main.h:5
float d1
Definition types.h:51
float a3
Definition types.h:51
int z
Definition types.h:48
float c0
Definition types.h:51
float z
Definition main.h:5
int x
Definition types.h:48
float b2
Definition types.h:51
float d0
Definition types.h:51
float a0
Definition types.h:51
float c3
Definition types.h:51
float b1
Definition types.h:51
float d2
Definition types.h:51
Definition types.h:48
Definition types.h:51
Definition types.h:45
Definition main.h:5
Definition types.h:47
uniform float t
Definition water.comp:23