Blade Math Lib数学库

>>最新源码发布

bmath2.h头文件

#ifndef   BMATH_H
#define   BMATH_H

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>

typedef float B_FLOAT;

const B_FLOAT B_PI = 3.1415926f;
const B_FLOAT B_DELAY = 0.00000001f;

const int CRASH_OUT = 0;
const int CRASH_IN  = 1;
const int CRASH_TOUCH = 2;

B_FLOAT BSqrt(B_FLOAT a);
B_FLOAT BPower(B_FLOAT a,int n);
B_FLOAT BRound(B_FLOAT val);

B_FLOAT* BAdd4(B_FLOAT* d,B_FLOAT* a,B_FLOAT* b);
B_FLOAT* BSub4(B_FLOAT* d,B_FLOAT* a,B_FLOAT* b);
B_FLOAT* BMul4(B_FLOAT* d,B_FLOAT* a,B_FLOAT b);
B_FLOAT* BDiv4(B_FLOAT* d,B_FLOAT* a,B_FLOAT b);
B_FLOAT* BSqrt4(B_FLOAT* d,B_FLOAT* a);


struct Vector2
{
	union
	{
		struct
		{
			B_FLOAT _x;
			B_FLOAT _y;
		};
		B_FLOAT _v[2];
	};
};

struct Vector3
{
	union
	{
		struct
		{
			B_FLOAT _x;
			B_FLOAT _y;
			B_FLOAT _z;
		};
		B_FLOAT _v[3];
	};
};

struct Vector4
{
	union
	{
		struct
		{
			B_FLOAT _x;
			B_FLOAT _y;
			B_FLOAT _z;
			B_FLOAT _w;
		};
		B_FLOAT _v[4];
	};
};

struct Matrix
{
	union
	{
		struct
		{
			B_FLOAT _11,_12,_13,_14;
			B_FLOAT _21,_22,_23,_24;
			B_FLOAT _31,_32,_33,_34;
			B_FLOAT _41,_42,_43,_44;
		};
		B_FLOAT bm[16];
	};
};

class BVector2:public Vector2
{
public:
	BVector2();
	BVector2(B_FLOAT x,B_FLOAT y);
	BVector2(B_FLOAT *v);

	BVector2  operator =( BVector2 v ) ;
	BVector2  operator =( B_FLOAT* v ) ;

	BVector2  operator +=( BVector2 v ) ;
	BVector2  operator -=( BVector2 v ) ;
	BVector2  operator *=( B_FLOAT v) ;
	BVector2  operator /=( B_FLOAT v) ;

	BVector2  operator +(BVector2 V);
	BVector2  operator -(BVector2 V);
	BVector2  operator *(B_FLOAT V);
	BVector2  operator /(B_FLOAT V);

	BVector2 operator +() ;
    BVector2 operator -() ;

	bool operator ==( BVector2 v) ;
    bool operator !=( BVector2 v) ;

};

class BVector3:public Vector3
{
public:
	BVector3();
	BVector3(B_FLOAT x,B_FLOAT y,B_FLOAT z);
	BVector3(B_FLOAT *v);

	BVector3  operator =( BVector3 v ) ;
	BVector3  operator =( B_FLOAT* v ) ;

	BVector3  operator +=( BVector3 v ) ;
	BVector3  operator -=( BVector3 v ) ;
	BVector3  operator *=( B_FLOAT v) ;
	BVector3  operator /=( B_FLOAT v) ;

	BVector3  operator +(BVector3 V);
	BVector3  operator -(BVector3 V);
	BVector3  operator *(B_FLOAT V);
	BVector3  operator /(B_FLOAT V);

	BVector3 operator +() ;
    BVector3 operator -() ;

	bool operator ==( BVector3 v) ;
    bool operator !=( BVector3 v) ;

};

class BVector4:public Vector4
{
public:
	BVector4();
	BVector4(B_FLOAT x,B_FLOAT y,B_FLOAT z,B_FLOAT w);
	BVector4(B_FLOAT *v);

	BVector4 operator =( BVector4 v ) ;
	BVector4 operator =( B_FLOAT* v ) ;

	BVector4  operator +=( BVector4 v ) ;
	BVector4  operator -=( BVector4 v ) ;
	BVector4  operator *=( B_FLOAT v) ;
	BVector4  operator /=( B_FLOAT v) ;

	BVector4  operator +(BVector4 V);
	BVector4  operator -(BVector4 V);
	BVector4  operator *(B_FLOAT V);
	BVector4  operator /(B_FLOAT V);

	BVector4 operator +() ;
    BVector4 operator -() ;

	bool operator ==( BVector4 v) ;
    bool operator !=( BVector4 v) ;

};

//Matrix 16*16

class BMatrix:public Matrix
{
public:
	BMatrix();
	BMatrix(B_FLOAT* val);

