GoldFish Engine
Quite simple and lightweight game engine
Loading...
Searching...
No Matches
gf_math.c
1
#include <
gf_pre.h
>
2
3
/* External library */
4
5
/* Interface */
6
#include <
gf_math.h
>
7
8
/* Engine */
9
10
/* Standard */
11
#include <string.h>
12
#include <stdlib.h>
13
#include <
math.h
>
14
15
double
gf_math_log2(
double
x) {
return
log(x) / log(2); }
16
17
void
gf_math_normalize(
gf_math_vector_t
v) {
18
double
x = v[0];
19
double
y = v[1];
20
double
z = v[2];
21
double
length;
22
23
x *= x;
24
y *= y;
25
z *= z;
26
27
length = sqrt(x + y + z);
28
if
(length > 0) {
29
length = (double)1 / length;
30
}
else
{
31
length = 0;
32
}
33
v[0] *= length;
34
v[1] *= length;
35
v[2] *= length;
36
}
37
38
void
gf_math_normal(
gf_math_vector_t
r,
gf_math_vector_t
v0,
gf_math_vector_t
v1,
gf_math_vector_t
v2) {
39
gf_math_vector_t
tmp0;
40
gf_math_vector_t
tmp1;
41
42
gf_math_subtract(tmp0, v1, v0);
43
gf_math_subtract(tmp1, v2, v0);
44
45
gf_math_multiply(r, tmp0, tmp1);
46
47
gf_math_normalize(r);
48
}
49
50
void
gf_math_subtract(
gf_math_vector_t
r,
gf_math_vector_t
v0,
gf_math_vector_t
v1) {
51
r[0] = v0[0] - v1[0];
52
r[1] = v0[1] - v1[1];
53
r[2] = v0[2] - v1[2];
54
}
55
56
void
gf_math_multiply(
gf_math_vector_t
r,
gf_math_vector_t
v0,
gf_math_vector_t
v1) {
57
r[0] = v0[1] * v1[2] - v0[2] * v1[1];
58
r[1] = v0[2] * v1[0] - v0[0] * v1[2];
59
r[2] = v0[0] * v1[1] - v0[1] * v1[0];
60
}
61
62
double
gf_math_cot(
double
x) {
return
(
double
)1 / tan(x); }
63
64
double
gf_math_nearest_2pow(
double
x) {
65
double
l = gf_math_log2(x);
66
double
r = pow(2, (
int
)l);
67
if
(x == r) {
68
return
r;
69
}
70
return
pow(2, 1 + (
int
)l);
71
}
gf_math.h
Math.
gf_pre.h
Required headers before anything.
math.h
Type definitions related to math.
gf_math_vector_t
double gf_math_vector_t[4]
Vector.
Definition
math.h:25
gf_math.c
Generated by
1.9.8