|
@@ -1,690 +0,0 @@
|
1
|
|
-/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
|
2
|
|
-/*===============================================================
|
3
|
|
- * Project: Mongoose Tool Kit
|
4
|
|
- * Author : Terry 'Mongoose' Hendrix
|
5
|
|
- * Website: http://www.westga.edu/~stu7440/
|
6
|
|
- * Email : stu7440@westga.edu
|
7
|
|
- * Object :
|
8
|
|
- * Comment: 3d math util functions.
|
9
|
|
- *
|
10
|
|
- * FIXME: OpenGL matrices are col major OOPS!
|
11
|
|
- * some are using col some aren't
|
12
|
|
- *
|
13
|
|
- *-- History -----------------------------------------------
|
14
|
|
- *
|
15
|
|
- * 2001.11.23:
|
16
|
|
- * Mongoose - Move to OpenGL compatible matrices
|
17
|
|
- *
|
18
|
|
- * 2000-03-25:
|
19
|
|
- * Mongoose - Recreated from GooseEgg prototyping
|
20
|
|
- *
|
21
|
|
- * 1999-06-14:
|
22
|
|
- * Mongoose - Created
|
23
|
|
- ==============================================================*/
|
24
|
|
-
|
25
|
|
-#include <math.h>
|
26
|
|
-#include <stdio.h>
|
27
|
|
-
|
28
|
|
-#include "mtk3d.h"
|
29
|
|
-
|
30
|
|
-
|
31
|
|
-/*
|
32
|
|
-double mtk3d_norm3d(double u[3])
|
33
|
|
-{
|
34
|
|
- return(sqrt(u[0]*u[0]+u[1]*u[1]+u[2]*u[2]));
|
35
|
|
-}
|
36
|
|
-
|
37
|
|
-double mtk3d_norm2d(double u[2])
|
38
|
|
-{
|
39
|
|
- return(sqrt(u[0]*u[0]+u[1]*u[1]));
|
40
|
|
-}
|
41
|
|
-*/
|
42
|
|
-
|
43
|
|
-vec_t mtkRandNum(vec_t from, vec_t to)
|
44
|
|
-{
|
45
|
|
- return from + (to*rand()/(RAND_MAX+1.0));
|
46
|
|
-}
|
47
|
|
-
|
48
|
|
-
|
49
|
|
-// Heh, this is scary... try to avoid many indexing computations
|
50
|
|
-void mtkVectorMatrixMult4dv(double v[4], matrix_t m, double result[4])
|
51
|
|
-{
|
52
|
|
-#ifdef OBSOLETE
|
53
|
|
- int i, j;
|
54
|
|
-
|
55
|
|
- for (i = 0; i < 4; i++)
|
56
|
|
- {
|
57
|
|
- result[i] = 0;
|
58
|
|
-
|
59
|
|
- for (j = 0; j < 4; j++)
|
60
|
|
- {
|
61
|
|
- result[i] += m[i][j] * v[j];
|
62
|
|
- }
|
63
|
|
- }
|
64
|
|
-#else
|
65
|
|
- /////////////////////////////////////////////
|
66
|
|
- // 0,0 - 0; 0,1 - 1; 0,2 - 2; 0,3 - 3
|
67
|
|
- // 1,0 - 4; 1,1 - 5; 1,2 - 6; 1,3 - 7
|
68
|
|
- // 2,0 - 8; 2,1 - 9; 2,2 - 10; 2,3 - 11
|
69
|
|
- // 3,0 - 12; 3,1 - 13; 3,2 - 14; 3,3 - 15
|
70
|
|
- /////////////////////////////////////////////
|
71
|
|
-
|
72
|
|
-
|
73
|
|
- // Col major?
|
74
|
|
- //result[0] = m[ 0] * v[0] + m[ 4] * v[1] + m[ 8] * v[2] + m[12] * v[3];
|
75
|
|
- //result[1] = m[ 1] * v[0] + m[ 5] * v[1] + m[ 9] * v[2] + m[13] * v[3];
|
76
|
|
- //result[2] = m[ 2] * v[0] + m[ 6] * v[1] + m[10] * v[2] + m[14] * v[3];
|
77
|
|
-
|
78
|
|
- result[0] = m[ 0] * v[0] + m[ 1] * v[1] + m[ 2] * v[2] + m[ 3] * v[3];
|
79
|
|
- result[1] = m[ 4] * v[0] + m[ 5] * v[1] + m[ 6] * v[2] + m[ 7] * v[3];
|
80
|
|
- result[2] = m[ 8] * v[0] + m[ 9] * v[1] + m[10] * v[2] + m[11] * v[3];
|
81
|
|
- result[3] = m[12] * v[0] + m[13] * v[1] + m[14] * v[2] + m[15] * v[3];
|
82
|
|
-#endif
|
83
|
|
-}
|
84
|
|
-
|
85
|
|
-
|
86
|
|
-void mtkMatrixPrint(matrix_t matrix)
|
87
|
|
-{
|
88
|
|
-#ifdef OBSOLETE
|
89
|
|
- printf("matrix = {\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n}\n",
|
90
|
|
- matrix[0][0], matrix[0][1], matrix[0][2], matrix[0][3],
|
91
|
|
- matrix[1][0], matrix[1][1], matrix[1][2], matrix[1][3],
|
92
|
|
- matrix[2][0], matrix[2][1], matrix[2][2], matrix[2][3],
|
93
|
|
- matrix[3][0], matrix[3][1], matrix[3][2], matrix[3][3]);
|
94
|
|
-#else
|
95
|
|
- printf("matrix = {\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n}\n",
|
96
|
|
- matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
|
97
|
|
- matrix[ 4], matrix[ 5], matrix[ 6], matrix[ 7],
|
98
|
|
- matrix[ 8], matrix[ 9], matrix[10], matrix[11],
|
99
|
|
- matrix[12], matrix[13], matrix[14], matrix[15]);
|
100
|
|
-#endif
|
101
|
|
-}
|
102
|
|
-
|
103
|
|
-
|
104
|
|
-vec_t mtkDist2d(vec_t x, vec_t y, vec_t x2, vec_t y2)
|
105
|
|
-{
|
106
|
|
- return (sqrt( ((x2 - x)*(x2 - x)) + ((y2 - y)*(y2 - y)) ));
|
107
|
|
-}
|
108
|
|
-
|
109
|
|
-
|
110
|
|
-vec_t mtkDist3d(vec_t x, vec_t y, vec_t z,
|
111
|
|
- vec_t x2, vec_t y2, vec_t z2)
|
112
|
|
-{
|
113
|
|
- return (sqrt( ((x2 - x)*(x2 - x))+((y2 - y)*(y2 - y))+((z2 - z)*(z2 - z))));
|
114
|
|
-}
|
115
|
|
-
|
116
|
|
-vec_t mtkDegToRad(vec_t degrees)
|
117
|
|
-{
|
118
|
|
- return ((degrees / 180.0) * MTK_PI); // degrees * (180.0 / MTK_PI);
|
119
|
|
-}
|
120
|
|
-
|
121
|
|
-vec_t mtkRadToDeg(vec_t rad)
|
122
|
|
-{
|
123
|
|
- return ((rad / MTK_PI) * 180.0); // rad * (MTK_PI / 180.0);
|
124
|
|
-}
|
125
|
|
-
|
126
|
|
-
|
127
|
|
-void mtkVectorMatrixMult(vec3_t v, matrix_t m, vec3_t result)
|
128
|
|
-{
|
129
|
|
- result[0] = m[ 0] * v[0] + m[ 1] * v[1] + m[ 2] * v[2] + m[ 3] * v[3];
|
130
|
|
- result[1] = m[ 4] * v[0] + m[ 5] * v[1] + m[ 6] * v[2] + m[ 7] * v[3];
|
131
|
|
- result[2] = m[ 8] * v[0] + m[ 9] * v[1] + m[10] * v[2] + m[11] * v[3];
|
132
|
|
-}
|
133
|
|
-
|
134
|
|
-
|
135
|
|
-// Vector from two points
|
136
|
|
-void mtkVectorSubtract(vec3_t a, vec3_t b, vec3_t result)
|
137
|
|
-{
|
138
|
|
- result[0] = a[0] - b[0];
|
139
|
|
- result[1] = a[1] - b[1];
|
140
|
|
- result[2] = a[2] - b[2];
|
141
|
|
-}
|
142
|
|
-
|
143
|
|
-
|
144
|
|
-void mtkVectorAdd(vec3_t a, vec3_t b, vec3_t result)
|
145
|
|
-{
|
146
|
|
- result[0] = a[0] + b[0];
|
147
|
|
- result[1] = a[1] + b[1];
|
148
|
|
- result[2] = a[2] + b[2];
|
149
|
|
-}
|
150
|
|
-
|
151
|
|
-
|
152
|
|
-// Magnitude
|
153
|
|
-vec_t mtkVectorNorm(vec3_t v)
|
154
|
|
-{
|
155
|
|
- return (vec_t)sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
|
156
|
|
-}
|
157
|
|
-
|
158
|
|
-
|
159
|
|
-void mtkVectorNormalize(vec3_t v, vec3_t result)
|
160
|
|
-{
|
161
|
|
- vec_t mag = mtkVectorNorm(v);
|
162
|
|
-
|
163
|
|
-
|
164
|
|
- result[0] = v[0] / mag;
|
165
|
|
- result[1] = v[1] / mag;
|
166
|
|
- result[2] = v[2] / mag;
|
167
|
|
-}
|
168
|
|
-
|
169
|
|
-
|
170
|
|
-void mtkVectorCrossProduct(vec3_t a, vec3_t b, vec3_t normal)
|
171
|
|
-{
|
172
|
|
- normal[0] = a[1]*b[2] - a[2]*b[1];
|
173
|
|
- normal[0] = a[2]*b[0] - a[0]*b[2];
|
174
|
|
- normal[0] = a[0]*b[1] - a[1]*b[0];
|
175
|
|
-}
|
176
|
|
-
|
177
|
|
-
|
178
|
|
-vec_t mtkVectorDotProduct(vec3_t a, vec3_t b)
|
179
|
|
-{
|
180
|
|
- return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
|
181
|
|
-}
|
182
|
|
-
|
183
|
|
-
|
184
|
|
-// Heh, this is scary... try to avoid many indexing computations
|
185
|
|
-void mtkMatrixMult(matrix_t m1, matrix_t m2, matrix_t mp)
|
186
|
|
-{
|
187
|
|
-#ifdef MTK3D_MULT_DIM_MATRIX
|
188
|
|
- int i,j;
|
189
|
|
-
|
190
|
|
-
|
191
|
|
- for (i = 0; i < 4; i++)
|
192
|
|
- {
|
193
|
|
- for (j = 0 ; j < 4; j++)
|
194
|
|
- {
|
195
|
|
- mp[i][j] = m1[i][0] * m2[0][j] +
|
196
|
|
- m1[i][1] * m2[1][j] +
|
197
|
|
- m1[i][2] * m2[2][j] +
|
198
|
|
- m1[i][3] * m2[3][j];
|
199
|
|
- }
|
200
|
|
- }
|
201
|
|
-#else
|
202
|
|
- /////////////////////////////////////////////
|
203
|
|
- // 0,0 - 0; 0,1 - 1; 0,2 - 2; 0,3 - 3
|
204
|
|
- // 1,0 - 4; 1,1 - 5; 1,2 - 6; 1,3 - 7
|
205
|
|
- // 2,0 - 8; 2,1 - 9; 2,2 - 10; 2,3 - 11
|
206
|
|
- // 3,0 - 12; 3,1 - 13; 3,2 - 14; 3,3 - 15
|
207
|
|
- /////////////////////////////////////////////
|
208
|
|
-
|
209
|
|
- mp[ 0] = m1[0] * m2[0] + m1[1] * m2[4] + m1[2] * m2[ 8] + m1[3] * m2[12];
|
210
|
|
- mp[ 1] = m1[0] * m2[1] + m1[1] * m2[5] + m1[2] * m2[ 9] + m1[3] * m2[13];
|
211
|
|
- mp[ 2] = m1[0] * m2[2] + m1[1] * m2[6] + m1[2] * m2[10] + m1[3] * m2[14];
|
212
|
|
- mp[ 3] = m1[0] * m2[3] + m1[1] * m2[7] + m1[2] * m2[11] + m1[3] * m2[15];
|
213
|
|
-
|
214
|
|
- mp[ 4] = m1[4] * m2[0] + m1[5] * m2[4] + m1[6] * m2[ 8] + m1[7] * m2[12];
|
215
|
|
- mp[ 5] = m1[4] * m2[1] + m1[5] * m2[5] + m1[6] * m2[ 9] + m1[7] * m2[13];
|
216
|
|
- mp[ 6] = m1[4] * m2[2] + m1[5] * m2[6] + m1[6] * m2[10] + m1[7] * m2[14];
|
217
|
|
- mp[ 7] = m1[4] * m2[3] + m1[5] * m2[7] + m1[6] * m2[11] + m1[7] * m2[15];
|
218
|
|
-
|
219
|
|
- mp[ 8] = m1[8] * m2[0] + m1[9] * m2[4] + m1[10] * m2[ 8] + m1[11] * m2[12];
|
220
|
|
- mp[ 9] = m1[8] * m2[1] + m1[9] * m2[5] + m1[10] * m2[ 9] + m1[11] * m2[13];
|
221
|
|
- mp[10] = m1[8] * m2[2] + m1[9] * m2[6] + m1[10] * m2[10] + m1[11] * m2[14];
|
222
|
|
- mp[11] = m1[8] * m2[3] + m1[9] * m2[7] + m1[10] * m2[11] + m1[11] * m2[15];
|
223
|
|
-
|
224
|
|
- mp[12] = m1[12] * m2[0] + m1[13] * m2[4] + m1[14] * m2[ 8] + m1[15] * m2[12];
|
225
|
|
- mp[13] = m1[12] * m2[1] + m1[13] * m2[5] + m1[14] * m2[ 9] + m1[15] * m2[13];
|
226
|
|
- mp[14] = m1[12] * m2[2] + m1[13] * m2[6] + m1[14] * m2[10] + m1[15] * m2[14];
|
227
|
|
- mp[15] = m1[12] * m2[3] + m1[13] * m2[7] + m1[14] * m2[11] + m1[15] * m2[15];
|
228
|
|
-#endif
|
229
|
|
-}
|
230
|
|
-
|
231
|
|
-
|
232
|
|
-void mtkMatrixConvert3x3ToMtk(float *m3x3, matrix_t dest)
|
233
|
|
-{
|
234
|
|
- if (!m3x3)
|
235
|
|
- {
|
236
|
|
- mtkMatrixIdentity(dest);
|
237
|
|
- return;
|
238
|
|
- }
|
239
|
|
-
|
240
|
|
- dest[0] = m3x3[0]; dest[1] = m3x3[1]; dest[2 ] = m3x3[2];
|
241
|
|
- dest[4] = m3x3[3]; dest[5] = m3x3[4]; dest[6 ] = m3x3[5];
|
242
|
|
- dest[8] = m3x3[6]; dest[9] = m3x3[7]; dest[10] = m3x3[8];
|
243
|
|
-
|
244
|
|
- // yeah, wolf
|
245
|
|
- dest[11] = dest[12] = dest[13] = dest[14] = 0.0f;
|
246
|
|
- dest[15] = 1.0f;
|
247
|
|
-}
|
248
|
|
-
|
249
|
|
-
|
250
|
|
-void mtkMatrixCopy(matrix_t source, matrix_t dest)
|
251
|
|
-{
|
252
|
|
-#ifdef OBSOLETE
|
253
|
|
- int i,j;
|
254
|
|
-
|
255
|
|
-
|
256
|
|
- for(i = 0; i < 4; i++)
|
257
|
|
- {
|
258
|
|
- for(j = 0 ; j < 4; j++)
|
259
|
|
- {
|
260
|
|
- dest[i][j] = source[i][j];
|
261
|
|
- }
|
262
|
|
- }
|
263
|
|
-#else
|
264
|
|
- dest[ 0] = source[ 0];
|
265
|
|
- dest[ 1] = source[ 1];
|
266
|
|
- dest[ 2] = source[ 2];
|
267
|
|
- dest[ 3] = source[ 3];
|
268
|
|
-
|
269
|
|
- dest[ 4] = source[ 4];
|
270
|
|
- dest[ 5] = source[ 5];
|
271
|
|
- dest[ 6] = source[ 6];
|
272
|
|
- dest[ 7] = source[ 7];
|
273
|
|
-
|
274
|
|
- dest[ 8] = source[8];
|
275
|
|
- dest[ 9] = source[9];
|
276
|
|
- dest[10] = source[10];
|
277
|
|
- dest[11] = source[11];
|
278
|
|
-
|
279
|
|
- dest[12] = source[12];
|
280
|
|
- dest[13] = source[13];
|
281
|
|
- dest[14] = source[14];
|
282
|
|
- dest[15] = source[15];
|
283
|
|
-#endif
|
284
|
|
-}
|
285
|
|
-
|
286
|
|
-
|
287
|
|
-void mtkMatrixTransform(matrix_t t, vec3_t p)
|
288
|
|
-{
|
289
|
|
- vec_t m1[4];
|
290
|
|
-
|
291
|
|
-
|
292
|
|
- m1[0] = p[0];
|
293
|
|
- m1[1] = p[1];
|
294
|
|
- m1[2] = p[2];
|
295
|
|
- m1[3] = 1;
|
296
|
|
-
|
297
|
|
-#ifdef OBSOLETE
|
298
|
|
- p[0] = t[0][0]*m1[0] + t[1][0]*m1[1] + t[2][0]*m1[2] + t[3][0]*m1[3];
|
299
|
|
- p[1] = t[0][1]*m1[0] + t[1][1]*m1[1] + t[2][1]*m1[2] + t[3][1]*m1[3];
|
300
|
|
- p[2] = t[0][2]*m1[0] + t[1][2]*m1[1] + t[2][2]*m1[2] + t[3][2]*m1[3];
|
301
|
|
-#else
|
302
|
|
- p[0] = t[0] * m1[0] + t[4] * m1[1] + t[ 8] * m1[2] + t[12] * m1[3];
|
303
|
|
- p[1] = t[1] * m1[0] + t[5] * m1[1] + t[ 9] * m1[2] + t[13] * m1[3];
|
304
|
|
- p[2] = t[2] * m1[0] + t[6] * m1[1] + t[10] * m1[2] + t[14] * m1[3];
|
305
|
|
-#endif
|
306
|
|
-}
|
307
|
|
-
|
308
|
|
-
|
309
|
|
-void mtkMatrixIdentity(matrix_t matrix)
|
310
|
|
-{
|
311
|
|
-#ifdef OBSOLETE
|
312
|
|
- matrix[0][0] = 1; matrix[0][1] = 0; matrix[0][2] = 0; matrix[0][3] = 0;
|
313
|
|
- matrix[1][0] = 0; matrix[1][1] = 1; matrix[1][2] = 0; matrix[1][3] = 0;
|
314
|
|
- matrix[2][0] = 0; matrix[2][1] = 0; matrix[2][2] = 1; matrix[2][3] = 0;
|
315
|
|
- matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
|
316
|
|
-#else
|
317
|
|
- matrix[ 0] = 1; matrix[ 1] = 0; matrix[ 2] = 0; matrix[ 3] = 0;
|
318
|
|
- matrix[ 4] = 0; matrix[ 5] = 1; matrix[ 6] = 0; matrix[ 7] = 0;
|
319
|
|
- matrix[ 8] = 0; matrix[ 9] = 0; matrix[10] = 1; matrix[11] = 0;
|
320
|
|
- matrix[12] = 0; matrix[13] = 0; matrix[14] = 0; matrix[15] = 1;
|
321
|
|
-#endif
|
322
|
|
-}
|
323
|
|
-
|
324
|
|
-
|
325
|
|
-// FIXME: Might want to make scale matrix, etc consts
|
326
|
|
-void mtkMatrixScale(matrix_t matrix, vec_t sx, vec_t sy, vec_t sz)
|
327
|
|
-{
|
328
|
|
- matrix_t tmp, smatrix;
|
329
|
|
-
|
330
|
|
-
|
331
|
|
-#ifdef OBSOLETE
|
332
|
|
- smatrix[0][0] = sx;smatrix[0][1] = 0; smatrix[0][2] = 0; smatrix[0][3] = 0;
|
333
|
|
- smatrix[1][0] = 0; smatrix[1][1] = sy;smatrix[1][2] = 0; smatrix[1][3] = 0;
|
334
|
|
- smatrix[2][0] = 0; smatrix[2][1] = 0; smatrix[2][2] = sz;smatrix[2][3] = 0;
|
335
|
|
- smatrix[3][0] = 0; smatrix[3][1] = 0; smatrix[3][2] = 0; smatrix[3][3] = 1;
|
336
|
|
-#else
|
337
|
|
- smatrix[ 0] = sx;smatrix[ 1] = 0; smatrix[ 2] = 0; smatrix[ 3] = 0;
|
338
|
|
- smatrix[ 4] = 0; smatrix[ 5] = sy;smatrix[ 6] = 0; smatrix[ 7] = 0;
|
339
|
|
- smatrix[ 8] = 0; smatrix[ 9] = 0; smatrix[10] = sz;smatrix[11] = 0;
|
340
|
|
- smatrix[12] = 0; smatrix[13] = 0; smatrix[14] = 0; smatrix[15] = 1;
|
341
|
|
-#endif
|
342
|
|
-
|
343
|
|
- mtkMatrixMult(matrix, smatrix, tmp);
|
344
|
|
- mtkMatrixCopy(tmp, matrix);
|
345
|
|
-}
|
346
|
|
-
|
347
|
|
-void mtkMatrixRotate(matrix_t matrix, vec_t ax, vec_t ay, vec_t az)
|
348
|
|
-{
|
349
|
|
- matrix_t xmat, ymat, zmat, tmp, tmp2;
|
350
|
|
-
|
351
|
|
-
|
352
|
|
-#ifdef OBSOLETE
|
353
|
|
- xmat[0][0]=1; xmat[0][1]=0; xmat[0][2]=0; xmat[0][3]=0;
|
354
|
|
- xmat[1][0]=0; xmat[1][1]=cos(ax); xmat[1][2]=sin(ax); xmat[1][3]=0;
|
355
|
|
- xmat[2][0]=0; xmat[2][1]=-sin(ax); xmat[2][2]=cos(ax); xmat[2][3]=0;
|
356
|
|
- xmat[3][0]=0; xmat[3][1]=0; xmat[3][2]=0; xmat[3][3]=1;
|
357
|
|
-
|
358
|
|
- ymat[0][0]=cos(ay); ymat[0][1]=0; ymat[0][2]=-sin(ay); ymat[0][3]=0;
|
359
|
|
- ymat[1][0]=0; ymat[1][1]=1; ymat[1][2]=0; ymat[1][3]=0;
|
360
|
|
- ymat[2][0]=sin(ay); ymat[2][1]=0; ymat[2][2]=cos(ay); ymat[2][3]=0;
|
361
|
|
- ymat[3][0]=0; ymat[3][1]=0; ymat[3][2]=0; ymat[3][3]=1;
|
362
|
|
-
|
363
|
|
- zmat[0][0]=cos(az); zmat[0][1]=sin(az); zmat[0][2]=0; zmat[0][3]=0;
|
364
|
|
- zmat[1][0]=-sin(az); zmat[1][1]=cos(az); zmat[1][2]=0; zmat[1][3]=0;
|
365
|
|
- zmat[2][0]=0; zmat[2][1]=0; zmat[2][2]=1; zmat[2][3]=0;
|
366
|
|
- zmat[3][0]=0; zmat[3][1]=0; zmat[3][2]=0; zmat[3][3]=1;
|
367
|
|
-#else
|
368
|
|
- xmat[ 0]=1; xmat[ 1]=0; xmat[ 2]=0; xmat[ 3]=0;
|
369
|
|
- xmat[ 4]=0; xmat[ 5]=cos(ax); xmat[ 6]=sin(ax); xmat[ 7]=0;
|
370
|
|
- xmat[ 8]=0; xmat[ 9]=-sin(ax); xmat[10]=cos(ax); xmat[11]=0;
|
371
|
|
- xmat[12]=0; xmat[13]=0; xmat[14]=0; xmat[15]=1;
|
372
|
|
-
|
373
|
|
- ymat[ 0]=cos(ay); ymat[ 1]=0; ymat[ 2]=-sin(ay); ymat[ 3]=0;
|
374
|
|
- ymat[ 4]=0; ymat[ 5]=1; ymat[ 6]=0; ymat[ 7]=0;
|
375
|
|
- ymat[ 8]=sin(ay); ymat[ 9]=0; ymat[10]=cos(ay); ymat[11]=0;
|
376
|
|
- ymat[12]=0; ymat[13]=0; ymat[14]=0; ymat[15]=1;
|
377
|
|
-
|
378
|
|
- zmat[ 0]=cos(az); zmat[ 1]=sin(az); zmat[ 2]=0; zmat[ 3]=0;
|
379
|
|
- zmat[ 4]=-sin(az); zmat[ 5]=cos(az); zmat[ 6]=0; zmat[ 7]=0;
|
380
|
|
- zmat[ 8]=0; zmat[ 9]=0; zmat[10]=1; zmat[11]=0;
|
381
|
|
- zmat[12]=0; zmat[13]=0; zmat[14]=0; zmat[15]=1;
|
382
|
|
-#endif
|
383
|
|
-
|
384
|
|
- mtkMatrixMult(matrix, ymat, tmp);
|
385
|
|
- mtkMatrixMult(tmp, xmat, tmp2);
|
386
|
|
- mtkMatrixMult(tmp2, zmat, matrix);
|
387
|
|
-}
|
388
|
|
-
|
389
|
|
-void mtkMatrixTranslate(matrix_t matrix, vec_t tx, vec_t ty, vec_t tz)
|
390
|
|
-{
|
391
|
|
- matrix_t tmat, tmp;
|
392
|
|
-
|
393
|
|
-#ifdef OBSOLETE
|
394
|
|
- tmat[0][0]=1; tmat[0][1]=0; tmat[0][2]=0; tmat[0][3]=0;
|
395
|
|
- tmat[1][0]=0; tmat[1][1]=1; tmat[1][2]=0; tmat[1][3]=0;
|
396
|
|
- tmat[2][0]=0; tmat[2][1]=0; tmat[2][2]=1; tmat[2][3]=0;
|
397
|
|
- tmat[3][0]=tx; tmat[3][1]=ty; tmat[3][2]=tz; tmat[3][3]=1;
|
398
|
|
-#else
|
399
|
|
- tmat[ 0]=1; tmat[ 1]=0; tmat[ 2]=0; tmat[ 3]=0;
|
400
|
|
- tmat[ 4]=0; tmat[ 5]=1; tmat[ 6]=0; tmat[ 7]=0;
|
401
|
|
- tmat[ 8]=0; tmat[ 9]=0; tmat[10]=1; tmat[11]=0;
|
402
|
|
- tmat[12]=tx; tmat[13]=ty; tmat[14]=tz; tmat[15]=1;
|
403
|
|
-#endif
|
404
|
|
-
|
405
|
|
- mtkMatrixMult(matrix, tmat, tmp);
|
406
|
|
- mtkMatrixCopy(tmp, matrix);
|
407
|
|
-}
|
408
|
|
-
|
409
|
|
-////////////////////////////////
|
410
|
|
-
|
411
|
|
-void mtkQuaternionCopy(quaternion_t source, quaternion_t dest)
|
412
|
|
-{
|
413
|
|
- dest.x = source.x;
|
414
|
|
- dest.y = source.y;
|
415
|
|
- dest.z = source.z;
|
416
|
|
- dest.w = source.w;
|
417
|
|
-}
|
418
|
|
-
|
419
|
|
-
|
420
|
|
-void mtkQuaternionSlerp(quaternion_t qI, quaternion_t qA, quaternion_t qB,
|
421
|
|
- float time)
|
422
|
|
-{
|
423
|
|
- /*******************************************************************
|
424
|
|
- * Spherical Linear Interpolation algorthim
|
425
|
|
- *-----------------------------------------------------------------
|
426
|
|
- *
|
427
|
|
- * Interpolate between A and B rotations ( Find qI )
|
428
|
|
- *
|
429
|
|
- * qI = (((qB . qA)^ -1)^ Time) qA
|
430
|
|
- *
|
431
|
|
- * http://www.magic-software.com/Documentation/quat.pdf
|
432
|
|
- *
|
433
|
|
- * Thanks to digiben for algorithms and basis of the notes in
|
434
|
|
- * this func
|
435
|
|
- *
|
436
|
|
- *******************************************************************/
|
437
|
|
-
|
438
|
|
- float result, scaleA, scaleB, theta, sinTheta;
|
439
|
|
-
|
440
|
|
-
|
441
|
|
- // Don't bother if it's the same rotation, it's the same as the result
|
442
|
|
- if (&qA == &qB ||
|
443
|
|
- qA.x == qB.x && qA.y == qB.y && qA.z == qB.z && qA.w == qB.w)
|
444
|
|
- {
|
445
|
|
- mtkQuaternionCopy(qA, qI);
|
446
|
|
- return;
|
447
|
|
- }
|
448
|
|
-
|
449
|
|
- // A . B
|
450
|
|
- result = (qA.x * qB.x) + (qA.y * qB.y) + (qA.z * qB.z) + (qA.w * qB.w);
|
451
|
|
-
|
452
|
|
- // If the dot product is less than 0, the angle is greater than 90 degrees
|
453
|
|
- if (result < 0.0f)
|
454
|
|
- {
|
455
|
|
- // Negate quaternion B and the result of the dot product
|
456
|
|
- qB.x = -qB.x;
|
457
|
|
- qB.y = -qB.y;
|
458
|
|
- qB.z = -qB.z;
|
459
|
|
- qB.w = -qB.w;
|
460
|
|
-
|
461
|
|
- result = -result;
|
462
|
|
- }
|
463
|
|
-
|
464
|
|
- // Set the first and second scale for the interpolation
|
465
|
|
- scaleA = 1 - time;
|
466
|
|
- scaleB = time;
|
467
|
|
-
|
468
|
|
- // Next, we want to actually calculate the spherical interpolation. Since this
|
469
|
|
- // calculation is quite computationally expensive, we want to only perform it
|
470
|
|
- // if the angle between the 2 quaternions is large enough to warrant it. If the
|
471
|
|
- // angle is fairly small, we can actually just do a simpler linear interpolation
|
472
|
|
- // of the 2 quaternions, and skip all the complex math. We create a "delta" value
|
473
|
|
- // of 0.1 to say that if the cosine of the angle (result of the dot product) between
|
474
|
|
- // the 2 quaternions is smaller than 0.1, then we do NOT want to perform the full on
|
475
|
|
- // interpolation using. This is because you won't really notice the difference.
|
476
|
|
-
|
477
|
|
- // Check if the angle between the 2 quaternions was big enough
|
478
|
|
- // to warrant such calculations
|
479
|
|
- if (1 - result > 0.1f)
|
480
|
|
- {
|
481
|
|
- // Get the angle between the 2 quaternions, and then
|
482
|
|
- // store the sin() of that angle
|
483
|
|
- theta = (float)acos(result);
|
484
|
|
- sinTheta = (float)sin(theta);
|
485
|
|
-
|
486
|
|
- // Calculate the scale for qA and qB, according to
|
487
|
|
- // the angle and it's sine value
|
488
|
|
- scaleA = (float)sin((1 - time) * theta) / sinTheta;
|
489
|
|
- scaleB = (float)sin((time * theta)) / sinTheta;
|
490
|
|
- }
|
491
|
|
-
|
492
|
|
- // Calculate the x, y, z and w values for the quaternion by using a special
|
493
|
|
- // form of linear interpolation for quaternions.
|
494
|
|
- qI.x = (scaleA * qA.x) + (scaleB * qB.x);
|
495
|
|
- qI.y = (scaleA * qA.y) + (scaleB * qB.y);
|
496
|
|
- qI.z = (scaleA * qA.z) + (scaleB * qB.z);
|
497
|
|
- qI.w = (scaleA * qA.w) + (scaleB * qB.w);
|
498
|
|
-}
|
499
|
|
-
|
500
|
|
-
|
501
|
|
-void mtkQuaternionFrom3x3Matrix(float *m3x3, quaternion_t q)
|
502
|
|
-{
|
503
|
|
- matrix_t matrix;
|
504
|
|
-
|
505
|
|
-
|
506
|
|
- mtkMatrixConvert3x3ToMtk(m3x3, matrix);
|
507
|
|
- mtkQuaternionFromMatrix(matrix, q);
|
508
|
|
-}
|
509
|
|
-
|
510
|
|
-
|
511
|
|
-void mtkQuaternionFromMatrix(matrix_t matrix, quaternion_t q)
|
512
|
|
-{
|
513
|
|
- float diagonal, scale;
|
514
|
|
-
|
515
|
|
-
|
516
|
|
- diagonal = matrix[0] + matrix[5] + matrix[10] + 1;
|
517
|
|
- scale = 0.0f;
|
518
|
|
-
|
519
|
|
- mtkQuaternionIdentity(q);
|
520
|
|
-
|
521
|
|
- // diagonal is greater than 'zero'
|
522
|
|
- if (diagonal > 0.00000001)
|
523
|
|
- {
|
524
|
|
- scale = float(sqrt(diagonal) * 2);
|
525
|
|
-
|
526
|
|
- q.x = (matrix[9] - matrix[6]) / scale;
|
527
|
|
- q.y = (matrix[2] - matrix[8]) / scale;
|
528
|
|
- q.z = (matrix[4] - matrix[1]) / scale;
|
529
|
|
- q.w = 0.25f * scale;
|
530
|
|
- }
|
531
|
|
- else
|
532
|
|
- {
|
533
|
|
- // If the first element of the diagonal is the greatest value
|
534
|
|
- if (matrix[0] > matrix[5] && matrix[0] > matrix[10])
|
535
|
|
- {
|
536
|
|
- // Find the scale according to the first element, and double it
|
537
|
|
- scale = (float)sqrt(1.0f + matrix[0] - matrix[5] - matrix[10]) * 2.0f;
|
538
|
|
-
|
539
|
|
- // Calculate the x, y, x and w of the quaternion
|
540
|
|
- q.x = 0.25f * scale;
|
541
|
|
- q.y = (matrix[4] + matrix[1]) / scale;
|
542
|
|
- q.z = (matrix[2] + matrix[8]) / scale;
|
543
|
|
- q.w = (matrix[9] - matrix[6]) / scale;
|
544
|
|
- }
|
545
|
|
- // Else if the second element of the diagonal is the greatest value
|
546
|
|
- else if (matrix[5] > matrix[10])
|
547
|
|
- {
|
548
|
|
- // Find the scale according to the second element, and double it
|
549
|
|
- scale = (float)sqrt(1.0f + matrix[5] - matrix[0] - matrix[10]) * 2.0f;
|
550
|
|
-
|
551
|
|
- // Calculate the x, y, x and w of the quaternion
|
552
|
|
- q.x = (matrix[4] + matrix[1]) / scale;
|
553
|
|
- q.y = 0.25f * scale;
|
554
|
|
- q.z = (matrix[9] + matrix[6]) / scale;
|
555
|
|
- q.w = (matrix[2] - matrix[8]) / scale;
|
556
|
|
- }
|
557
|
|
- // Else the third element of the diagonal is the greatest value
|
558
|
|
- else
|
559
|
|
- {
|
560
|
|
- // Find the scale according to the third element, and double it
|
561
|
|
- scale = (float)sqrt(1.0f + matrix[10] - matrix[0] - matrix[5]) * 2.0f;
|
562
|
|
-
|
563
|
|
- // Calculate the x, y, x and w of the quaternion
|
564
|
|
- q.x = (matrix[2] + matrix[8]) / scale;
|
565
|
|
- q.y = (matrix[9] + matrix[6]) / scale;
|
566
|
|
- q.z = 0.25f * scale;
|
567
|
|
- q.w = (matrix[4] - matrix[1]) / scale;
|
568
|
|
- }
|
569
|
|
- }
|
570
|
|
-}
|
571
|
|
-
|
572
|
|
-
|
573
|
|
-void mtkQuaternionToMatrix(quaternion_t q, matrix_t m)
|
574
|
|
-{
|
575
|
|
-#ifdef OBSOLETE
|
576
|
|
- float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
|
577
|
|
-
|
578
|
|
-
|
579
|
|
- x2 = q.x + q.x; y2 = q.y + q.y; z2 = q.z + q.z;
|
580
|
|
- xx = q.x * x2; xy = q.x * y2; xz = q.x * z2;
|
581
|
|
- yy = q.y * y2; yz = q.y * z2; zz = q.z * z2;
|
582
|
|
- wx = q.w * x2; wy = q.w * y2; wz = q.w * z2;
|
583
|
|
-
|
584
|
|
- m[ 0] = 1.0 - (yy + zz);
|
585
|
|
- m[ 1] = xy - wz;
|
586
|
|
- m[ 2] = xz + wy;
|
587
|
|
- m[ 3] = 0.0;
|
588
|
|
-
|
589
|
|
- m[ 4] = xy + wz;
|
590
|
|
- m[ 5] = 1.0 - (xx + zz);
|
591
|
|
- m[ 6] = yz - wx;
|
592
|
|
- m[ 7] = 0.0;
|
593
|
|
-
|
594
|
|
- m[ 8] = xz - wy;
|
595
|
|
- m[ 9] = yz + wx;
|
596
|
|
- m[10] = 1.0 - (xx + yy);
|
597
|
|
- m[11] = 0.0;
|
598
|
|
-
|
599
|
|
- m[12] = 0;
|
600
|
|
- m[13] = 0;
|
601
|
|
- m[14] = 0;
|
602
|
|
- m[15] = 1;
|
603
|
|
-#else
|
604
|
|
- m[ 0] = 1.0f - 2.0f * (q.y*q.y + q.z*q.z);
|
605
|
|
- m[ 1] = 2.0f * (q.x*q.y - q.w*q.z);
|
606
|
|
- m[ 2] = 2.0f * (q.x*q.z + q.w*q.y);
|
607
|
|
- m[ 3] = 0.0f;
|
608
|
|
-
|
609
|
|
- m[ 4] = 2.0f * (q.x*q.y + q.w*q.z);
|
610
|
|
- m[ 5] = 1.0f - 2.0f * (q.x*q.x + q.z*q.z);
|
611
|
|
- m[ 6] = 2.0f * (q.y*q.z - q.w*q.x);
|
612
|
|
- m[ 7] = 0.0f;
|
613
|
|
-
|
614
|
|
- m[ 8] = 2.0f * (q.x*q.z - q.w*q.y);
|
615
|
|
- m[ 9] = 2.0f * (q.y*q.z + q.w*q.x);
|
616
|
|
- m[10] = 1.0 - 2.0f * (q.x*q.x + q.y*q.y);
|
617
|
|
- m[11] = 0.0f;
|
618
|
|
-
|
619
|
|
- m[12] = 0.0f;
|
620
|
|
- m[13] = 0.0f;
|
621
|
|
- m[14] = 0.0f;
|
622
|
|
- m[15] = 1.0f;
|
623
|
|
-#endif
|
624
|
|
-}
|
625
|
|
-
|
626
|
|
-
|
627
|
|
-void mtkQuaternionIdentity(quaternion_t q)
|
628
|
|
-{
|
629
|
|
- q.w = 1.0;
|
630
|
|
- q.x = 0.0;
|
631
|
|
- q.y = 0.0;
|
632
|
|
- q.z = 0.0;
|
633
|
|
-}
|
634
|
|
-
|
635
|
|
-
|
636
|
|
-void mtkQuaternionSet(quaternion_t q, float angle, float x, float y, float z)
|
637
|
|
-{
|
638
|
|
- float temp, dist;
|
639
|
|
-
|
640
|
|
- // Normalize
|
641
|
|
- temp = x*x + y*y + z*z;
|
642
|
|
-
|
643
|
|
- dist = (float)(1.0 / sqrt(temp));
|
644
|
|
-
|
645
|
|
- x *= dist;
|
646
|
|
- y *= dist;
|
647
|
|
- z *= dist;
|
648
|
|
-
|
649
|
|
- q.x = x;
|
650
|
|
- q.y = y;
|
651
|
|
- q.z = z;
|
652
|
|
-
|
653
|
|
- q.w = (float)cos(angle / 2.0f);
|
654
|
|
-}
|
655
|
|
-
|
656
|
|
-
|
657
|
|
-void mtkQuaternionMult(quaternion_t a, quaternion_t b, quaternion_t result)
|
658
|
|
-{
|
659
|
|
- result.x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y;
|
660
|
|
- result.y = a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z;
|
661
|
|
- result.z = a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x;
|
662
|
|
- result.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
|
663
|
|
-
|
664
|
|
- // Quaternion needs to be a unit quaternion
|
665
|
|
- mtkQuaternionNormalize(result);
|
666
|
|
-}
|
667
|
|
-
|
668
|
|
-
|
669
|
|
-void mtkQuaternionNormalize(quaternion_t q)
|
670
|
|
-{
|
671
|
|
- float dist, square;
|
672
|
|
-
|
673
|
|
-
|
674
|
|
- square = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
|
675
|
|
-
|
676
|
|
- if (square > 0.00000001)
|
677
|
|
- {
|
678
|
|
- dist = (float)(1.0 / sqrt(square));
|
679
|
|
- }
|
680
|
|
- else
|
681
|
|
- {
|
682
|
|
- dist = 1;
|
683
|
|
- }
|
684
|
|
-
|
685
|
|
- q.x *= dist;
|
686
|
|
- q.y *= dist;
|
687
|
|
- q.z *= dist;
|
688
|
|
- q.w *= dist;
|
689
|
|
-}
|
690
|
|
-
|