	BMatrix  operator +=( BMatrix m ) ;
	BMatrix  operator -=( BMatrix m ) ;
	BMatrix  operator *=( B_FLOAT v) ;
	BMatrix  operator /=( B_FLOAT v) ;

	BMatrix  operator +( BMatrix m ) ;
	BMatrix  operator -( BMatrix m ) ;
	BMatrix  operator *( B_FLOAT v) ;
	BMatrix  operator /( B_FLOAT v) ;

	bool operator == ( BMatrix m) ;
    bool operator != ( BMatrix m) ;

	B_FLOAT operator ()(int n);
	B_FLOAT operator ()(int row,int col);

	void	Identity();
	void	Inverse();

};

//QUATERNION
class BQuaternion:public Vector4
{
public:
	BQuaternion();
	BQuaternion(B_FLOAT* val);
	BQuaternion(B_FLOAT x,B_FLOAT y,B_FLOAT z,B_FLOAT w);

	BQuaternion operator +=(BQuaternion q);
	BQuaternion operator -=(BQuaternion q);
	BQuaternion operator *=(BQuaternion q);
	BQuaternion operator *=(B_FLOAT v);
	BQuaternion operator /=(B_FLOAT v);

	BQuaternion operator +();
	BQuaternion operator -();

	BQuaternion operator +(BQuaternion q);
	BQuaternion operator -(BQuaternion q);
	BQuaternion operator *(BQuaternion q);
	BQuaternion operator *(B_FLOAT v);
	BQuaternion operator /(B_FLOAT v);

	bool operator == ( BQuaternion q) ;
        bool operator != ( BQuaternion q) ;


};

//common
const char* GetVerID();
B_FLOAT     GetRandom();
long        GetTime();
B_FLOAT	    BSin(B_FLOAT val);
B_FLOAT	    BCos(B_FLOAT val);

BVector2*   BVec2Lerp(BVector2* dest,BVector2* src1,BVector2* src2,B_FLOAT s);
B_FLOAT	    BVec2LengthSq(BVector2* src);
B_FLOAT     BVec2DistanceSq(BVector2* src,BVector2* det);
BVector2*   BVec2Maximize(BVector2* dest,BVector2* src1,BVector2* src2);
BVector2*   BVec2Minimize(BVector2* dest,BVector2* src1,BVector2* src2);
BVector2*   BVec2Normalize(BVector2* dest,BVector2* src);
B_FLOAT	    BVec2Cross(BVector2* src1,BVector2* src2);
B_FLOAT	    BVec2Dot(BVector2* src1,BVector2* src2);
B_FLOAT     BVec2ComputAngle(BVector2* v1,BVector2* v2,BVector2* v3);

BVector3*   BVec3Lerp(BVector3* dest,BVector3* src1,BVector3* src2,B_FLOAT s);
B_FLOAT	    BVec3LengthSq(BVector3* src);
B_FLOAT     BVec3DistanceSq(BVector3* src,BVector3* det);
BVector3*   BVec3Maximize(BVector3* dest,BVector3* src1,BVector3* src2);
BVector3*   BVec3Minimize(BVector3* dest,BVector3* src1,BVector3* src2);
BVector3*   BVec3Normalize(BVector3* dest,BVector3* src);
BVector3*   BVec3Cross(BVector3* dest,BVector3* src1,BVector3* src2);
B_FLOAT	    BVec3Dot(BVector3* src1,BVector3* src2);
B_FLOAT     BVec3ComputAngle(BVector3* v1,BVector3* v2,BVector3* v3);

BVector4*   BVec4Lerp(BVector4* dest,BVector4* src1,BVector4* src2,B_FLOAT s);
B_FLOAT	    BVec4LengthSq(BVector4* src);
B_FLOAT     BVec4DistanceSq(BVector4* src,BVector4* det);
BVector4*   BVec4Maximize(BVector4* dest,BVector4* src1,BVector4* src2);
BVector4*   BVec4Minimize(BVector4* dest,BVector4* src1,BVector4* src2);
BVector4*   BVec4Normalize(BVector4* dest,BVector4* src);
BVector4*   BVec4Cross(BVector4* dest,BVector4* src1,BVector4* src2,BVector4* src3);
B_FLOAT	    BVec4Dot(BVector4* src1,BVector4* src2);

BVector4*   BVec2Transform(BVector4* dest,BVector2* src,BMatrix*  m);
BVector4*   BVec3Transform(BVector4* dest,BVector3* src,BMatrix*  m);
BVector4*   BVec4Transform(BVector4* dest,BVector4* src,BMatrix*  m);

B_FLOAT		BQuatLengthSq(BQuaternion* src);
BQuaternion*	BQuatNormalize(BQuaternion* dest,BQuaternion* src);
BQuaternion*	BQuatSlerp(BQuaternion* dest,BQuaternion* src1, BQuaternion* src2, B_FLOAT s,bool line=true);
BQuaternion*	BQuatRotationAxis(BQuaternion* dest,BVector3* pv,B_FLOAT angle);
void		MatToQuat(BQuaternion* dest,BMatrix* src);
void		QuatToMat(BMatrix* dest,BQuaternion* src);

BVector2*	BVec2BaryCentric(BVector2* dest,BVector2* point1,BVector2* point2,BVector2* point3,B_FLOAT f,B_FLOAT g);
BVector3*	BVec3BaryCentric(BVector3* dest,BVector3* point1,BVector3* point2,BVector3* point3,B_FLOAT f,B_FLOAT g);

BVector2*       BVec2CatmullRom(BVector2* dest,BVector2* src1,BVector2* src2,BVector2* src3,BVector2* src4,B_FLOAT s);
BVector3*       BVec3CatmullRom(BVector3* dest,BVector3* src1,BVector3* src2,BVector3* src3,BVector3* src4,B_FLOAT s);
BVector4*       BVec4CatmullRom(BVector4* dest,BVector4* src1,BVector4* src2,BVector4* src3,BVector4* src4,B_FLOAT s);

BVector2*       BVec2Hermite(BVector2* dest,BVector2* src1,BVector2* tan1,BVector2* src2,BVector2* tan2,B_FLOAT s);
BVector3*       BVec3Hermite(BVector3* desc,BVector3* src1,BVector3* tan1,BVector3* src2,BVector3* tan2,B_FLOAT s);
BVector4*       BVec4Hermite(BVector4* dest,BVector4* src1,BVector4* tan1,BVector4* src2,BVector4* tan2,B_FLOAT s);

BMatrix*	BMatrixLookAtRH(BMatrix* dest,BVector3* pEye,BVector3* pAt,BVector3* pUp);
BMatrix*	BMatrixLookAtLH(BMatrix* dest,BVector3* pEye,BVector3* pAt,BVector3* pUp);

BMatrix*	BMatrixOrthoLH(BMatrix* dest,B_FLOAT w,B_FLOAT h,B_FLOAT zn,B_FLOAT zf);
BMatrix*	BMatrixOrthoRH(BMatrix* dest,B_FLOAT w,B_FLOAT h,B_FLOAT zn,B_FLOAT zf);

BMatrix*	BMatrixPerspectiveLH(BMatrix* dest,B_FLOAT w,B_FLOAT h,B_FLOAT zn,B_FLOAT zf);
BMatrix*	BMatrixPerspectiveRH(BMatrix* dest,B_FLOAT w,B_FLOAT h,B_FLOAT zn,B_FLOAT zf);

BMatrix*	BMatrixPerspectiveFovLH(BMatrix* dest,B_FLOAT fovy,B_FLOAT aspect,B_FLOAT zn,B_FLOAT zf);
BMatrix*	BMatrixPerspectiveFovRH(BMatrix* dest,B_FLOAT fovy,B_FLOAT aspect,B_FLOAT zn,B_FLOAT zf);

BVector3*       BMatrixUnProj(BVector3 pt,BMatrix* matView,BMatrix* matProj,int* viewport,BVector3* dest);

BMatrix*	BMatrixTranslation(BMatrix* dest,B_FLOAT x,B_FLOAT y,B_FLOAT z);
BMatrix*        BMatrixScaling(BMatrix* dest,B_FLOAT x,B_FLOAT y,B_FLOAT z);
BMatrix*	BMatrixRotateX(BMatrix* dest,B_FLOAT angle);
BMatrix*	BMatrixRotateY(BMatrix* dest,B_FLOAT angle);
BMatrix*	BMatrixRotateZ(BMatrix* dest,B_FLOAT angle);
BMatrix*        BMatrixMultiply(BMatrix* dest,BMatrix* src1,BMatrix* src2);

BMatrix*	BMatrixTranspose(BMatrix* dest,BMatrix* src);
BMatrix*	BMatrixInverse(BMatrix* dest,BMatrix* src);

bool		BRay2Triangle(BVector3 pStart,BVector3 pEnd,BVector3 p1,BVector3 p2,BVector3 p3,B_FLOAT* len);
bool            BRay2Quad(BVector3 pStart,BVector3 pEnd,BVector3 p1,BVector3 p2,BVector3 p3,BVector3 p4,B_FLOAT* len);
bool            BRay2Box(BVector3 pStart,BVector3 pEnd,BVector3 MinVector,BVector3 MaxVector,B_FLOAT* len,int* style);

int	        CrashQuad2Quad(BVector2 v1,BVector2 v2,BVector2 m1,BVector2 m2);
int             CrashBox2Box(BVector3 Min1,BVector3 Max1,BVector3 Min2,BVector3 Max2);
bool            CrashTriangle(BVector3 a1,BVector3 a2,BVector3 a3,BVector3 b1,BVector3 b2,BVector3 b3);

BVector3        GetRayPos(BVector3 pStart,BVector3 pEnd,B_FLOAT v);

#endif
滚动到顶部