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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
/*
* PARSEC - Math Code I (ANSI-C)
*
* $Author: uberlinuxguy $ - $Date: 2004/09/15 12:25:43 $
*
* Orginally written by:
* Copyright (c) Clemens Beer <cbx@parsec.org> 2002
* Copyright (c) Markus Hadwiger <msh@parsec.org> 1998-1999
* Copyright (c) Andreas Varga <sid@parsec.org> 1998
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// C library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// compilation flags/debug support
#include "config.h"
#include "debug.h"
// general definitions
#include "general.h"
#include "objstruc.h"
// global externals
#include "globals.h"
// mathematics header
#include "utl_math.h"
// local module header
// flags
//#define USE_SINCOSTABLE
// sine/cosine tables ---------------------------------------------------------
//
float fsin_tab[] =
#include "utl_fsin.h"
float fcos_tab[] =
#include "utl_fcos.h"
// adjoint matrix sub-determinant table ---------------------------------------
//
dword adj_tab[ 4*9 ] = {
#define M_WIDTH 4 // number of row elements
M_WIDTH*1+1, // [1][1]
M_WIDTH*2+3, // [2][2]
M_WIDTH*1+3, // [1][2]
M_WIDTH*2+1, // [2][1]
M_WIDTH*0+3, // [0][2]
M_WIDTH*2+1, // [2][1]
M_WIDTH*0+1, // [0][1]
M_WIDTH*2+3, // [2][2]
M_WIDTH*0+1, // [0][1]
M_WIDTH*1+3, // [1][2]
M_WIDTH*0+3, // [0][2]
M_WIDTH*1+1, // [1][1]
M_WIDTH*1+3, // [1][2]
M_WIDTH*2+0, // [2][0]
M_WIDTH*1+0, // [1][0]
M_WIDTH*2+3, // [2][2]
M_WIDTH*0+0, // [0][0]
M_WIDTH*2+3, // [2][2]
M_WIDTH*0+3, // [0][2]
M_WIDTH*2+0, // [2][0]
M_WIDTH*0+3, // [0][2]
M_WIDTH*1+0, // [1][0]
M_WIDTH*0+0, // [0][0]
M_WIDTH*1+3, // [1][2]
M_WIDTH*1+0, // [1][0]
M_WIDTH*2+1, // [2][1]
M_WIDTH*1+1, // [1][1]
M_WIDTH*2+0, // [2][0]
M_WIDTH*0+1, // [0][1]
M_WIDTH*2+0, // [2][0]
M_WIDTH*0+0, // [0][0]
M_WIDTH*2+1, // [2][1]
M_WIDTH*0+0, // [0][0]
M_WIDTH*1+1, // [1][1]
M_WIDTH*0+1, // [0][1]
M_WIDTH*1+0, // [1][0]
};
// calculate adjoint 3x3 matrix -----------------------------------------------
//
void AdjointMtx( const Xmatrx smatrx, Xmatrx dmatrx )
{
ASSERT( smatrx != NULL );
ASSERT( dmatrx != NULL );
ASSERT( smatrx != dmatrx );
geomv_t *pdmatrx = (geomv_t *) dmatrx;
int tabindx = 0;
for ( int curdet = 0; curdet < 9; curdet++, tabindx+=4 ) {
geomv_t A = *( (geomv_t *)smatrx + adj_tab[ tabindx + 0 ] );
geomv_t B = *( (geomv_t *)smatrx + adj_tab[ tabindx + 2 ] );
geomv_t C = *( (geomv_t *)smatrx + adj_tab[ tabindx + 3 ] );
geomv_t D = *( (geomv_t *)smatrx + adj_tab[ tabindx + 1 ] );
geomv_t d1 = GEOMV_MUL( A, D );
geomv_t d2 = GEOMV_MUL( B, C );
pdmatrx[ curdet ] = d1 - d2;
}
}
// multiply two 4x4 matrices --------------------------------------------------
//
void MtxMtxMUL( const Xmatrx matrxb, const Xmatrx matrxa, Xmatrx dmatrx )
{
ASSERT( matrxb != NULL );
ASSERT( matrxa != NULL );
ASSERT( dmatrx != NULL );
ASSERT( matrxb != dmatrx );
ASSERT( matrxa != dmatrx );
//NOTE:
// D = B * A
// ---------
// [ d d d d ] [ b b b b ] [ a a a a ]
// [ d d d d ] = [ b b b b ] * [ a a a a ]
// [ d d d d ] [ b b b b ] [ a a a a ]
// [ 0 0 0 1 ] [ 0 0 0 1 ] [ 0 0 0 1 ]
dmatrx[0][0] = GEOMV_MUL(matrxb[0][0],matrxa[0][0]) + GEOMV_MUL(matrxb[0][1],matrxa[1][0]) + GEOMV_MUL(matrxb[0][2],matrxa[2][0]);
dmatrx[0][1] = GEOMV_MUL(matrxb[0][0],matrxa[0][1]) + GEOMV_MUL(matrxb[0][1],matrxa[1][1]) + GEOMV_MUL(matrxb[0][2],matrxa[2][1]);
dmatrx[0][2] = GEOMV_MUL(matrxb[0][0],matrxa[0][2]) + GEOMV_MUL(matrxb[0][1],matrxa[1][2]) + GEOMV_MUL(matrxb[0][2],matrxa[2][2]);
dmatrx[0][3] = GEOMV_MUL(matrxb[0][0],matrxa[0][3]) + GEOMV_MUL(matrxb[0][1],matrxa[1][3]) + GEOMV_MUL(matrxb[0][2],matrxa[2][3]) + matrxb[0][3];
dmatrx[1][0] = GEOMV_MUL(matrxb[1][0],matrxa[0][0]) + GEOMV_MUL(matrxb[1][1],matrxa[1][0]) + GEOMV_MUL(matrxb[1][2],matrxa[2][0]);
dmatrx[1][1] = GEOMV_MUL(matrxb[1][0],matrxa[0][1]) + GEOMV_MUL(matrxb[1][1],matrxa[1][1]) + GEOMV_MUL(matrxb[1][2],matrxa[2][1]);
dmatrx[1][2] = GEOMV_MUL(matrxb[1][0],matrxa[0][2]) + GEOMV_MUL(matrxb[1][1],matrxa[1][2]) + GEOMV_MUL(matrxb[1][2],matrxa[2][2]);
dmatrx[1][3] = GEOMV_MUL(matrxb[1][0],matrxa[0][3]) + GEOMV_MUL(matrxb[1][1],matrxa[1][3]) + GEOMV_MUL(matrxb[1][2],matrxa[2][3]) + matrxb[1][3];
dmatrx[2][0] = GEOMV_MUL(matrxb[2][0],matrxa[0][0]) + GEOMV_MUL(matrxb[2][1],matrxa[1][0]) + GEOMV_MUL(matrxb[2][2],matrxa[2][0]);
dmatrx[2][1] = GEOMV_MUL(matrxb[2][0],matrxa[0][1]) + GEOMV_MUL(matrxb[2][1],matrxa[1][1]) + GEOMV_MUL(matrxb[2][2],matrxa[2][1]);
dmatrx[2][2] = GEOMV_MUL(matrxb[2][0],matrxa[0][2]) + GEOMV_MUL(matrxb[2][1],matrxa[1][2]) + GEOMV_MUL(matrxb[2][2],matrxa[2][2]);
dmatrx[2][3] = GEOMV_MUL(matrxb[2][0],matrxa[0][3]) + GEOMV_MUL(matrxb[2][1],matrxa[1][3]) + GEOMV_MUL(matrxb[2][2],matrxa[2][3]) + matrxb[2][3];
}
// multiply two 4x4 matrices (neglects translation part of matrix a) ----------
//
void MtxMtxMULt( const Xmatrx matrxb, const Xmatrx matrxa, Xmatrx dmatrx )
{
ASSERT( matrxb != NULL );
ASSERT( matrxa != NULL );
ASSERT( dmatrx != NULL );
ASSERT( matrxb != dmatrx );
ASSERT( matrxa != dmatrx );
//NOTE:
// D = B * A
// ---------
// [ d d d d ] [ b b b b ] [ a a a 0 ]
// [ d d d d ] = [ b b b b ] * [ a a a 0 ]
// [ d d d d ] [ b b b b ] [ a a a 0 ]
// [ 0 0 0 1 ] [ 0 0 0 1 ] [ 0 0 0 1 ]
dmatrx[0][0] = GEOMV_MUL(matrxb[0][0],matrxa[0][0]) + GEOMV_MUL(matrxb[0][1],matrxa[1][0]) + GEOMV_MUL(matrxb[0][2],matrxa[2][0]);
dmatrx[0][1] = GEOMV_MUL(matrxb[0][0],matrxa[0][1]) + GEOMV_MUL(matrxb[0][1],matrxa[1][1]) + GEOMV_MUL(matrxb[0][2],matrxa[2][1]);
dmatrx[0][2] = GEOMV_MUL(matrxb[0][0],matrxa[0][2]) + GEOMV_MUL(matrxb[0][1],matrxa[1][2]) + GEOMV_MUL(matrxb[0][2],matrxa[2][2]);
dmatrx[0][3] = matrxb[0][3];
dmatrx[1][0] = GEOMV_MUL(matrxb[1][0],matrxa[0][0]) + GEOMV_MUL(matrxb[1][1],matrxa[1][0]) + GEOMV_MUL(matrxb[1][2],matrxa[2][0]);
dmatrx[1][1] = GEOMV_MUL(matrxb[1][0],matrxa[0][1]) + GEOMV_MUL(matrxb[1][1],matrxa[1][1]) + GEOMV_MUL(matrxb[1][2],matrxa[2][1]);
dmatrx[1][2] = GEOMV_MUL(matrxb[1][0],matrxa[0][2]) + GEOMV_MUL(matrxb[1][1],matrxa[1][2]) + GEOMV_MUL(matrxb[1][2],matrxa[2][2]);
dmatrx[1][3] = matrxb[1][3];
dmatrx[2][0] = GEOMV_MUL(matrxb[2][0],matrxa[0][0]) + GEOMV_MUL(matrxb[2][1],matrxa[1][0]) + GEOMV_MUL(matrxb[2][2],matrxa[2][0]);
dmatrx[2][1] = GEOMV_MUL(matrxb[2][0],matrxa[0][1]) + GEOMV_MUL(matrxb[2][1],matrxa[1][1]) + GEOMV_MUL(matrxb[2][2],matrxa[2][1]);
dmatrx[2][2] = GEOMV_MUL(matrxb[2][0],matrxa[0][2]) + GEOMV_MUL(matrxb[2][1],matrxa[1][2]) + GEOMV_MUL(matrxb[2][2],matrxa[2][2]);
dmatrx[2][3] = matrxb[2][3];
}
// multiply 4x4 matrix by 4x1 matrix (column vector) --------------------------
//
void MtxVctMUL( const Xmatrx matrx, const Vector3 *svect, Vector3 *dvect )
{
ASSERT( matrx != NULL );
ASSERT( svect != NULL );
ASSERT( dvect != NULL );
ASSERT( svect != dvect );
dvect->X = GEOMV_MUL(matrx[0][0],svect->X) + GEOMV_MUL(matrx[0][1],svect->Y) + GEOMV_MUL(matrx[0][2],svect->Z) + matrx[0][3];
dvect->Y = GEOMV_MUL(matrx[1][0],svect->X) + GEOMV_MUL(matrx[1][1],svect->Y) + GEOMV_MUL(matrx[1][2],svect->Z) + matrx[1][3];
dvect->Z = GEOMV_MUL(matrx[2][0],svect->X) + GEOMV_MUL(matrx[2][1],svect->Y) + GEOMV_MUL(matrx[2][2],svect->Z) + matrx[2][3];
}
// multiply 4x4 matrix by 4x1 matrix (column vector); skip translation --------
//
void MtxVctMULt( const Xmatrx matrx, const Vector3 *svect, Vector3 *dvect )
{
ASSERT( matrx != NULL );
ASSERT( svect != NULL );
ASSERT( dvect != NULL );
ASSERT( svect != dvect );
//NOTE:
// actually 3x3 matrix times 3x1 matrix since all
// homogeneous components (including translation!)
// are neglected (assumed to be zero/one).
dvect->X = GEOMV_MUL(matrx[0][0],svect->X) + GEOMV_MUL(matrx[0][1],svect->Y) + GEOMV_MUL(matrx[0][2],svect->Z);
dvect->Y = GEOMV_MUL(matrx[1][0],svect->X) + GEOMV_MUL(matrx[1][1],svect->Y) + GEOMV_MUL(matrx[1][2],svect->Z);
dvect->Z = GEOMV_MUL(matrx[2][0],svect->X) + GEOMV_MUL(matrx[2][1],svect->Y) + GEOMV_MUL(matrx[2][2],svect->Z);
}
// multiply basis vector 3 by scalar (generate direction vector) --------------
//
void DirVctMUL( const Xmatrx matrx, geomv_t scalar, Vector3 *dvect )
{
ASSERT( matrx != NULL );
ASSERT( dvect != NULL );
dvect->X = GEOMV_MUL( matrx[ 0 ][ 2 ], scalar );
dvect->Y = GEOMV_MUL( matrx[ 1 ][ 2 ], scalar );
dvect->Z = GEOMV_MUL( matrx[ 2 ][ 2 ], scalar );
}
// multiply basis vector 3 by scalar (generate horizontal slide vector) -------
//
void RightVctMUL( const Xmatrx matrx, geomv_t scalar, Vector3 *dvect )
{
ASSERT( matrx != NULL );
ASSERT( dvect != NULL );
dvect->X = GEOMV_MUL( matrx[ 0 ][ 0 ], scalar );
dvect->Y = GEOMV_MUL( matrx[ 1 ][ 0 ], scalar );
dvect->Z = GEOMV_MUL( matrx[ 2 ][ 0 ], scalar );
}
// multiply basis vector 3 by scalar (generate vertical slide vector) ---------
//
void UpVctMUL( const Xmatrx matrx, geomv_t scalar, Vector3 *dvect )
{
ASSERT( matrx != NULL );
ASSERT( dvect != NULL );
dvect->X = GEOMV_MUL( matrx[ 0 ][ 1 ], scalar );
dvect->Y = GEOMV_MUL( matrx[ 1 ][ 1 ], scalar );
dvect->Z = GEOMV_MUL( matrx[ 2 ][ 1 ], scalar );
}
// Reflect incident vector (ivec) on surface normal to produce reflection (destvec)
// 'normal' needs to be normalized, ivec doesn't
//
void VctReflect( const Vector3 *ivec, const Vector3 *normal, Vector3 *destvec )
{
ASSERT(ivec != NULL);
ASSERT(destvec != NULL);
ASSERT(normal != NULL);
float dot = DotProduct(ivec, normal);
destvec->X = ivec->X - 2.0f * dot * normal->X;
destvec->Y = ivec->Y - 2.0f * dot * normal->Y;
destvec->Z = ivec->Z - 2.0f * dot * normal->Z;
}
// calculate new position from forward/horizontal/vertical movements ----------
//
void CalcMovement( Vector3* movement, const Xmatrx frame, fixed_t forward, geomv_t horiz, geomv_t vert, refframe_t refframes )
{
ASSERT( movement != NULL );
ASSERT( frame != NULL );
// horizontal-slide
Vector3 rightvec;
geomv_t horiz_slide = horiz * refframes;
RightVctMUL( frame, horiz_slide, &rightvec );
movement->X = rightvec.X;
movement->Y = rightvec.Y;
movement->Z = rightvec.Z;
// vertical-slide
Vector3 upvec;
geomv_t vert_slide = vert * refframes;
UpVctMUL( frame, vert_slide, &upvec );
movement->X += upvec.X;
movement->Y += upvec.Y;
movement->Z += upvec.Z;
// forward movement
Vector3 dirvec;
fixed_t forward_movement = forward * refframes;
DirVctMUL( frame, FIXED_TO_GEOMV( forward_movement ), &dirvec );
movement->X += dirvec.X;
movement->Y += dirvec.Y;
movement->Z += dirvec.Z;
}
// fetch sine/cosine value for given angle from table -------------------------
//
void GetSinCos( dword angle, sincosval_s *resultp )
{
ASSERT( resultp != NULL );
#ifdef USE_SINCOSTABLE
//NOTE:
// uses 32K table (4K entries for sin and cos each)
// angles are in BAMS
int tabindx = ( angle & 0xffff ) >> 4;
resultp->sinval = FLOAT_TO_GEOMV( fsin_tab[ tabindx ] );
resultp->cosval = FLOAT_TO_GEOMV( fcos_tab[ tabindx ] );
#else
resultp->sinval = FLOAT_TO_GEOMV( sin( BAMS_TO_RAD( angle ) ) );
resultp->cosval = FLOAT_TO_GEOMV( cos( BAMS_TO_RAD( angle ) ) );
#endif
}
// rotate vector space around x axis (multiply from the right) ----------------
//
void ObjRotX( Xmatrx matrix, bams_t pitch )
{
ASSERT( matrix != NULL );
sincosval_s sincosv;
GetSinCos( pitch, &sincosv );
Xmatrx rotmtx;
rotmtx[0][0] = GEOMV_1;
rotmtx[0][1] = GEOMV_0;
rotmtx[0][2] = GEOMV_0;
// rotmtx[0][3] = GEOMV_0;
rotmtx[1][0] = GEOMV_0;
rotmtx[1][1] = sincosv.cosval;
rotmtx[1][2] = -sincosv.sinval;
// rotmtx[1][3] = GEOMV_0;
rotmtx[2][0] = GEOMV_0;
rotmtx[2][1] = sincosv.sinval;
rotmtx[2][2] = sincosv.cosval;
// rotmtx[2][3] = GEOMV_0;
Xmatrx destmtx;
MtxMtxMULt( matrix, rotmtx, destmtx );
memcpy( matrix, destmtx, sizeof( Xmatrx ) );
}
// rotate vector space around y axis (multiply from the right) ----------------
//
void ObjRotY( Xmatrx matrix, bams_t yaw )
{
ASSERT( matrix != NULL );
sincosval_s sincosv;
GetSinCos( yaw, &sincosv );
Xmatrx rotmtx;
rotmtx[0][0] = sincosv.cosval;
rotmtx[0][1] = GEOMV_0;
rotmtx[0][2] = sincosv.sinval;
// rotmtx[0][3] = GEOMV_0;
rotmtx[1][0] = GEOMV_0;
rotmtx[1][1] = GEOMV_1;
rotmtx[1][2] = GEOMV_0;
// rotmtx[1][3] = GEOMV_0;
rotmtx[2][0] = -sincosv.sinval;
rotmtx[2][1] = GEOMV_0;
rotmtx[2][2] = sincosv.cosval;
// rotmtx[2][3] = GEOMV_0;
Xmatrx destmtx;
MtxMtxMULt( matrix, rotmtx, destmtx );
memcpy( matrix, destmtx, sizeof( Xmatrx ) );
}
// rotate vector space around z axis (multiply from the right) ----------------
//
void ObjRotZ( Xmatrx matrix, bams_t roll )
{
ASSERT( matrix != NULL );
sincosval_s sincosv;
GetSinCos( roll, &sincosv );
Xmatrx rotmtx;
rotmtx[0][0] = sincosv.cosval;
rotmtx[0][1] = -sincosv.sinval;
rotmtx[0][2] = GEOMV_0;
// rotmtx[0][3] = GEOMV_0;
rotmtx[1][0] = sincosv.sinval;
rotmtx[1][1] = sincosv.cosval;
rotmtx[1][2] = GEOMV_0;
// rotmtx[1][3] = GEOMV_0;
rotmtx[2][0] = GEOMV_0;
rotmtx[2][1] = GEOMV_0;
rotmtx[2][2] = GEOMV_1;
// rotmtx[2][3] = GEOMV_0;
Xmatrx destmtx;
MtxMtxMULt( matrix, rotmtx, destmtx );
memcpy( matrix, destmtx, sizeof( Xmatrx ) );
}
// rotate vector space around x axis (multiply from the left) -----------------
//
void CamRotX( Xmatrx matrix, bams_t pitch )
{
ASSERT( matrix != NULL );
sincosval_s sincosv;
GetSinCos( pitch, &sincosv );
Xmatrx rotmtx;
rotmtx[0][0] = GEOMV_1;
rotmtx[0][1] = GEOMV_0;
rotmtx[0][2] = GEOMV_0;
rotmtx[0][3] = GEOMV_0;
rotmtx[1][0] = GEOMV_0;
rotmtx[1][1] = sincosv.cosval;
rotmtx[1][2] = -sincosv.sinval;
rotmtx[1][3] = GEOMV_0;
rotmtx[2][0] = GEOMV_0;
rotmtx[2][1] = sincosv.sinval;
rotmtx[2][2] = sincosv.cosval;
rotmtx[2][3] = GEOMV_0;
Xmatrx destmtx;
MtxMtxMUL( rotmtx, matrix, destmtx );
memcpy( matrix, destmtx, sizeof( Xmatrx ) );
}
// rotate vector space around y axis (multiply from the left) -----------------
//
void CamRotY( Xmatrx matrix, bams_t yaw )
{
ASSERT( matrix != NULL );
sincosval_s sincosv;
GetSinCos( yaw, &sincosv );
Xmatrx rotmtx;
rotmtx[0][0] = sincosv.cosval;
rotmtx[0][1] = GEOMV_0;
rotmtx[0][2] = sincosv.sinval;
rotmtx[0][3] = GEOMV_0;
rotmtx[1][0] = GEOMV_0;
rotmtx[1][1] = GEOMV_1;
rotmtx[1][2] = GEOMV_0;
rotmtx[1][3] = GEOMV_0;
rotmtx[2][0] = -sincosv.sinval;
rotmtx[2][1] = GEOMV_0;
rotmtx[2][2] = sincosv.cosval;
rotmtx[2][3] = GEOMV_0;
Xmatrx destmtx;
MtxMtxMUL( rotmtx, matrix, destmtx );
memcpy( matrix, destmtx, sizeof( Xmatrx ) );
}
// rotate vector space around z axis (multiply from the left) -----------------
//
void CamRotZ( Xmatrx matrix, bams_t roll )
{
ASSERT( matrix != NULL );
sincosval_s sincosv;
GetSinCos( roll, &sincosv );
Xmatrx rotmtx;
rotmtx[0][0] = sincosv.cosval;
rotmtx[0][1] = -sincosv.sinval;
rotmtx[0][2] = GEOMV_0;
rotmtx[0][3] = GEOMV_0;
rotmtx[1][0] = sincosv.sinval;
rotmtx[1][1] = sincosv.cosval;
rotmtx[1][2] = GEOMV_0;
rotmtx[1][3] = GEOMV_0;
rotmtx[2][0] = GEOMV_0;
rotmtx[2][1] = GEOMV_0;
rotmtx[2][2] = GEOMV_1;
rotmtx[2][3] = GEOMV_0;
Xmatrx destmtx;
MtxMtxMUL( rotmtx, matrix, destmtx );
memcpy( matrix, destmtx, sizeof( Xmatrx ) );
}
// calc dot-product of two vectors (*v1 * *v2) --------------------------------
//
geomv_t DotProduct( const Vector3 *vect1, const Vector3 *vect2 )
{
ASSERT( vect1 != NULL );
ASSERT( vect2 != NULL );
return GEOMV_MUL( vect1->X, vect2->X ) + GEOMV_MUL( vect1->Y, vect2->Y ) + GEOMV_MUL( vect1->Z, vect2->Z );
}
// calc cross product of two vectors (*cproduct = *v1 x *v2) ------------------
//
void CrossProduct( const Vector3 *vect1, const Vector3 *vect2, Vector3 *cproduct )
{
ASSERT( vect1 != NULL );
ASSERT( vect2 != NULL );
ASSERT( cproduct != NULL );
ASSERT( vect1 != cproduct );
ASSERT( vect2 != cproduct );
cproduct->X = GEOMV_MUL( vect1->Y, vect2->Z ) - GEOMV_MUL( vect1->Z, vect2->Y );
cproduct->Y = GEOMV_MUL( vect1->Z, vect2->X ) - GEOMV_MUL( vect1->X, vect2->Z );
cproduct->Z = GEOMV_MUL( vect1->X, vect2->Y ) - GEOMV_MUL( vect1->Y, vect2->X );
}
// calc cross product of two vectors imbedded in matrix -----------------------
//
void CrossProduct2( const geomv_t *vect1, const geomv_t *vect2, geomv_t *cproduct )
{
ASSERT( vect1 != NULL );
ASSERT( vect2 != NULL );
ASSERT( cproduct != NULL );
ASSERT( vect1 != cproduct );
ASSERT( vect2 != cproduct );
cproduct[0] = GEOMV_MUL( vect1[4], vect2[8] ) - GEOMV_MUL( vect1[8], vect2[4] );
cproduct[4] = GEOMV_MUL( vect1[8], vect2[0] ) - GEOMV_MUL( vect1[0], vect2[8] );
cproduct[8] = GEOMV_MUL( vect1[0], vect2[4] ) - GEOMV_MUL( vect1[4], vect2[0] );
}
// re-orthogonalize matrix column vectors -------------------------------------
//
void ReOrthoMtx( Xmatrx matrix )
{
ASSERT( matrix != NULL );
//TODO:
// replace with numerically more
// sane version.
CrossProduct2( &matrix[ 0 ][ 0 ], &matrix[ 0 ][ 1 ], &matrix[ 0 ][ 2 ] );
CrossProduct2( &matrix[ 0 ][ 1 ], &matrix[ 0 ][ 2 ], &matrix[ 0 ][ 0 ] );
}
#ifdef PARSEC_CLIENT
// process entire object (transforms and projects all vertices) ---------------
//
void ProcessObject( GenObject *object )
{
ASSERT( object != NULL );
// leave normals alone
int base = object->NumNormals;
Vertex3 *vtxlist = &object->VertexList[ base ];
Vertex3 *x_vtxlist = &object->X_VertexList[ base ];
SPoint *s_vtxlist = &object->S_VertexList[ base ];
for ( int numvtxs = object->NumPolyVerts; numvtxs > 0; numvtxs--, vtxlist++ ) {
// only transform and project actually visible vertices
if ( vtxlist->VisibleFrame == CurVisibleFrame ) {
// transform vertex into view-space
MtxVctMUL( object->CurrentXmatrx, vtxlist, x_vtxlist );
// project vertex onto screen (but do not add origin offset!!)
s_vtxlist->X = GEOMV_TO_COORD( GEOMV_DIV( x_vtxlist->X, x_vtxlist->Z ) );
s_vtxlist->Y = GEOMV_TO_COORD( GEOMV_DIV( x_vtxlist->Y, x_vtxlist->Z ) );
}
x_vtxlist++;
s_vtxlist++;
}
}
#endif // PARSEC_CLIENT
|