/
geom.c
371 lines (295 loc) · 8.2 KB
/
geom.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#define _GNU_SOURCE /* for sincosf */
#include <math.h>
#include "geom.h"
#define EPSILON (1e-6f)
const vec3_t vec_px = VEC3i( 1, 0, 0);
const vec3_t vec_nx = VEC3i(-1, 0, 0);
const vec3_t vec_py = VEC3i( 0, 1, 0);
const vec3_t vec_ny = VEC3i( 0,-1, 0);
const vec3_t vec_pz = VEC3i( 0, 0, 1);
const vec3_t vec_nz = VEC3i( 0, 0,-1);
void vec3_cross(vec3_t *out, const vec3_t *a, const vec3_t *b)
{
out->x = a->y * b->z - a->z * b->y;
out->y = a->z * b->x - a->x * b->z;
out->z = a->x * b->y - a->y * b->x;
}
float vec3_dot(const vec3_t *a, const vec3_t *b)
{
return a->x*b->x + a->y*b->y + a->z*b->z;
}
float vec3_magnitude(const vec3_t *v)
{
return sqrtf(vec3_dot(v, v));
}
void vec3_scale(vec3_t *v, float scale)
{
v->x *= scale;
v->y *= scale;
v->z *= scale;
}
void vec3_normalize(vec3_t *v)
{
float len = vec3_magnitude(v);
if (len < 1.e-5)
return;
len = 1.f / len;
vec3_scale(v, len);
}
void vec3_add(vec3_t *out, const vec3_t *a, const vec3_t *b)
{
out->x = a->x + b->x;
out->y = a->y + b->y;
out->z = a->z + b->z;
}
void vec3_sub(vec3_t *out, const vec3_t *a, const vec3_t *b)
{
out->x = a->x - b->x;
out->y = a->y - b->y;
out->z = a->z - b->z;
}
void vec3_abs(vec3_t *v)
{
v->x = fabsf(v->x);
v->y = fabsf(v->y);
v->z = fabsf(v->z);
}
void vec3_min(vec3_t *out, const vec3_t *a, const vec3_t *b)
{
out->x = a->x < b->x ? a->x : b->x;
out->y = a->y < b->y ? a->y : b->y;
out->z = a->z < b->z ? a->z : b->z;
}
void vec3_max(vec3_t *out, const vec3_t *a, const vec3_t *b)
{
out->x = a->x > b->x ? a->x : b->x;
out->y = a->y > b->y ? a->y : b->y;
out->z = a->z > b->z ? a->z : b->z;
}
void vec3_majoraxis(vec3_t *out, const vec3_t *v)
{
float x, y, z;
x = fabsf(v->x);
y = fabsf(v->y);
z = fabsf(v->z);
if (x > y && x > z)
*out = VEC3(v->x < 0 ? -1 : 1, 0, 0);
else if (y > x && y > z)
*out = VEC3(0, v->y < 0 ? -1 : 1, 0);
else
*out = VEC3(0, 0, v->z < 0 ? -1 : 1);
}
void vec3_rotate(vec3_t *out, const vec3_t *v, float angle, const vec3_t *axis)
{
quat_t q;
quat_axis_angle(&q, axis, angle);
quat_rotate(out, &q, v);
}
/* epsilon? */
int vec3_equal(const vec3_t *a, const vec3_t *b)
{
return a->x == b->x && a->y == b->y && a->z == b->z;
}
void quat_axis_angle(quat_t *q, const vec3_t *v, float angle)
{
float s, c;
sincosf(angle/2, &s, &c);
q->w = c;
q->v = *v;
vec3_normalize(&q->v);
vec3_scale(&q->v, s);
}
void quat_vector_vector(quat_t *q, const vec3_t *a, const vec3_t *b)
{
float cost = vec3_dot(a, b);
if (cost > 0.99999f) {
/* parallel */
*q = QUAT_IDENT;
} else if (cost < -0.99999f) {
/* opposite */
vec3_t t = VEC3(0, a->x, -a->y); /* cross with (1,0,0) */
if (vec3_magnitude(&t) < EPSILON)
t = VEC3(-a->z, 0, a->x); /* nope, use (0,1,0) */
vec3_normalize(&t);
q->v = t;
q->w = 0.f;
} else {
vec3_t t;
vec3_cross(&t, a, b);
vec3_normalize(&t);
/* sin^2 t = (1 - cos(2t)) / 2 */
float ss = sqrt(.5f * (1.f - cost));
vec3_scale(&t, ss);
q->v = t;
/* cos^2 t = (1 + cos(2t) / 2 */
q->w = sqrt(.5f * (1.f + cost));
}
}
void quat_mult(quat_t *r, const quat_t *a, const quat_t *b)
{
quat_t ret;
vec3_t t;
ret.w = a->w * b->w - vec3_dot(&a->v, &b->v);
vec3_cross(&ret.v, &a->v, &b->v);
t = a->v;
vec3_scale(&t, b->w);
vec3_add(&ret.v, &ret.v, &t);
t = b->v;
vec3_scale(&t, a->w);
vec3_add(&ret.v, &ret.v, &t);
*r = ret;
}
void quat_normalize(quat_t *q)
{
float m = q->w * q->w + vec3_dot(&q->v, &q->v);
m = 1.f / m;
q->w *= m;
vec3_scale(&q->v, m);
}
void quat_conj(quat_t *out, const quat_t *in)
{
out->v.x = -in->v.x;
out->v.y = -in->v.y;
out->v.z = -in->v.z;
out->w = in->w;
}
void quat_invert(quat_t *out, const quat_t *in)
{
float m = 1.f / (in->w * in->w + vec3_dot(&in->v, &in->v));
out->v.x = -in->v.x * m;
out->v.y = -in->v.y * m;
out->v.z = -in->v.z * m;
out->w = in->w * m;
}
void quat_rotate(vec3_t *res, const quat_t *q, const vec3_t *v)
{
quat_t tq, itq, tv;
quat_conj(&itq, q);
tv = QUAT(0, *v);
quat_mult(&tq, q, &tv);
quat_mult(&tq, &tq, &itq);
*res = tq.v;
}
void matrix_quat(matrix_t *m, const quat_t *q)
{
float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
x2 = q->v.x + q->v.x; y2 = q->v.y + q->v.y; z2 = q->v.z + q->v.z;
xx = q->v.x * x2; xy = q->v.x * y2; xz = q->v.x * z2;
yy = q->v.y * y2; yz = q->v.y * z2; zz = q->v.z * z2;
wx = q->w * x2; wy = q->w * y2; wz = q->w * z2;
m->_11 = 1.0 - (yy + zz);
m->_21 = xy - wz;
m->_31 = xz + wy;
m->_41 = 0.0;
m->_12 = xy + wz;
m->_22 = 1.0 - (xx + zz);
m->_32 = yz - wx;
m->_42 = 0.0;
m->_31 = xz - wy;
m->_32 = yz + wx;
m->_33 = 1.0 - (xx + yy);
m->_34 = 0.0;
m->_41 = 0;
m->_42 = 0;
m->_43 = 0;
m->_44 = 1;
}
void matrix_transform(const matrix_t *m, const vec3_t *in, vec3_t *out)
{
vec3_t t;
t.x = m->m[0] * in->x + m->m[4] * in->y + m->m[ 8] * in->z + m->m[12];
t.y = m->m[1] * in->x + m->m[5] * in->y + m->m[ 9] * in->z + m->m[13];
t.z = m->m[2] * in->x + m->m[6] * in->y + m->m[10] * in->z + m->m[14];
*out = t;
}
void matrix_project(const matrix_t *m, const vec3_t *in, vec3_t *out)
{
vec3_t t;
float w;
t.x = m->m[0] * in->x + m->m[4] * in->y + m->m[ 8] * in->z + m->m[12];
t.y = m->m[1] * in->x + m->m[5] * in->y + m->m[ 9] * in->z + m->m[13];
t.z = m->m[2] * in->x + m->m[6] * in->y + m->m[10] * in->z + m->m[14];
w = m->m[3] * in->x + m->m[7] * in->y + m->m[11] * in->z + m->m[15];
if (w != 0.f)
vec3_scale(&t, 1.f/w);
*out = t;
}
void matrix_multiply(const matrix_t *a, const matrix_t *b, matrix_t *out)
{
#define A(r,c) a->m[4*c+r]
#define B(r,c) b->m[4*c+r]
#define P(r,c) out->m[4*c+r]
for(int i = 0; i < 4; i++) {
const float ai0 = A(i, 0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
}
#undef A
#undef B
#undef P
}
void plane_normalize(plane_t *p)
{
float len = 1.f / vec3_magnitude(&p->normal);
vec3_scale(&p->normal, len);
p->dist *= len;
}
/*
Taken from "Fast Extraction of Viewing Frustum Planes from the
World- View-Projection Matrix" Gil Gribb, Klaus Hartmann. Planes
will be in whatever space your matrix transforms from.
*/
void plane_extract(const matrix_t *mat, plane_t planes[6])
{
// Left clipping plane
planes[PLANE_LEFT].normal.x = mat->_41 + mat->_11;
planes[PLANE_LEFT].normal.y = mat->_42 + mat->_12;
planes[PLANE_LEFT].normal.z = mat->_43 + mat->_13;
planes[PLANE_LEFT].dist = mat->_44 + mat->_14;
// Right clipping plane
planes[PLANE_RIGHT].normal.x = mat->_41 - mat->_11;
planes[PLANE_RIGHT].normal.y = mat->_42 - mat->_12;
planes[PLANE_RIGHT].normal.z = mat->_43 - mat->_13;
planes[PLANE_RIGHT].dist = mat->_44 - mat->_14;
// Top clipping plane
planes[PLANE_TOP].normal.x = mat->_41 - mat->_21;
planes[PLANE_TOP].normal.y = mat->_42 - mat->_22;
planes[PLANE_TOP].normal.z = mat->_43 - mat->_23;
planes[PLANE_TOP].dist = mat->_44 - mat->_24;
// Bottom clipping plane
planes[PLANE_BOTTOM].normal.x = mat->_41 + mat->_21;
planes[PLANE_BOTTOM].normal.y = mat->_42 + mat->_22;
planes[PLANE_BOTTOM].normal.z = mat->_43 + mat->_23;
planes[PLANE_BOTTOM].dist = mat->_44 + mat->_24;
// Near clipping plane
planes[PLANE_NEAR].normal.x = mat->_41 + mat->_31;
planes[PLANE_NEAR].normal.y = mat->_42 + mat->_32;
planes[PLANE_NEAR].normal.z = mat->_43 + mat->_33;
planes[PLANE_NEAR].dist = mat->_44 + mat->_34;
// Far clipping plane
planes[PLANE_FAR].normal.x = mat->_41 - mat->_31;
planes[PLANE_FAR].normal.y = mat->_42 - mat->_32;
planes[PLANE_FAR].normal.z = mat->_43 - mat->_33;
planes[PLANE_FAR].dist = mat->_44 - mat->_34;
}
enum cull_result box_cull(const box_t *box, const plane_t *planes, int nplanes)
{
int visible = 0;
for(int i = 0; i < nplanes; i++) {
const plane_t *p = &planes[i];
float reff =
fabsf(box->extent.x * p->normal.x) +
fabsf(box->extent.y * p->normal.y) +
fabsf(box->extent.z * p->normal.z);
float dot = vec3_dot(&p->normal, &box->centre) + p->dist;
if (dot <= -reff)
return CULL_OUT;
else {
if (++visible == nplanes)
return CULL_IN;
}
}
return CULL_PARTIAL;
}