Math Engine CIS 488588 Bruce R Maxim UMDearborn
Math Engine CIS 488/588 Bruce R. Maxim UM-Dearborn 10/24/2021 1
Overview • T 3 DLIB 1. CPP/ T 3 DLIB 1. H – globals • T 3 DLIB 2. CPP/ T 3 DLIB 2. H – I/O, joystick, music, keyboard • T 3 DLIB 3. CPP/ T 3 DLIB 3. H – sound, music • T 3 DLIB 4. CPP/ T 3 DLIB 4. H – math engine (depends on T 3 DLIB 1) • External Libraries – DDRAW. LIB, DSOUND. LIB, DINPUT 8. LIB 10/24/2021 2
Conventions Used • When functions are updated without changing the interface number added to name (e. g. Multiply 2( ) ) • Class/structure name will be used as part of the descriptive function name • Class/structure names and defines will be all uppercase • There is no exception handling in the library functions (its up to you to add it) 10/24/2021 3
From La. Mothe 10/24/2021 4
Some Types and Constants // defines for small numbers #define EPSILON_E 3 (float)(1 E-3) #define EPSILON_E 4 (float)(1 E-4) #define EPSILON_E 5 (float)(1 E-5) #define EPSILON_E 6 (float)(1 E-6) // defines for parametric line intersections #define PARM_LINE_NO_INTERSECT 0 #define PARM_LINE_INTERSECT_IN_SEGMENT 1 #define PARM_LINE_INTERSECT_OUT_SEGMENT 2 #define PARM_LINE_INTERSECT_EVERYWHERE 3 // fixed point types typedef int FIXP 16; typedef int *FIXP 16_PTR; 10/24/2021 5
Data Types - 1 // 3 D vector, point without the w typedef struct VECTOR 3 D_TYP { union { float M[3]; // array indexed storage // explicit names struct { float x, y, z; }; // end struct }; // end union } VECTOR 3 D, POINT 3 D, *VECTOR 3 D_PTR, *POINT 3 D_PTR; 10/24/2021 6
Data Types - 2 // 4 D homogenous vector, point with w typedef struct VECTOR 4 D_TYP { union { float M[4]; // array indexed storage // explicit names struct { float x, y, z, w; }; // end struct }; // end union } VECTOR 4 D, POINT 4 D, *VECTOR 4 D_PTR, *POINT 4 D_PTR; 10/24/2021 7
Data Types - 3 // 3 D parametric line typedef struct PARMLINE 3 D_TYP { POINT 3 D p 0; // start point POINT 3 D p 1; // end point VECTOR 3 D v; // direction vector of line segment // |v|=|p 0 ->p 1| } PARMLINE 3 D, *PARMLINE 3 D_PTR; 10/24/2021 8
Data Types - 4 // 3 D plane typedef struct PLANE 3 D_TYP { POINT 3 D p 0; // point on the plane VECTOR 3 D n; // normal to the plane // (not necessarily a unit vector) } PLANE 3 D, *PLANE 3 D_PTR; 10/24/2021 9
Data Types - 5 // 2 x 2 matrix (also need 1 x 2, 1 x 3, 3 x 2, 3 x 3, 1 x 4, 4 x 3) typedef struct MATRIX 2 X 2_TYP { union { float M[2][2]; // array indexed data storage struct // explicit names – row major storage { float M 00, M 01; float M 10, M 11; }; // end explicit names }; // end union } MATRIX 2 X 2, *MATRIX 2 X 2_PTR; 10/24/2021 10
Data Types - 6 typedef struct QUAT_TYP // 4 d quaternion { union { // array indexed storage w, x, y, z order float M[4]; // vector part, real part format struct { float q 0; // the real part VECTOR 3 D qv; // the imaginary part xi+yj+zk }; struct { float w, x, y, z; }; }; // end union } QUAT, *QUAT_PTR; 10/24/2021 11
Quaternion Access QUAT q = {1, 1, 2, 3}; q. w = 5; q. q 0 = 5; q. M[0] = 5; 10/24/2021 // access with explicit name // access with real/float vector name // access with array name 12
Data Types - 6 // 3 D cylindrical coordinates typedef struct CYLINDRICAL 3 D_TYP { float r; // radi of the point float theta; // angle in degrees about z axis float z; // the z-height of the point } CYLINDRICAL 3 D, *CYLINDRICAL 3 D_PTR; 10/24/2021 13
Data Types - 7 // 3 D spherical coordinates typedef struct SPHERICAL 3 D_TYP { float p; // rho, distance to point from origin float theta; // angle from z-axis and line // segment o->p float phi; // angle from projection of o->p // onto x-y plane and x-axis } SPHERICAL 3 D, *SPHERICAL 3 D_PTR; 10/24/2021 14
Macros and In-line Functions - 1 • Pages 384 -395 list several macros and in-line functions • The reason for going in this direction is to improve performance by eliminating the overhead associated with function calls • They improve the readability and write ability of the source code • The actual source code must be available at compile time to any function using them 10/24/2021 15
Macros and In-line Functions - 2 // convert integer and float to fixed point 16. 16 #define INT_TO_FIXP 16(i) ((i) << FIXP 16_SHIFT) #define FLOAT_TO_FIXP 16(f) (((float)(f) * (float)FIXP 16_MAG+0. 5)) // convert fixed point to float #define FIXP 16_TO_FLOAT(fp) (((float)fp)/FIXP 16_MAG) // extract whole part and decimal part from a // fixed point 16. 16 #define FIXP 16_WP(fp) ((fp) >> FIXP 16_SHIFT) #define FIXP 16_DP(fp) ((fp) && FIXP 16_DP_MASK) 10/24/2021 16
Macros and In-line Functions - 3 // some quaternion macros inline void QUAT_ZERO(QUAT_PTR q) {(q)->x = (q)->y = (q)->z = (q)->w = 0. 0; } inline void QUAT_INIT(QUAT_PTR qdst, QUAT_PTR qsrc) {(qdst)->w = (qsrc)->w; (qdst)->x = (qsrc)->x; (qdst)->y = (qsrc)->y; (qdst)->z = (qsrc)->z; } inline void QUAT_COPY(QUAT_PTR qdst, QUAT_PTR qsrc) {(qdst)->x = (qsrc)->x; (qdst)->y = (qsrc)->y; (qdst)->z = (qsrc)->z; (qdst)->w = (qsrc)->w; } 10/24/2021 17
Function Prototypes • Pages 395 -468 contains the function prototypes and Math engine API listing • There a dozens of prototypes and functions described in this section • Obviously this where functions for initializion, operations, and type conversions are defined for each data type 10/24/2021 18
Only Globals Used // from T 3 DLIB 1. CPP and T 3 DLIB 1. H // trig value look up tables populated by a // single call to Build_Sin_Cos_Tables( ) extern float cos_look[361]; // 0 to 360 inclusive 10/24/2021 19
Fast_Sin // from T 3 DLIB. CPP and T 3 DLIB. H // trig value look up tables populated by a // single call to Build_Sin_Cos_Tables( ) extern float cos_look[361]; // 0 to 360 inclusive // Fast_Sin and Fast_Cos are also defined in // T 3 DLIB. CPP and T 3 DLIB. H using degree values // for theta float Fast_Sin(float theta); float Fast_Cos(float theta); 10/24/2021 20
Other Routine • Fixed-point mathematics support comes from T 3 DLIB 1. CPP and T 3 DLIB 1. H and is reviewed on pages 458 -464 • The equation solving routines are new and appear on pages 464 -468 • FPU programming needs to be done using the in-line assembly language programming to hand-optimize code produced by VC++ (described on pages 468 -488) 10/24/2021 21
Using the Math Engine • The new Game Console is TD 3 DCONSOLE 3. CPP • The only change from TD 3 DCONSOLE 2. CPP is the addition of the include #include “T 3 DLIB 4. h” • There are lots of includes in the older version of the Game Console 10/24/2021 22
Demo Program • Use text interfaces to exercise the following processes – – – 2 D parametric line intersection tester 3 D half-space tester 3 D parametric line intersection tester Quaternion operations Fixed-point number operations 2 D Linear System Solver 10/24/2021 23
- Slides: 23