230 likes | 245 Views
Math Engine. CIS 488/588 Bruce R. Maxim UM-Dearborn. Overview. T3DLIB1.CPP/ T3DLIB1. H globals T3DLIB2.CPP/ T3DLIB2. H I/O, joystick, music, keyboard T3DLIB3.CPP/ T3DLIB3. H sound, music T3DLIB4.CPP/ T3DLIB4. H math engine (depends on T3DLIB1) External Libraries
E N D
Math Engine CIS 488/588 Bruce R. Maxim UM-Dearborn
Overview • T3DLIB1.CPP/ T3DLIB1. H • globals • T3DLIB2.CPP/ T3DLIB2. H • I/O, joystick, music, keyboard • T3DLIB3.CPP/ T3DLIB3. H • sound, music • T3DLIB4.CPP/ T3DLIB4. H • math engine (depends on T3DLIB1) • External Libraries • DDRAW.LIB, DSOUND.LIB, DINPUT.LIB, DINPUT8.LIB
Conventions Used • When functions are updated without changing the interface number added to name (e.g. Multiply2( ) ) • 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)
Some Types and Constants // defines for small numbers #define EPSILON_E3 (float)(1E-3) #define EPSILON_E4 (float)(1E-4) #define EPSILON_E5 (float)(1E-5) #define EPSILON_E6 (float)(1E-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 FIXP16; typedef int *FIXP16_PTR;
Data Types - 1 // 3D vector, point without the w typedef struct VECTOR3D_TYP { union { float M[3]; // array indexed storage // explicit names struct { float x,y,z; }; // end struct }; // end union } VECTOR3D, POINT3D, *VECTOR3D_PTR, *POINT3D_PTR;
Data Types - 2 // 4D homogenous vector, point with w typedef struct VECTOR4D_TYP { union { float M[4]; // array indexed storage // explicit names struct { float x,y,z,w; }; // end struct }; // end union } VECTOR4D, POINT4D, *VECTOR4D_PTR, *POINT4D_PTR;
Data Types - 3 // 3D parametric line typedef struct PARMLINE3D_TYP { POINT3D p0; // start point POINT3D p1; // end point VECTOR3D v; // direction vector of line segment // |v|=|p0->p1| } PARMLINE3D, *PARMLINE3D_PTR;
Data Types - 4 // 3D plane typedef struct PLANE3D_TYP { POINT3D p0; // point on the plane VECTOR3D n; // normal to the plane // (not necessarily a unit vector) } PLANE3D, *PLANE3D_PTR;
Data Types - 5 // 2x2 matrix (also need 1x2,1x3,3x2,3x3,1x4,4x4,4x3) typedef struct MATRIX2X2_TYP { union { float M[2][2]; // array indexed data storage struct // explicit names – row major storage { float M00, M01; float M10, M11; }; // end explicit names }; // end union } MATRIX2X2, *MATRIX2X2_PTR;
Data Types - 6 typedef struct QUAT_TYP // 4d quaternion { union { // array indexed storage w,x,y,z order float M[4]; // vector part, real part format struct { float q0; // the real part VECTOR3D qv; // the imaginary part xi+yj+zk }; struct {float w,x,y,z;}; }; // end union } QUAT, *QUAT_PTR;
Quaternion Access QUAT q = {1, 1, 2, 3}; q.w = 5; // access with explicit name q.q0 = 5; // access with real/float vector name q.M[0] = 5; // access with array name
Data Types - 6 // 3D cylindrical coordinates typedef struct CYLINDRICAL3D_TYP { float r; // radi of the point float theta; // angle in degrees about z axis float z; // the z-height of the point } CYLINDRICAL3D, *CYLINDRICAL3D_PTR;
Data Types - 7 // 3D spherical coordinates typedef struct SPHERICAL3D_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 } SPHERICAL3D, *SPHERICAL3D_PTR;
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
Macros and In-line Functions - 2 // convert integer and float to fixed point 16.16 #define INT_TO_FIXP16(i) ((i) << FIXP16_SHIFT) #define FLOAT_TO_FIXP16(f) (((float)(f) * (float)FIXP16_MAG+0.5)) // convert fixed point to float #define FIXP16_TO_FLOAT(fp) (((float)fp)/FIXP16_MAG) // extract whole part and decimal part from a // fixed point 16.16 #define FIXP16_WP(fp) ((fp) >> FIXP16_SHIFT) #define FIXP16_DP(fp) ((fp) && FIXP16_DP_MASK)
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; }
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
Only Globals Used // from T3DLIB1.CPP and T3DLIB1.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 extern float cos_look[361]; // 0 to 360 inclusive
Fast_Sin // from T3DLIB.CPP and T3DLIB.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 extern float cos_look[361]; // 0 to 360 inclusive // Fast_Sin and Fast_Cos are also defined in // T3DLIB.CPP and T3DLIB.H using degree values // for theta float Fast_Sin(float theta); float Fast_Cos(float theta);
Other Routine • Fixed-point mathematics support comes from T3DLIB1.CPP and T3DLIB1.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)
Using the Math Engine • The new Game Console is TD3DCONSOLE3.CPP • The only change from TD3DCONSOLE2.CPP is the addition of the include #include “T3DLIB4.h” • There are lots of includes in the older version of the Game Console
Demo Program • Use text interfaces to exercise the following processes • 2D parametric line intersection tester • 3D half-space tester • 3D parametric line intersection tester • Quaternion operations • Fixed-point number operations • 2D Linear System Solver