⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t3dlib4.h

📁 3D游戏编程大师技巧第十一章的源代码
💻 H
📖 第 1 页 / 共 2 页
字号:
// T3DLIB4.H - Header file for T3DLIB4.CPP game engine library

// watch for multiple inclusions
#ifndef T3DLIB4
#define T3DLIB4

// DEFINES & 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

// TYPES //////////////////////////////////////////////////

// 2x2 matrix /////////////////////////////////////////////
typedef struct MATRIX2X2_TYP
{
union
    {
    float M[2][2]; // array indexed data storage

    // storage in row major form with explicit names
    struct
         {
         float M00, M01;
         float M10, M11;
         }; // end explicit names

     }; // end union
} MATRIX2X2, *MATRIX2X2_PTR;

// 1x4 matrix /////////////////////////////////////////////
typedef struct MATRIX1X4_TYP
{
union
    {
    float M[4]; // array indexed data storage

    // storage in row major form with explicit names
    struct
         {
         float M00, M01, M02, M03;
         }; // end explicit names

     }; // end union
} MATRIX1X4, *MATRIX1X4_PTR;

// 4x4 matrix /////////////////////////////////////////////
typedef struct MATRIX4X4_TYP
{
union
    {
    float M[4][4]; // array indexed data storage

    // storage in row major form with explicit names
    struct
         {
         float M00, M01, M02, M03;
         float M10, M11, M12, M13;
         float M20, M21, M22, M23;
         float M30, M31, M32, M33;
         }; // end explicit names

    }; // end union

} MATRIX4X4, *MATRIX4X4_PTR;

// 4x3 matrix /////////////////////////////////////////////
typedef struct MATRIX4X3_TYP
{
union
    {
    float M[4][3]; // array indexed data storage

    // storage in row major form with explicit names
    struct
         {
         float M00, M01, M02;
         float M10, M11, M12;
         float M20, M21, M22;
         float M30, M31, M32;
         }; // end explicit names

    }; // end union

} MATRIX4X3, *MATRIX4X3_PTR;

// vector types ///////////////////////////////////////////

// 2D vector, point without the w ////////////////////////
typedef struct VECTOR2D_TYP
{
union
    {
    float M[2]; // array indexed storage

    // explicit names
    struct
         {
         float x,y;
         }; // end struct

    }; // end union

} VECTOR2D, POINT2D, *VECTOR2D_PTR, *POINT2D_PTR;

// 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;

// 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;

// 2D parametric line /////////////////////////////////////////
typedef struct PARMLINE2D_TYP
{
POINT2D  p0; // start point of parametric line
POINT2D  p1; // end point of parametric line
VECTOR2D v;  // direction vector of line segment
             // |v|=|p0->p1|
} PARMLINE2D, *PARMLINE2D_PTR;

// 3D parametric line /////////////////////////////////////////
typedef struct PARMLINE3D_TYP
{
POINT3D  p0; // start point of parametric line
POINT3D  p1; // end point of parametric line
VECTOR3D v;  // direction vector of line segment
             // |v|=|p0->p1|
} PARMLINE3D, *PARMLINE3D_PTR;

// 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;

// 2D polar coordinates ///////////////////////////////////////
typedef struct POLAR2D_TYP
{
float r;     // the radi of the point
float theta; // the angle in rads
} POLAR2D, *POLAR2D_PTR;

// 3D cylindrical coordinates ////////////////////////////////
typedef struct CYLINDRICAL3D_TYP
{
float r;     // the radi of the point
float theta; // the angle in degrees about the z axis
float z;     // the z-height of the point
} CYLINDRICAL3D, *CYLINDRICAL3D_PTR;

// 3D spherical coordinates //////////////////////////////////
typedef struct SPHERICAL3D_TYP
{
float p;      // rho, the distance to the point from the origin
float theta;  // the angle from the z-axis and the line segment o->p
float phi;    // the angle from the projection if o->p onto the x-y 
              // plane and the x-axis
} SPHERICAL3D, *SPHERICAL3D_PTR;

