aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/src/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/src/matrix.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/src/matrix.c263
1 files changed, 263 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/src/matrix.c b/src/others/mimesh/libg3d-0.0.8/src/matrix.c
new file mode 100644
index 0000000..b2b8815
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/src/matrix.c
@@ -0,0 +1,263 @@
1/* $Id$ */
2
3/*
4 libg3d - 3D object loading library
5
6 Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21*/
22
23#include <string.h>
24#include <math.h>
25#include <glib.h>
26#include <g3d/vector.h>
27
28EAPI
29gboolean g3d_matrix_identity(G3DMatrix *matrix)
30{
31 static G3DMatrix identity[16] = {
32 1.0, 0.0, 0.0, 0.0,
33 0.0, 1.0, 0.0, 0.0,
34 0.0, 0.0, 1.0, 0.0,
35 0.0, 0.0, 0.0, 1.0 };
36
37 memcpy(matrix, identity, sizeof(G3DMatrix) * 16);
38 return TRUE;
39}
40
41EAPI
42gboolean g3d_matrix_multiply(G3DMatrix *m1, G3DMatrix *m2, G3DMatrix *rm)
43{
44 guint32 i, j;
45 G3DMatrix matrix[16];
46
47 for(i = 0; i < 4; i ++)
48 for(j = 0; j < 4; j ++)
49#if 0
50 matrix[i * 4 + j] =
51 m2[0 * 4 + j] * m1[i * 4 + 0] +
52 m2[1 * 4 + j] * m1[i * 4 + 1] +
53 m2[2 * 4 + j] * m1[i * 4 + 2] +
54 m2[3 * 4 + j] * m1[i * 4 + 3];
55#else
56 matrix[j * 4 + i] =
57 m2[j * 4 + 0] * m1[0 * 4 + i] +
58 m2[j * 4 + 1] * m1[1 * 4 + i] +
59 m2[j * 4 + 2] * m1[2 * 4 + i] +
60 m2[j * 4 + 3] * m1[3 * 4 + i];
61#endif
62
63 memcpy(rm, matrix, 16 * sizeof(G3DMatrix));
64 return TRUE;
65}
66
67EAPI
68gboolean g3d_matrix_rotate(G3DFloat angle, G3DVector ax, G3DVector ay,
69 G3DVector az, G3DMatrix *rm)
70{
71 g3d_vector_unify(&ax, &ay, &az);
72 g3d_matrix_identity(rm);
73
74#if 0
75 rm[0 * 4 + 0] = cos(angle) + (ax * ax) * (1 - cos(angle));
76 rm[0 * 4 + 1] = ax * ay * (1 - cos(angle)) - az * sin(angle);
77 rm[0 * 4 + 2] = ax * az * (1 - cos(angle)) + ay * sin(angle);
78
79 rm[1 * 4 + 0] = ay * ax * (1 - cos(angle)) + az * sin(angle);
80 rm[1 * 4 + 1] = cos(angle) + (ay * ay) * (1 - cos(angle));
81 rm[1 * 4 + 2] = ay * az * (1 - cos(angle)) - ax * sin(angle);
82
83 rm[2 * 4 + 0] = az * ax * (1 - cos(angle)) - ay * sin(angle);
84 rm[2 * 4 + 1] = az * ay * (1 - cos(angle)) + ax * sin(angle);
85 rm[2 * 4 + 2] = cos(angle) + (az * az) * (1 - cos(angle));
86#else
87 rm[0 * 4 + 0] = cos(angle) + (ax * ax) * (1 - cos(angle));
88 rm[1 * 4 + 0] = ax * ay * (1 - cos(angle)) - az * sin(angle);
89 rm[2 * 4 + 0] = ax * az * (1 - cos(angle)) + ay * sin(angle);
90
91 rm[0 * 4 + 1] = ay * ax * (1 - cos(angle)) + az * sin(angle);
92 rm[1 * 4 + 1] = cos(angle) + (ay * ay) * (1 - cos(angle));
93 rm[2 * 4 + 1] = ay * az * (1 - cos(angle)) - ax * sin(angle);
94
95 rm[0 * 4 + 2] = az * ax * (1 - cos(angle)) - ay * sin(angle);
96 rm[1 * 4 + 2] = az * ay * (1 - cos(angle)) + ax * sin(angle);
97 rm[2 * 4 + 2] = cos(angle) + (az * az) * (1 - cos(angle));
98#endif
99
100 return TRUE;
101}
102
103EAPI
104gboolean g3d_matrix_rotate_xyz(G3DFloat rx, G3DFloat ry, G3DFloat rz,
105 G3DMatrix *rm)
106{
107 G3DMatrix matrix[16];
108
109 g3d_matrix_identity(rm);
110
111 g3d_matrix_rotate(rx, 1.0, 0.0, 0.0, matrix);
112 g3d_matrix_multiply(matrix, rm, rm);
113
114 g3d_matrix_rotate(ry, 0.0, 1.0, 0.0, matrix);
115 g3d_matrix_multiply(matrix, rm, rm);
116
117 g3d_matrix_rotate(rz, 0.0, 0.0, 1.0, matrix);
118 g3d_matrix_multiply(matrix, rm, rm);
119
120 return TRUE;
121}
122
123EAPI
124gboolean g3d_matrix_translate(G3DVector x, G3DVector y, G3DVector z,
125 G3DMatrix *rm)
126{
127 guint32 i;
128
129#if 0
130 for(i = 0; i < 4; i ++)
131 rm[i * 4 + 3] =
132 rm[i * 4 + 0] * x +
133 rm[i * 4 + 1] * y +
134 rm[i * 4 + 2] * z +
135 rm[i * 4 + 3];
136#else
137 for(i = 0; i < 4; i ++)
138 rm[3 * 4 + i] =
139 rm[0 * 4 + i] * x +
140 rm[1 * 4 + i] * y +
141 rm[2 * 4 + i] * z +
142 rm[3 * 4 + i];
143#endif
144 return TRUE;
145}
146
147EAPI
148gboolean g3d_matrix_scale(G3DVector x, G3DVector y, G3DVector z, G3DMatrix *rm)
149{
150 G3DMatrix sm[16];
151
152 g3d_matrix_identity(sm);
153 sm[0] = x;
154 sm[5] = y;
155 sm[10] = z;
156
157 g3d_matrix_multiply(rm, sm, rm);
158
159 return TRUE;
160}
161
162EAPI
163gboolean g3d_matrix_transpose(G3DMatrix *matrix)
164{
165 G3DMatrix tmp[16];
166 gint32 i, j;
167
168 memcpy(tmp, matrix, 16 * sizeof(G3DMatrix));
169
170 for(i = 0; i < 4; i ++)
171 for(j = 0; j < 4; j ++)
172 matrix[i * 4 + j] = tmp[j * 4 + i];
173
174 return TRUE;
175}
176
177static G3DFloat det2x2(G3DFloat a1, G3DFloat a2, G3DFloat b1, G3DFloat b2)
178{
179 return a1 * b2 - a2 * b1;
180}
181
182static G3DFloat det3x3(G3DFloat a1, G3DFloat a2, G3DFloat a3,
183 G3DFloat b1, G3DFloat b2, G3DFloat b3,
184 G3DFloat c1, G3DFloat c2, G3DFloat c3)
185{
186 return
187 a1 * det2x2(b2, b3, c2, c3) -
188 b1 * det2x2(a2, a3, c2, c3) +
189 c1 * det2x2(a2, a3, b2, b3);
190}
191
192EAPI
193G3DFloat g3d_matrix_determinant(G3DMatrix *matrix)
194{
195 G3DFloat a1, a2, a3, a4;
196 G3DFloat b1, b2, b3, b4;
197 G3DFloat c1, c2, c3, c4;
198 G3DFloat d1, d2, d3, d4;
199#if 0
200 a1 = matrix[0 * 4 + 0];
201 a2 = matrix[0 * 4 + 1];
202 a3 = matrix[0 * 4 + 2];
203 a4 = matrix[0 * 4 + 3];
204
205 b1 = matrix[1 * 4 + 0];
206 b2 = matrix[1 * 4 + 1];
207 b3 = matrix[1 * 4 + 2];
208 b4 = matrix[1 * 4 + 3];
209
210 c1 = matrix[2 * 4 + 0];
211 c2 = matrix[2 * 4 + 1];
212 c3 = matrix[2 * 4 + 2];
213 c4 = matrix[2 * 4 + 3];
214
215 d1 = matrix[3 * 4 + 0];
216 d2 = matrix[3 * 4 + 1];
217 d3 = matrix[3 * 4 + 2];
218 d4 = matrix[3 * 4 + 3];
219#else
220 a1 = matrix[0 * 4 + 0];
221 b1 = matrix[0 * 4 + 1];
222 c1 = matrix[0 * 4 + 2];
223 d1 = matrix[0 * 4 + 3];
224
225 a2 = matrix[1 * 4 + 0];
226 b2 = matrix[1 * 4 + 1];
227 c2 = matrix[1 * 4 + 2];
228 d2 = matrix[1 * 4 + 3];
229
230 a3 = matrix[2 * 4 + 0];
231 b3 = matrix[2 * 4 + 1];
232 c3 = matrix[2 * 4 + 2];
233 d3 = matrix[2 * 4 + 3];
234
235 a4 = matrix[3 * 4 + 0];
236 b4 = matrix[3 * 4 + 1];
237 c4 = matrix[3 * 4 + 2];
238 d4 = matrix[3 * 4 + 3];
239#endif
240 return
241 a1 * det3x3(b2, b3, b4, c2, c3, c4, d2, d3, d4) -
242 b1 * det3x3(a2, a3, a4, c2, c3, c4, d2, d3, d4) +
243 c1 * det3x3(a2, a3, a4, b2, b3, b4, d2, d3, d4) -
244 d1 * det3x3(a2, a3, a4, b2, b3, b4, c2, c3, c4);
245}
246
247gboolean g3d_matrix_dump(G3DMatrix *matrix)
248{
249#if DEBUG > 0
250 gint32 row;
251
252 for(row = 0; row < 4; row ++)
253 {
254 g_debug("[Matrix] % 2.2f % 2.2f % 2.2f % 2.2f",
255 matrix[0 * 4 + row], matrix[1 * 4 + row],
256 matrix[2 * 4 + row], matrix[3 * 4 + row]);
257 }
258
259 return TRUE;
260#else
261 return FALSE;
262#endif
263}