#ifndef MATH_H_#define MATH_H_#include <math.h>#define PI (3.141592654f)#define RADIAN_TO_DEGREE(r) ((r) * 180.0f / PI)//弧度到角度#define DEGREE_TO_RADIAN(a) ((a) * PI / 180.0f)//角度到弧度//4D向量(齊次座標)struct Vector4{union{float M[4] ;struct{float x ;float y ;float z ;float w ;} ;} ;} ;//3D向量(座標)struct Vector3{union{float M[3] ;struct{float x ;float y ;float z ;} ;} ;} ;void VectorPlusVector(Vector3* lhs, Vector3* rhs, Vector3* pResult) ;void VectorMinusVector(Vector3* endPoint, Vector3* startPoint, Vector3* pResult) ;void VectorCrossVector(Vector3* lhs, Vector3* rhs, Vector3* pResult) ;float VectorDotVector(Vector3* lhs, Vector3* rhs) ;void Vector4ToVector3(Vector4* resourcePoint, Vector3* newPoint) ;//平面struct Plane{float distance ;Vector3 normal ;} ;//多邊形struct Poly{Vector4* pVertexList ;int vertexIndexArray[3] ;} ;//自包含頂點多邊形struct PolySelfContained{Vector4 vertexList[3] ;bool removed ;} ;//3X3矩陣struct Matrix3X3{union{float M[3][3] ;struct{float m11, m12, m13 ;float m21, m22, m23 ;float m31, m32, m33 ;} ;} ;} ;void InitializeMatrix(Matrix3X3* pMatrix,float m11, float m12, float m13,float m21, float m22, float m23,float m31, float m32, float m33) ;//4X4矩陣struct Matrix4X4{union{float M[4][4] ;struct{float m11, m12, m13, m14 ;float m21, m22, m23, m24 ;float m31, m32, m33, m34 ;float m41, m42, m43, m44 ;} ;} ;} ;void InitializeMatrix(Matrix4X4* pMatrix,float m11, float m12, float m13, float m14,float m21, float m22, float m23, float m24,float m31, float m32, float m33, float m34,float m41, float m42, float m43, float m44) ;void VectorMultMatrix(Vector4* point, Matrix4X4* matrix, Vector4* pResult) ;void MatrixMultMatrix(Matrix4X4* lhs, Matrix4X4* rhs, Matrix4X4* pResult) ;float GetDeterminant(const Matrix4X4* pMatrix) ;class Cube{public:Vector4 position ;//位置Vector4 localVertices[8] ;//局部頂點座標數組Vector4 transformedVertices[8] ;//轉換後頂點座標數組Poly polygones[12] ;//多邊形數組int vertexCount ;//頂點數量int polyCount ;//多邊形數量Cube(float x, float y, float z) ;void RotateAroundY(float unit) ;void RotateAroundX(float unit) ;void RotateAroundZ(float unit) ;} ;//沿任意軸縮放(pAxis是正常化向量)void ScaleAccordingTo(Cube* pCube, Vector3* pAxis, float k) ;//將產生立方體全局座標void ModelToWorld(Cube* pCube) ;//背面消除void RemoveBackface(PolySelfContained* polyList, int count, Vector3* pViewPortPosition) ;//全局座標到相機座標void WorldToCamera(PolySelfContained* polyList, int count, Matrix4X4* pMatrix) ;//相機座標到裁剪座標void CameraToClip(PolySelfContained* polyList, int count, Matrix4X4* pMatrix) ;//裁剪座標到投影座標void ClipToPerspective(PolySelfContained* polyList, int count, float nearDistance) ;//相機座標到透視座標//沒有對Z值進行檢測,在沒有裁剪的情況下要當心void CameraToperspective(PolySelfContained* polyList, int count, float viewPortWidth, float viewPortHeight, float fov) ;//透視座標到視口座標void PerspectiveToScreen(PolySelfContained* polyList, int count, float viewPortWidth, float viewPortHeight) ;#endif