VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/matrix.c@ 62425

最後變更 在這個檔案從62425是 53726,由 vboxsync 提交於 10 年 前

properties.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 12.5 KB
 
1
2#include <math.h>
3#include <stdio.h>
4#include "cr_matrix.h"
5#include "cr_mem.h"
6
7#ifndef M_PI
8#define M_PI 3.14159265358979323846
9#endif
10
11
12static const CRmatrix identity_matrix = {
13 1.0, 0.0, 0.0, 0.0,
14 0.0, 1.0, 0.0, 0.0,
15 0.0, 0.0, 1.0, 0.0,
16 0.0, 0.0, 0.0, 1.0
17};
18
19
20/*
21 * Initialize the given matrix to the identity.
22 */
23void
24crMatrixInit(CRmatrix *m)
25{
26 *m = identity_matrix;
27}
28
29
30/*
31 * Parse a string of 16 floats to initialize a matrix (row major order).
32 * If there's a parsing error, initialize the matrix to the identity.
33 */
34void
35crMatrixInitFromString(CRmatrix *m, const char *s)
36{
37 const char *fmt = "%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f";
38 const char *fmtb = "[ %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f ]";
39 int n = sscanf(s, (s[0] == '[' ? fmtb : fmt),
40 &m->m00, &m->m01, &m->m02, &m->m03,
41 &m->m10, &m->m11, &m->m12, &m->m13,
42 &m->m20, &m->m21, &m->m22, &m->m23,
43 &m->m30, &m->m31, &m->m32, &m->m33);
44 if (n != 16) {
45 /* insufficient parameters */
46 crMatrixInit(m);
47 }
48}
49
50
51/*
52 * Initialize a matrix from an array of 16 values.
53 */
54void
55crMatrixInitFromFloats(CRmatrix *m, const float *v)
56{
57 m->m00 = v[0];
58 m->m01 = v[1];
59 m->m02 = v[2];
60 m->m03 = v[3];
61 m->m10 = v[4];
62 m->m11 = v[5];
63 m->m12 = v[6];
64 m->m13 = v[7];
65 m->m20 = v[8];
66 m->m21 = v[9];
67 m->m22 = v[10];
68 m->m23 = v[11];
69 m->m30 = v[12];
70 m->m31 = v[13];
71 m->m32 = v[14];
72 m->m33 = v[15];
73}
74
75
76void
77crMatrixInitFromDoubles(CRmatrix *m, const double *v)
78{
79 m->m00 = (float) v[0];
80 m->m01 = (float) v[1];
81 m->m02 = (float) v[2];
82 m->m03 = (float) v[3];
83 m->m10 = (float) v[4];
84 m->m11 = (float) v[5];
85 m->m12 = (float) v[6];
86 m->m13 = (float) v[7];
87 m->m20 = (float) v[8];
88 m->m21 = (float) v[9];
89 m->m22 = (float) v[10];
90 m->m23 = (float) v[11];
91 m->m30 = (float) v[12];
92 m->m31 = (float) v[13];
93 m->m32 = (float) v[14];
94 m->m33 = (float) v[15];
95}
96
97
98/* useful for debugging */
99void
100crMatrixPrint(const char *msg, const CRmatrix *m)
101{
102 printf("%s\n", msg);
103 printf(" %f %f %f %f\n", m->m00, m->m10, m->m20, m->m30);
104 printf(" %f %f %f %f\n", m->m01, m->m11, m->m21, m->m31);
105 printf(" %f %f %f %f\n", m->m02, m->m12, m->m22, m->m32);
106 printf(" %f %f %f %f\n", m->m03, m->m13, m->m23, m->m33);
107}
108
109
110void
111crMatrixGetFloats(float *values, const CRmatrix *m)
112{
113 values[0] = m->m00;
114 values[1] = m->m01;
115 values[2] = m->m02;
116 values[3] = m->m03;
117 values[4] = m->m10;
118 values[5] = m->m11;
119 values[6] = m->m12;
120 values[7] = m->m13;
121 values[8] = m->m20;
122 values[9] = m->m21;
123 values[10] = m->m22;
124 values[11] = m->m23;
125 values[12] = m->m30;
126 values[13] = m->m31;
127 values[14] = m->m32;
128 values[15] = m->m33;
129}
130
131
132/* Return 1 if the matrices are equal, return 0 otherwise.
133 */
134int
135crMatrixIsEqual(const CRmatrix *m, const CRmatrix *n)
136{
137 return crMemcmp(m, n, sizeof(CRmatrix)) == 0;
138}
139
140
141/*
142 * Test if matrix is identity
143 */
144int
145crMatrixIsIdentity(const CRmatrix *m)
146{
147 return crMemcmp(m, &identity_matrix, sizeof(CRmatrix)) == 0;
148}
149
150
151/*
152 * Test if matrix is orthographic projection matrix.
153 */
154int
155crMatrixIsOrthographic(const CRmatrix *m)
156{
157 return m->m33 != 0.0;
158}
159
160
161void
162crMatrixCopy(CRmatrix *dest, const CRmatrix *src)
163{
164 crMemcpy(dest, src, sizeof(CRmatrix));
165}
166
167
168/*
169 * Compute p = a * b
170 */
171void
172crMatrixMultiply(CRmatrix *p, const CRmatrix *a, const CRmatrix *b)
173{
174 CRmatrix t; /* temporary result, in case p = a or p = b */
175 t.m00 = a->m00 * b->m00 + a->m10 * b->m01 + a->m20 * b->m02 + a->m30 * b->m03;
176 t.m01 = a->m01 * b->m00 + a->m11 * b->m01 + a->m21 * b->m02 + a->m31 * b->m03;
177 t.m02 = a->m02 * b->m00 + a->m12 * b->m01 + a->m22 * b->m02 + a->m32 * b->m03;
178 t.m03 = a->m03 * b->m00 + a->m13 * b->m01 + a->m23 * b->m02 + a->m33 * b->m03;
179 t.m10 = a->m00 * b->m10 + a->m10 * b->m11 + a->m20 * b->m12 + a->m30 * b->m13;
180 t.m11 = a->m01 * b->m10 + a->m11 * b->m11 + a->m21 * b->m12 + a->m31 * b->m13;
181 t.m12 = a->m02 * b->m10 + a->m12 * b->m11 + a->m22 * b->m12 + a->m32 * b->m13;
182 t.m13 = a->m03 * b->m10 + a->m13 * b->m11 + a->m23 * b->m12 + a->m33 * b->m13;
183 t.m20 = a->m00 * b->m20 + a->m10 * b->m21 + a->m20 * b->m22 + a->m30 * b->m23;
184 t.m21 = a->m01 * b->m20 + a->m11 * b->m21 + a->m21 * b->m22 + a->m31 * b->m23;
185 t.m22 = a->m02 * b->m20 + a->m12 * b->m21 + a->m22 * b->m22 + a->m32 * b->m23;
186 t.m23 = a->m03 * b->m20 + a->m13 * b->m21 + a->m23 * b->m22 + a->m33 * b->m23;
187 t.m30 = a->m00 * b->m30 + a->m10 * b->m31 + a->m20 * b->m32 + a->m30 * b->m33;
188 t.m31 = a->m01 * b->m30 + a->m11 * b->m31 + a->m21 * b->m32 + a->m31 * b->m33;
189 t.m32 = a->m02 * b->m30 + a->m12 * b->m31 + a->m22 * b->m32 + a->m32 * b->m33;
190 t.m33 = a->m03 * b->m30 + a->m13 * b->m31 + a->m23 * b->m32 + a->m33 * b->m33;
191 *p = t;
192}
193
194
195void
196crMatrixTransformPointf(const CRmatrix *m, GLvectorf *p)
197{
198 float x = p->x;
199 float y = p->y;
200 float z = p->z;
201 float w = p->w;
202
203 p->x = m->m00*x + m->m10*y + m->m20*z + m->m30*w;
204 p->y = m->m01*x + m->m11*y + m->m21*z + m->m31*w;
205 p->z = m->m02*x + m->m12*y + m->m22*z + m->m32*w;
206 p->w = m->m03*x + m->m13*y + m->m23*z + m->m33*w;
207}
208
209
210void
211crMatrixTransformPointd(const CRmatrix *m, GLvectord *p)
212{
213 double x = p->x;
214 double y = p->y;
215 double z = p->z;
216 double w = p->w;
217
218 p->x = (double) (m->m00*x + m->m10*y + m->m20*z + m->m30*w);
219 p->y = (double) (m->m01*x + m->m11*y + m->m21*z + m->m31*w);
220 p->z = (double) (m->m02*x + m->m12*y + m->m22*z + m->m32*w);
221 p->w = (double) (m->m03*x + m->m13*y + m->m23*z + m->m33*w);
222}
223
224
225void
226crMatrixInvertTranspose(CRmatrix *inv, const CRmatrix *mat)
227{
228 /* Taken from Pomegranate code, trans.c.
229 * Note: We have our data structures reversed
230 */
231 const float m00 = mat->m00;
232 const float m01 = mat->m10;
233 const float m02 = mat->m20;
234 const float m03 = mat->m30;
235
236 const float m10 = mat->m01;
237 const float m11 = mat->m11;
238 const float m12 = mat->m21;
239 const float m13 = mat->m31;
240
241 const float m20 = mat->m02;
242 const float m21 = mat->m12;
243 const float m22 = mat->m22;
244 const float m23 = mat->m32;
245
246 const float m30 = mat->m03;
247 const float m31 = mat->m13;
248 const float m32 = mat->m23;
249 const float m33 = mat->m33;
250
251#define det3x3(a1, a2, a3, b1, b2, b3, c1, c2, c3) \
252 (a1 * (b2 * c3 - b3 * c2) + \
253 b1 * (c2 * a3 - a2 * c3) + \
254 c1 * (a2 * b3 - a3 * b2))
255
256 const float cof00 = det3x3( m11, m12, m13,
257 m21, m22, m23,
258 m31, m32, m33 );
259
260 const float cof01 = -det3x3( m12, m13, m10,
261 m22, m23, m20,
262 m32, m33, m30 );
263
264 const float cof02 = det3x3( m13, m10, m11,
265 m23, m20, m21,
266 m33, m30, m31 );
267
268 const float cof03 = -det3x3( m10, m11, m12,
269 m20, m21, m22,
270 m30, m31, m32 );
271
272
273 const float inv_det = 1.0f / ( m00 * cof00 + m01 * cof01 +
274 m02 * cof02 + m03 * cof03 );
275
276
277 const float cof10 = -det3x3( m21, m22, m23,
278 m31, m32, m33,
279 m01, m02, m03 );
280
281 const float cof11 = det3x3( m22, m23, m20,
282 m32, m33, m30,
283 m02, m03, m00 );
284
285 const float cof12 = -det3x3( m23, m20, m21,
286 m33, m30, m31,
287 m03, m00, m01 );
288
289 const float cof13 = det3x3( m20, m21, m22,
290 m30, m31, m32,
291 m00, m01, m02 );
292
293
294
295 const float cof20 = det3x3( m31, m32, m33,
296 m01, m02, m03,
297 m11, m12, m13 );
298
299 const float cof21 = -det3x3( m32, m33, m30,
300 m02, m03, m00,
301 m12, m13, m10 );
302
303 const float cof22 = det3x3( m33, m30, m31,
304 m03, m00, m01,
305 m13, m10, m11 );
306
307 const float cof23 = -det3x3( m30, m31, m32,
308 m00, m01, m02,
309 m10, m11, m12 );
310
311
312 const float cof30 = -det3x3( m01, m02, m03,
313 m11, m12, m13,
314 m21, m22, m23 );
315
316 const float cof31 = det3x3( m02, m03, m00,
317 m12, m13, m10,
318 m22, m23, m20 );
319
320 const float cof32 = -det3x3( m03, m00, m01,
321 m13, m10, m11,
322 m23, m20, m21 );
323
324 const float cof33 = det3x3( m00, m01, m02,
325 m10, m11, m12,
326 m20, m21, m22 );
327
328#undef det3x3
329
330 /* Perform transpose in asignment */
331
332 inv->m00 = cof00 * inv_det; inv->m10 = cof01 * inv_det;
333 inv->m20 = cof02 * inv_det; inv->m30 = cof03 * inv_det;
334
335 inv->m01 = cof10 * inv_det; inv->m11 = cof11 * inv_det;
336 inv->m21 = cof12 * inv_det; inv->m31 = cof13 * inv_det;
337
338 inv->m02 = cof20 * inv_det; inv->m12 = cof21 * inv_det;
339 inv->m22 = cof22 * inv_det; inv->m32 = cof23 * inv_det;
340
341 inv->m03 = cof30 * inv_det; inv->m13 = cof31 * inv_det;
342 inv->m23 = cof32 * inv_det; inv->m33 = cof33 * inv_det;
343}
344
345void
346crMatrixTranspose(CRmatrix *t, const CRmatrix *m)
347{
348 CRmatrix c;
349
350 c.m00 = m->m00; c.m10 = m->m01; c.m20 = m->m02; c.m30 = m->m03;
351 c.m01 = m->m10; c.m11 = m->m11; c.m21 = m->m12; c.m31 = m->m13;
352 c.m02 = m->m20; c.m12 = m->m21; c.m22 = m->m22; c.m32 = m->m23;
353 c.m03 = m->m30; c.m13 = m->m31; c.m23 = m->m32; c.m33 = m->m33;
354
355 *t = c;
356}
357
358/*
359 * Apply a translation to the given matrix.
360 */
361void
362crMatrixTranslate(CRmatrix *m, float x, float y, float z)
363{
364 m->m30 = m->m00 * x + m->m10 * y + m->m20 * z + m->m30;
365 m->m31 = m->m01 * x + m->m11 * y + m->m21 * z + m->m31;
366 m->m32 = m->m02 * x + m->m12 * y + m->m22 * z + m->m32;
367 m->m33 = m->m03 * x + m->m13 * y + m->m23 * z + m->m33;
368}
369
370
371/*
372 * Apply a rotation to the given matrix.
373 */
374void
375crMatrixRotate(CRmatrix *m, float angle, float x, float y, float z)
376{
377 const float c = (float) cos(angle * M_PI / 180.0f);
378 const float one_minus_c = 1.0f - c;
379 const float s = (float) sin(angle * M_PI / 180.0f);
380 const float v_len = (float) sqrt (x*x + y*y + z*z);
381 float x_one_minus_c;
382 float y_one_minus_c;
383 float z_one_minus_c;
384 CRmatrix rot;
385
386 /* Begin/end Checking and flushing will be done by MultMatrix. */
387
388 if (v_len == 0.0f)
389 return;
390
391 /* Normalize the vector */
392 if (v_len != 1.0f) {
393 x /= v_len;
394 y /= v_len;
395 z /= v_len;
396 }
397 /* compute some common values */
398 x_one_minus_c = x * one_minus_c;
399 y_one_minus_c = y * one_minus_c;
400 z_one_minus_c = z * one_minus_c;
401 /* Generate the terms of the rotation matrix
402 ** from pg 325 OGL 1.1 Blue Book.
403 */
404 rot.m00 = x * x_one_minus_c + c;
405 rot.m01 = x * y_one_minus_c + z * s;
406 rot.m02 = x * z_one_minus_c - y * s;
407 rot.m03 = 0.0f;
408 rot.m10 = y * x_one_minus_c - z * s;
409 rot.m11 = y * y_one_minus_c + c;
410 rot.m12 = y * z_one_minus_c + x * s;
411 rot.m13 = 0.0f;
412 rot.m20 = z * x_one_minus_c + y * s;
413 rot.m21 = z * y_one_minus_c - x * s;
414 rot.m22 = z * z_one_minus_c + c;
415 rot.m23 = 0.0f;
416 rot.m30 = 0.0f;
417 rot.m31 = 0.0f;
418 rot.m32 = 0.0f;
419 rot.m33 = 1.0f;
420 crMatrixMultiply(m, m, &rot);
421}
422
423
424/*
425 * Apply a scale to the given matrix.
426 */
427void
428crMatrixScale(CRmatrix *m, float x, float y, float z)
429{
430 m->m00 *= x;
431 m->m01 *= x;
432 m->m02 *= x;
433 m->m03 *= x;
434 m->m10 *= y;
435 m->m11 *= y;
436 m->m12 *= y;
437 m->m13 *= y;
438 m->m20 *= z;
439 m->m21 *= z;
440 m->m22 *= z;
441 m->m23 *= z;
442}
443
444
445/*
446 * Make a projection matrix from frustum parameters.
447 */
448void
449crMatrixFrustum(CRmatrix *m,
450 float left, float right,
451 float bottom, float top,
452 float zNear, float zFar)
453{
454 CRmatrix f;
455
456 f.m00 = (2.0f * zNear) / (right - left);
457 f.m01 = 0.0;
458 f.m02 = 0.0;
459 f.m03 = 0.0;
460
461 f.m10 = 0.0;
462 f.m11 = (2.0f * zNear) / (top - bottom);
463 f.m12 = 0.0;
464 f.m13 = 0.0;
465
466 f.m20 = (right + left) / (right - left);
467 f.m21 = (top + bottom) / (top - bottom);
468 f.m22 = (-zNear - zFar) / (zFar - zNear);
469 f.m23 = -1.0;
470
471 f.m30 = 0.0;
472 f.m31 = 0.0;
473 f.m32 = (2.0f * zFar * zNear) / (zNear - zFar);
474 f.m33 = 0.0;
475
476 crMatrixMultiply(m, m, &f);
477}
478
479
480void
481crMatrixOrtho(CRmatrix *m,
482 float left, float right,
483 float bottom, float top,
484 float znear, float zfar)
485{
486 CRmatrix ortho;
487
488 ortho.m00 = 2.0f / (right - left);
489 ortho.m01 = 0.0;
490 ortho.m02 = 0.0;
491 ortho.m03 = 0.0;
492
493 ortho.m10 = 0.0;
494 ortho.m11 = 2.0f / (top - bottom);
495 ortho.m12 = 0.0;
496 ortho.m13 = 0.0;
497
498 ortho.m20 = 0.0;
499 ortho.m21 = 0.0;
500 ortho.m22 = -2.0f / (zfar - znear);
501 ortho.m23 = 0.0;
502
503 ortho.m30 = -(right + left) / (right - left);
504 ortho.m31 = -(top + bottom) / (top - bottom);
505 ortho.m32= -(zfar + znear) / (zfar - znear);
506 ortho.m33 = 1.0;
507
508 crMatrixMultiply(m, m, &ortho);
509}
510
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette