-
Notifications
You must be signed in to change notification settings - Fork 4
/
mathx.c
187 lines (157 loc) · 5.43 KB
/
mathx.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
#include "mathx.h"
#define det2(a00,a01,a10,a11) ((a00)*(a11) - (a01)*(a10))
#define det3(a00,a01,a02,a10,a11,a12,a20,a21,a22) ((a00)*det2(a11,a12,a21,a22)- \
(a10)*det2(a01,a02,a21,a22)+ \
(a20)*det2(a01,a02,a11,a12))
#define M(m,i,j) (m[i+4*j])
#define A(i,j) M(a,i,j)
#define B(i,j) M(b,i,j)
#define C(i,j) M(c,i,j)
#define R(i,j) M(r,i,j)
float mat_determinant(const matrix a)
{
return A(0,0)*det3(A(1,1),A(1,2),A(1,3),A(2,1),A(2,2),A(2,3),A(3,1),A(3,2),A(3,3))-
A(0,1)*det3(A(1,0),A(1,2),A(1,3),A(2,0),A(2,2),A(2,3),A(3,0),A(3,2),A(3,3))+
A(0,2)*det3(A(1,0),A(1,1),A(1,3),A(2,0),A(2,1),A(2,3),A(3,0),A(3,1),A(3,3))-
A(0,3)*det3(A(1,0),A(1,1),A(1,2),A(2,0),A(2,1),A(2,2),A(3,0),A(3,1),A(3,2));
}
void mat_adjoint(matrix r, const matrix a, float *det)
{
R(0,0) = det3(A(1,1),A(1,2),A(1,3),A(2,1),A(2,2),A(2,3),A(3,1),A(3,2),A(3,3));
R(0,1) = -det3(A(0,1),A(0,2),A(0,3),A(2,1),A(2,2),A(2,3),A(3,1),A(3,2),A(3,3));
R(0,2) = det3(A(0,1),A(0,2),A(0,3),A(1,1),A(1,2),A(1,3),A(3,1),A(3,2),A(3,3));
R(0,3) = -det3(A(0,1),A(0,2),A(0,3),A(1,1),A(1,2),A(1,3),A(2,1),A(2,2),A(2,3));
R(1,0) = -det3(A(1,0),A(1,2),A(1,3),A(2,0),A(2,2),A(2,3),A(3,0),A(3,2),A(3,3));
R(1,1) = det3(A(0,0),A(0,2),A(0,3),A(2,0),A(2,2),A(2,3),A(3,0),A(3,2),A(3,3));
R(1,2) = -det3(A(0,0),A(1,0),A(3,0),A(0,2),A(1,2),A(3,2),A(0,3),A(1,3),A(3,3));
R(1,3) = det3(A(0,0),A(0,2),A(0,3),A(1,0),A(1,2),A(1,3),A(2,0),A(2,2),A(2,3));
R(2,0) = det3(A(1,0),A(1,1),A(1,3),A(2,0),A(2,1),A(2,3),A(3,0),A(3,1),A(3,3));
R(2,1) = -det3(A(0,0),A(0,1),A(0,3),A(2,0),A(2,1),A(2,3),A(3,0),A(3,1),A(3,3));
R(2,2) = det3(A(0,0),A(0,1),A(0,3),A(1,0),A(1,1),A(1,3),A(3,0),A(3,1),A(3,3));
R(2,3) = -det3(A(0,0),A(0,1),A(0,3),A(1,0),A(1,1),A(1,3),A(2,0),A(2,1),A(2,3));
R(3,0) = -det3(A(1,0),A(1,1),A(1,2),A(2,0),A(2,1),A(2,2),A(3,0),A(3,1),A(3,2));
R(3,1) = det3(A(0,0),A(0,1),A(0,2),A(2,0),A(2,1),A(2,2),A(3,0),A(3,1),A(3,2));
R(3,2) = -det3(A(0,0),A(0,1),A(0,2),A(1,0),A(1,1),A(1,2),A(3,0),A(3,1),A(3,2));
R(3,3) = det3(A(0,0),A(0,1),A(0,2),A(1,0),A(1,1),A(1,2),A(2,0),A(2,1),A(2,2));
if (det)
*det = A(0,0) * R(0,0) + A(0,1) * R(1,0) +
A(0,2) * R(2,0) + A(0,3) * R(3,0);
}
void mat_invert(matrix r, const matrix a)
{
int i, j;
float det, inv_det;
mat_adjoint(r, a, &det);
inv_det = 1.0f / det;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
R(i,j) *= inv_det;
}
void mat_mul(matrix r, const matrix a, const matrix b)
{
int i, j, k;
matrix c;
mat_zero(c);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
for (k = 0; k < 4; k++)
C(i,j) += A(i,k) * B(k,j);
mat_copy(r, c);
}
void mat_mul_point(vector r, const matrix a, const vector v)
{
vector res;
res[0] = A(0,0) * v[0] + A(0,1) * v[1] + A(0,2) * v[2] + A(0,3);
res[1] = A(1,0) * v[0] + A(1,1) * v[1] + A(1,2) * v[2] + A(1,3);
res[2] = A(2,0) * v[0] + A(2,1) * v[1] + A(2,2) * v[2] + A(2,3);
vec_copy(r, res);
}
void mat_mul_vector(vector r, const matrix a, const vector v)
{
vector res;
res[0] = A(0,0) * v[0] + A(0,1) * v[1] + A(0,2) * v[2];
res[1] = A(1,0) * v[0] + A(1,1) * v[1] + A(1,2) * v[2];
res[2] = A(2,0) * v[0] + A(2,1) * v[1] + A(2,2) * v[2];
vec_copy(r, res);
}
void mat_scale(matrix r, float s)
{
mat_identity(r);
R(0,0) = R(1,1) = R(2,2) = s;
}
void mat_translate(matrix r, const vector v)
{
mat_identity(r);
R(0,3) = v[0];
R(1,3) = v[1];
R(2,3) = v[2];
}
void mat_rotate(matrix r, const vector axis, float angle)
{
vector a;
float c, s;
vec_normalize(a, axis);
c = cosf(radians(angle));
s = sinf(radians(angle));
mat_identity(r);
R(0,0) = a[0] * a[0] * (1.0f - c) + c;
R(0,1) = a[0] * a[1] * (1.0f - c) - a[2] * s;
R(0,2) = a[0] * a[2] * (1.0f - c) + a[1] * s;
R(1,0) = a[0] * a[1] * (1.0f - c) + a[2] * s;
R(1,1) = a[1] * a[1] * (1.0f - c) + c;
R(1,2) = a[1] * a[2] * (1.0f - c) - a[0] * s;
R(2,0) = a[0] * a[2] * (1.0f - c) - a[1] * s;
R(2,1) = a[1] * a[2] * (1.0f - c) + a[0] * s;
R(2,2) = a[2] * a[2] * (1.0f - c) + c;
}
void mat_lookat(matrix r, const vector eye, const vector at, const vector up)
{
vector x, y, z;
vec_sub(z, eye, at);
vec_normalize(z, z);
vec_cross(x, up, z);
vec_normalize(x, x);
vec_cross(y, z, x);
mat_frame(r, eye, x, y, z);
}
void mat_frame(matrix r, const vector o, const vector x, const vector y, const vector z)
{
vector p;
float a, b, c;
vec_neg(p, o);
a = vec_dot(p, x);
b = vec_dot(p, y);
c = vec_dot(p, z);
R(0,0) = x[0]; R(0,1) = x[1]; R(0,2) = x[2]; R(0, 3) = a;
R(1,0) = y[0]; R(1,1) = y[1]; R(1,2) = y[2]; R(1, 3) = b;
R(2,0) = z[0]; R(2,1) = z[1]; R(2,2) = z[2]; R(2, 3) = c;
R(3,0) = 0.0f; R(3,1) = 0.0f; R(3,2) = 0.0f; R(3, 3) = 1.0f;
}
void mat_frustum(matrix r, float left, float right, float bot, float top, float near, float far)
{
mat_zero(r);
R(0,0) = 2 * near / (right - left);
R(0,2) = (right + left) / (right - left);
R(1,1) = 2 * near / (top - bot);
R(1,2) = (top + bot) / (top - bot);
R(2,2) = -(far + near) / (far - near);
R(2,3) = -(2 * far * near) / (far - near);
R(3,2) = -1;
}
void mat_ortho(matrix r, float left, float right, float bot, float top, float near, float far)
{
mat_identity(r);
R(0,0) = 2 / (right - left);
R(0,3) = -(right + left) / (right - left);
R(1,1) = 2 / (top - bot);
R(1,3) = -(top + bot) / (top - bot);
R(2,2) = -2 / (far - near);
R(2,3) = -(far + near) / (far - near);
}
void mat_persp(matrix r, float fovy, float aspect, float near, float far)
{
float top, right;
top = near * tanf(radians(fovy) / 2.0f);
right = aspect * top ;
mat_frustum(r, -right, right, -top, top, near, far);
}