// 4d quaternion ////////////////////////////////////////////
// note the union gives us a number of ways to work with
// the components of the quaternion
typedef struct QUAT_TYP
{
union
    {
    float M[4]; // array indexed storage w,x,y,z order

    // 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;

// fixed point types //////////////////////////////////////////

typedef int FIXP16;
typedef int *FIXP16_PTR;

///////////////////////////////////////////////////////////////

// FORWARD REF DEFINES & CONSTANTS ////////////////////////////

// identity matrices

// 4x4 identity matrix
const MATRIX4X4 IMAT_4X4 = {1,0,0,0, 
                            0,1,0,0, 
                            0,0,1,0, 
                            0,0,0,1};

// 4x3 identity matrix (note this is not correct mathematically)
// but later we may use 4x3 matrices with the assumption that 
// the last column is always [0 0 0 1]t
const MATRIX4X3 IMAT_4X3 = {1,0,0, 
                            0,1,0, 
                            0,0,1, 
                            0,0,0,};


// 3x3 identity matrix
const MATRIX3X3 IMAT_3X3 = {1,0,0, 
                            0,1,0, 
                            0,0,1};

// 2x2 identity matrix
const MATRIX2X2 IMAT_2X2 = {1,0, 
                            0,1};

// MACROS, SMALL INLINE FUNCS /////////////////////////////////

// matrix macros

// macros to clear out matrices
#define MAT_ZERO_2X2(m) {memset((void *)(m), 0, sizeof(MATRIX2X2));}
#define MAT_ZERO_3X3(m) {memset((void *)(m), 0, sizeof(MATRIX3X3));}
#define MAT_ZERO_4X4(m) {memset((void *)(m), 0, sizeof(MATRIX4X4));}
#define MAT_ZERO_4X3(m) {memset((void *)(m), 0, sizeof(MATRIX4X3));}

// macros to set the identity matrix
#define MAT_IDENTITY_2X2(m) {memcpy((void *)(m), (void *)&IMAT_2X2, sizeof(MATRIX2X2));}
#define MAT_IDENTITY_3X3(m) {memcpy((void *)(m), (void *)&IMAT_3X3, sizeof(MATRIX3X3));}
#define MAT_IDENTITY_4X4(m) {memcpy((void *)(m), (void *)&IMAT_4X4, sizeof(MATRIX4X4));}
#define MAT_IDENTITY_4X3(m) {memcpy((void *)(m), (void *)&IMAT_4X3, sizeof(MATRIX4X3));}

// matrix copying macros
#define MAT_COPY_2X2(src_mat, dest_mat) {memcpy((void *)(dest_mat), (void *)(src_mat), sizeof(MATRIX2X2) ); }
#define MAT_COPY_3X3(src_mat, dest_mat) {memcpy((void *)(dest_mat), (void *)(src_mat), sizeof(MATRIX3X3) ); }
#define MAT_COPY_4X4(src_mat, dest_mat) {memcpy((void *)(dest_mat), (void *)(src_mat), sizeof(MATRIX4X4) ); }
#define MAT_COPY_4X3(src_mat, dest_mat) {memcpy((void *)(dest_mat), (void *)(src_mat), sizeof(MATRIX4X3) ); }

// matrix transposing macros
inline void MAT_TRANSPOSE_3X3(MATRIX3X3_PTR m) 
{ MATRIX3X3 mt;
mt.M00 = m->M00; mt.M01 = m->M10; mt.M02 = m->M20;
mt.M10 = m->M01; mt.M11 = m->M11; mt.M12 = m->M21;
mt.M20 = m->M02; mt.M21 = m->M12; mt.M22 = m->M22;
memcpy((void *)m,(void *)&mt, sizeof(MATRIX3X3)); }

inline void MAT_TRANSPOSE_4X4(MATRIX4X4_PTR m) 
{ MATRIX4X4 mt;
mt.M00 = m->M00; mt.M01 = m->M10; mt.M02 = m->M20; mt.M03 = m->M30; 
mt.M10 = m->M01; mt.M11 = m->M11; mt.M12 = m->M21; mt.M13 = m->M31; 
mt.M20 = m->M02; mt.M21 = m->M12; mt.M22 = m->M22; mt.M23 = m->M32; 
mt.M30 = m->M03; mt.M31 = m->M13; mt.M32 = m->M22; mt.M33 = m->M33; 
memcpy((void *)m,(void *)&mt, sizeof(MATRIX4X4)); }

inline void MAT_TRANSPOSE_3X3(MATRIX3X3_PTR m, MATRIX3X3_PTR mt) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -