summaryrefslogtreecommitdiff
path: root/include/rvl/mtx.h
blob: 3bb804edfb77dd5fd536cb6e3d456b6b64a36dd7 (plain)
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
#ifndef __MTX_H__
#define __MTX_H__





#ifdef __cplusplus
extern "C" {
#endif

/*---------------------------------------------------------------------------*
    Default function binding configuration
 *---------------------------------------------------------------------------*/
// [Binding Rule]
//
// "MTX_USE_PS" -> When this flag is specified, it uses PS* (Paired-Single
//                 assembler code) functions for non-prefixed function calls.
// "MTX_USE_C " -> When this flag is specified, it uses C_* (C code) functions
//                 for non-prefixed function calls.
//
// If both are specified, it will be treated as MTX_USE_PS.
// If nothing is specified, NDEBUG build refers PS* functions and
// DEBUG build uses C_* functions.

// For non-Gekko HW (e.g. emulator)
#ifndef GEKKO
#define MTX_USE_C
#undef  MTX_USE_PS
#endif

#if ( !defined(MTX_USE_PS) && !defined(MTX_USE_C) )
#ifndef _DEBUG
#define MTX_USE_PS
#endif
#endif

/*---------------------------------------------------------------------------*
    Macro definitions
 *---------------------------------------------------------------------------*/

// MtxPtr offset to access next Mtx of an array
#define MTX_PTR_OFFSET      3

// Mtx44Ptr offset to access next Mtx44 of an array
#define MTX44_PTR_OFFSET    4   


// matrix stack
typedef struct				    
{            

    u32    numMtx;
    MtxPtr stackBase;
    MtxPtr stackPtr;
	
} MtxStack, *MtxStackPtr;

 
// degree <--> radian conversion macros
#define MTXDegToRad(a)   ( (a) *  0.01745329252f )
#define MTXRadToDeg(a)   ( (a) * 57.29577951f )


// Matrix-element-referencing macro.
// Insulates user from changes from row-major to column-major and vice-versa.
// Fully documents which index is row, which index is column.
// XXX this version will change once matrices are transposed.

#define MTXRowCol(m,r,c) ((m)[(r)][(c)])


/*---------------------------------------------------------------------------*
    GENERAL MATRIX SECTION
 *---------------------------------------------------------------------------*/
// C version
void    C_MTXIdentity           ( Mtx m );
void    C_MTXCopy               ( const Mtx src, Mtx dst );
void    C_MTXConcat             ( const Mtx a, const Mtx b, Mtx ab );
void    C_MTXConcatArray        ( const Mtx a, const Mtx* srcBase, Mtx* dstBase, u32 count );
void    C_MTXTranspose          ( const Mtx src, Mtx xPose );
u32     C_MTXInverse            ( const Mtx src, Mtx inv );
u32     C_MTXInvXpose           ( const Mtx src, Mtx invX );

// PS assembler version
#ifdef GEKKO
void    PSMTXIdentity           ( Mtx m );
void    PSMTXCopy               ( const Mtx src, Mtx dst );
void    PSMTXConcat             ( const Mtx a, const Mtx b, Mtx ab );
void    PSMTXConcatArray        ( const Mtx a, const Mtx* srcBase, Mtx* dstBase, u32 count );
void    PSMTXTranspose          ( const Mtx src, Mtx xPose );
u32     PSMTXInverse            ( const Mtx src, Mtx inv );
u32     PSMTXInvXpose           ( const Mtx src, Mtx invX );
#endif

// Bindings
#ifdef MTX_USE_PS
#define MTXIdentity             PSMTXIdentity
#define MTXCopy                 PSMTXCopy
#define MTXConcat               PSMTXConcat
#define MTXConcatArray          PSMTXConcatArray
#define MTXTranspose            PSMTXTranspose
#define MTXInverse              PSMTXInverse
#define MTXInvXpose             PSMTXInvXpose
#else // MTX_USE_C
#define MTXIdentity             C_MTXIdentity
#define MTXCopy                 C_MTXCopy
#define MTXConcat               C_MTXConcat
#define MTXConcatArray          C_MTXConcatArray
#define MTXTranspose            C_MTXTranspose
#define MTXInverse              C_MTXInverse
#define MTXInvXpose             C_MTXInvXpose
#endif


/*---------------------------------------------------------------------------*
    MATRIX-VECTOR SECTION
 *---------------------------------------------------------------------------*/
// C version
void    C_MTXMultVec            ( const Mtx m, const Vec *src, Vec *dst );
void    C_MTXMultVecArray       ( const Mtx m, const Vec *srcBase, Vec *dstBase, u32 count );
void    C_MTXMultVecSR          ( const Mtx m, const Vec *src, Vec *dst );
void    C_MTXMultVecArraySR     ( const Mtx m, const Vec *srcBase, Vec *dstBase, u32 count );

// PS assembler version
#ifdef GEKKO
void    PSMTXMultVec            ( const Mtx m, const Vec *src, Vec *dst );
void    PSMTXMultVecArray       ( const Mtx m, const Vec *srcBase, Vec *dstBase, u32 count );
void    PSMTXMultVecSR          ( const Mtx m, const Vec *src, Vec *dst );
void    PSMTXMultVecArraySR     ( const Mtx m, const Vec *srcBase, Vec *dstBase, u32 count );
#endif

// Bindings
#ifdef MTX_USE_PS
#define MTXMultVec              PSMTXMultVec
#define MTXMultVecArray         PSMTXMultVecArray
#define MTXMultVecSR            PSMTXMultVecSR
#define MTXMultVecArraySR       PSMTXMultVecArraySR
#else // MTX_USE_C
#define MTXMultVec              C_MTXMultVec
#define MTXMultVecArray         C_MTXMultVecArray
#define MTXMultVecSR            C_MTXMultVecSR
#define MTXMultVecArraySR       C_MTXMultVecArraySR
#endif


/*---------------------------------------------------------------------------*
    MODEL MATRIX SECTION
 *---------------------------------------------------------------------------*/
// C version
void    C_MTXQuat             ( Mtx m, const Quaternion *q );
void    C_MTXReflect          ( Mtx m, const Vec *p, const Vec *n );

void    C_MTXTrans            ( Mtx m, f32 xT, f32 yT, f32 zT );
void    C_MTXTransApply       ( const Mtx src, Mtx dst, f32 xT, f32 yT, f32 zT );
void    C_MTXScale            ( Mtx m, f32 xS, f32 yS, f32 zS );
void    C_MTXScaleApply       ( const Mtx src, Mtx dst, f32 xS, f32 yS, f32 zS );

void    C_MTXRotRad           ( Mtx m, char axis, f32 rad );
void    C_MTXRotTrig          ( Mtx m, char axis, f32 sinA, f32 cosA );
void    C_MTXRotAxisRad       ( Mtx m, const Vec *axis, f32 rad );

// PS assembler version
#ifdef GEKKO
void    PSMTXQuat             ( Mtx m, const Quaternion *q );
void    PSMTXReflect          ( Mtx m, const Vec *p, const Vec *n );

void    PSMTXTrans            ( Mtx m, f32 xT, f32 yT, f32 zT );
void    PSMTXTransApply       ( const Mtx src, Mtx dst, f32 xT, f32 yT, f32 zT );
void    PSMTXScale            ( Mtx m, f32 xS, f32 yS, f32 zS );
void    PSMTXScaleApply       ( const Mtx src, Mtx dst, f32 xS, f32 yS, f32 zS );

void    PSMTXRotRad           ( Mtx m, char axis, f32 rad );
void    PSMTXRotTrig          ( Mtx m, char axis, f32 sinA, f32 cosA );
void    PSMTXRotAxisRad       ( Mtx m, const Vec *axis, f32 rad );
#endif

// Bindings
#ifdef MTX_USE_PS
#define MTXQuat                 PSMTXQuat
#define MTXReflect              PSMTXReflect
#define MTXTrans                PSMTXTrans
#define MTXTransApply           PSMTXTransApply
#define MTXScale                PSMTXScale
#define MTXScaleApply           PSMTXScaleApply
#define MTXRotRad               PSMTXRotRad
#define MTXRotTrig              PSMTXRotTrig
#define MTXRotAxisRad           PSMTXRotAxisRad

#define MTXRotDeg( m, axis, deg ) \
    PSMTXRotRad( m, axis, MTXDegToRad(deg) )
#define MTXRotAxisDeg( m, axis, deg ) \
    PSMTXRotAxisRad( m, axis, MTXDegToRad(deg) )

#else // MTX_USE_C
#define MTXQuat                 C_MTXQuat
#define MTXReflect              C_MTXReflect
#define MTXTrans                C_MTXTrans
#define MTXTransApply           C_MTXTransApply
#define MTXScale                C_MTXScale
#define MTXScaleApply           C_MTXScaleApply
#define MTXRotRad               C_MTXRotRad
#define MTXRotTrig              C_MTXRotTrig
#define MTXRotAxisRad           C_MTXRotAxisRad

#define MTXRotDeg( m, axis, deg ) \
    C_MTXRotRad( m, axis, MTXDegToRad(deg) )
#define MTXRotAxisDeg( m, axis, deg ) \
    C_MTXRotAxisRad( m, axis, MTXDegToRad(deg) )

#endif


// Obsolete. Don't use this if possible.
#define MTXRotAxis              MTXRotAxisDeg


/*---------------------------------------------------------------------------*
    VIEW MATRIX SECTION
 *---------------------------------------------------------------------------*/
// C version only so far
void    C_MTXLookAt         ( Mtx            m, 
                              const Point3d *camPos, 
                              const Vec     *camUp, 
                              const Point3d *target );

// Bindings
#define MTXLookAt               C_MTXLookAt


/*---------------------------------------------------------------------------*
    PROJECTION MATRIX SECTION
 *---------------------------------------------------------------------------*/
// C version only so far
void    C_MTXFrustum        ( Mtx44 m, f32 t, f32 b, f32 l, f32 r, f32 n, f32 f );
void    C_MTXPerspective    ( Mtx44 m, f32 fovY, f32 aspect, f32 n, f32 f );
void    C_MTXOrtho          ( Mtx44 m, f32 t, f32 b, f32 l, f32 r, f32 n, f32 f );

// Bindings
#define MTXFrustum              C_MTXFrustum
#define MTXPerspective          C_MTXPerspective
#define MTXOrtho                C_MTXOrtho


/*---------------------------------------------------------------------------*
    TEXTURE PROJECTION MATRIX SECTION
 *---------------------------------------------------------------------------*/
// C version only so far
void    C_MTXLightFrustum       ( Mtx m, f32 t, f32 b, f32 l, f32 r, f32 n, 
                                  f32 scaleS, f32 scaleT, f32 transS, 
                                  f32 transT );

void    C_MTXLightPerspective   ( Mtx m, f32 fovY, f32 aspect, f32 scaleS, 
                                  f32 scaleT, f32 transS, f32 transT );

void    C_MTXLightOrtho         ( Mtx m, f32 t, f32 b, f32 l, f32 r, f32 scaleS, 
                                  f32 scaleT, f32 transS, f32 transT );

// Bindings
#define MTXLightFrustum         C_MTXLightFrustum
#define MTXLightPerspective     C_MTXLightPerspective
#define MTXLightOrtho           C_MTXLightOrtho


/*---------------------------------------------------------------------------*
    VECTOR   SECTION
 *---------------------------------------------------------------------------*/
// C version
void    C_VECAdd              ( const Vec *a, const Vec *b, Vec *ab );
void    C_VECSubtract         ( const Vec *a, const Vec *b, Vec *a_b );
void    C_VECScale            ( const Vec *src, Vec *dst, f32 scale );
void    C_VECNormalize        ( const Vec *src, Vec *unit );
f32     C_VECSquareMag        ( const Vec *v );
f32     C_VECMag              ( const Vec *v );
f32     C_VECDotProduct       ( const Vec *a, const Vec *b );
void    C_VECCrossProduct     ( const Vec *a, const Vec *b, Vec *axb );
f32     C_VECSquareDistance   ( const Vec *a, const Vec *b );
f32     C_VECDistance         ( const Vec *a, const Vec *b );
void    C_VECReflect          ( const Vec *src, const Vec *normal, Vec *dst );
void    C_VECHalfAngle        ( const Vec *a, const Vec *b, Vec *half );

// PS assembler version
#ifdef GEKKO
void    PSVECAdd              ( const Vec *a, const Vec *b, Vec *ab );
void    PSVECSubtract         ( const Vec *a, const Vec *b, Vec *a_b );
void    PSVECScale            ( const Vec *src, Vec *dst, f32 scale );
void    PSVECNormalize        ( const Vec *src, Vec *unit );
f32     PSVECSquareMag        ( const Vec *v );
f32     PSVECMag              ( const Vec *v );
f32     PSVECDotProduct       ( const Vec *a, const Vec *b );
void    PSVECCrossProduct     ( const Vec *a, const Vec *b, Vec *axb );
f32     PSVECSquareDistance   ( const Vec *a, const Vec *b );
f32     PSVECDistance         ( const Vec *a, const Vec *b );
#endif

// Bindings
#ifdef MTX_USE_PS
#define VECAdd                  PSVECAdd
#define VECSubtract             PSVECSubtract
#define VECScale                PSVECScale
#define VECNormalize            PSVECNormalize
#define VECSquareMag            PSVECSquareMag
#define VECMag                  PSVECMag
#define VECDotProduct           PSVECDotProduct
#define VECCrossProduct         PSVECCrossProduct
#define VECSquareDistance       PSVECSquareDistance
#define VECDistance             PSVECDistance
#else // MTX_USE_C
#define VECAdd                  C_VECAdd
#define VECSubtract             C_VECSubtract
#define VECScale                C_VECScale
#define VECNormalize            C_VECNormalize
#define VECSquareMag            C_VECSquareMag
#define VECMag                  C_VECMag
#define VECDotProduct           C_VECDotProduct
#define VECCrossProduct         C_VECCrossProduct
#define VECSquareDistance       C_VECSquareDistance
#define VECDistance             C_VECDistance
#endif

#define VECReflect              C_VECReflect
#define VECHalfAngle            C_VECHalfAngle


/*---------------------------------------------------------------------------*
    QUATERNION SECTION
 *---------------------------------------------------------------------------*/
// C version
void C_QUATAdd              ( const Quaternion *p, const Quaternion *q, Quaternion *r );
void C_QUATSubtract         ( const Quaternion *p, const Quaternion *q, Quaternion *r );
void C_QUATMultiply         ( const Quaternion *p, const Quaternion *q, Quaternion *pq );
void C_QUATDivide           ( const Quaternion *p, const Quaternion *q, Quaternion *r );
void C_QUATScale            ( const Quaternion *q, Quaternion *r, f32 scale );
f32  C_QUATDotProduct       ( const Quaternion *p, const Quaternion *q );
void C_QUATNormalize        ( const Quaternion *src, Quaternion *unit );
void C_QUATInverse          ( const Quaternion *src, Quaternion *inv );
void C_QUATExp              ( const Quaternion *q, Quaternion *r );
void C_QUATLogN             ( const Quaternion *q, Quaternion *r );

void C_QUATMakeClosest      ( const Quaternion *q, const Quaternion *qto, Quaternion *r );
void C_QUATRotAxisRad       ( Quaternion *r, const Vec *axis, f32 rad );
void C_QUATMtx              ( Quaternion *r, const Mtx m );

void C_QUATLerp             ( const Quaternion *p, const Quaternion *q, Quaternion *r, f32 t );
void C_QUATSlerp            ( const Quaternion *p, const Quaternion *q, Quaternion *r, f32 t );
void C_QUATSquad            ( const Quaternion *p, const Quaternion *a, const Quaternion *b,
                              const Quaternion *q, Quaternion *r, f32 t );
void C_QUATCompA            ( const Quaternion *qprev, const Quaternion *q,
                              const Quaternion *qnext, Quaternion *a );


// PS assembler version
#ifdef GEKKO
void PSQUATAdd              ( const Quaternion *p, const Quaternion *q, Quaternion *r );
void PSQUATSubtract         ( const Quaternion *p, const Quaternion *q, Quaternion *r );
void PSQUATMultiply         ( const Quaternion *p, const Quaternion *q, Quaternion *pq );
void PSQUATDivide           ( const Quaternion *p, const Quaternion *q, Quaternion *r );
void PSQUATScale            ( const Quaternion *q, Quaternion *r, f32 scale );
f32  PSQUATDotProduct       ( const Quaternion *p, const Quaternion *q );
void PSQUATNormalize        ( const Quaternion *src, Quaternion *unit );
void PSQUATInverse          ( const Quaternion *src, Quaternion *inv );
#endif


// Bindings
#ifdef MTX_USE_PS
#define QUATAdd                 PSQUATAdd
#define QUATSubtract            PSQUATSubtract
#define QUATMultiply            PSQUATMultiply
#define QUATDivide              PSQUATDivide
#define QUATScale               PSQUATScale
#define QUATDotProduct          PSQUATDotProduct
#define QUATNormalize           PSQUATNormalize
#define QUATInverse             PSQUATInverse
#else // MTX_USE_C
#define QUATAdd                 C_QUATAdd
#define QUATSubtract            C_QUATSubtract
#define QUATMultiply            C_QUATMultiply
#define QUATDivide              C_QUATDivide
#define QUATScale               C_QUATScale
#define QUATDotProduct          C_QUATDotProduct
#define QUATNormalize           C_QUATNormalize
#define QUATInverse             C_QUATInverse
#endif

#define QUATExp                 C_QUATExp
#define QUATLogN                C_QUATLogN
#define QUATMakeClosest         C_QUATMakeClosest
#define QUATRotAxisRad          C_QUATRotAxisRad
#define QUATMtx                 C_QUATMtx
#define QUATLerp                C_QUATLerp
#define QUATSlerp               C_QUATSlerp
#define QUATSquad               C_QUATSquad
#define QUATCompA               C_QUATCompA


/*---------------------------------------------------------------------------*
    SPECIAL PURPOSE MATRIX SECTION
 *---------------------------------------------------------------------------*/
// Only PS assembler versions are available
#ifdef GEKKO
void    PSMTXReorder          ( const Mtx src, ROMtx dest );
void    PSMTXROMultVecArray   ( const ROMtx m, const Vec *srcBase, Vec *dstBase, u32 count );
void    PSMTXROSkin2VecArray  ( const ROMtx m0, const ROMtx m1, const f32 *wtBase, const Vec *srcBase, Vec *dstBase, u32 count);
void    PSMTXMultS16VecArray  ( const Mtx m, const S16Vec *srcBase, Vec *dstBase, u32 count );
void    PSMTXROMultS16VecArray( const ROMtx m, const S16Vec *srcBase, Vec *dstBase, u32 count );
#endif


/*---------------------------------------------------------------------------*
    MATRIX STACK SECTION
 *---------------------------------------------------------------------------*/
void    MTXInitStack          ( MtxStack *sPtr, u32 numMtx );
MtxPtr  MTXPush               ( MtxStack *sPtr, const Mtx m );
MtxPtr  MTXPushFwd            ( MtxStack *sPtr, const Mtx m );
MtxPtr  MTXPushInv            ( MtxStack *sPtr, const Mtx m );
MtxPtr  MTXPushInvXpose       ( MtxStack *sPtr, const Mtx m );
MtxPtr  MTXPop                ( MtxStack *sPtr );
MtxPtr  MTXGetStackPtr        ( const MtxStack *sPtr );


// macro to create a matrix stack.
// the macro exposes the  use of OSAlloc() for
// ease of replacement if desired.
#define MTXAllocStack( sPtr, numMtx ) (  ((MtxStackPtr)(sPtr))->stackBase = (MtxPtr)OSAlloc( ( (numMtx) * sizeof(Mtx) ) )  )
 
// macro to free a matrix stack
#define MTXFreeStack( sPtr )    (  OSFree( (void*)( ((MtxStackPtr)(sPtr))->stackBase ) )  )
 

/*---------------------------------------------------------------------------*/


#ifdef __cplusplus
}
#endif

#endif // __MTX_H__

/*===========================================================================*/