Math.h

#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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值