CMCore/inc/hgl/math/Matrix.h

225 lines
6.2 KiB
C
Raw Normal View History

2019-08-19 19:19:58 +08:00
#ifndef HGL_ALGORITHM_MATH_VECTOR_MATRIX_INCLUDE
#define HGL_ALGORITHM_MATH_VECTOR_MATRIX_INCLUDE
#include<hgl/math/Vector.h>
#include<hgl/TypeFunc.h>
//注GLM/CML(OpenGLMode)是列矩阵,计算坐标matrix*pos
// 而MGL是行矩阵需要反过来pos*matrix
2021-09-24 01:51:32 +08:00
#include<glm/gtc/matrix_transform.hpp>
#include<glm/gtc/quaternion.hpp>
#include<glm/gtc/constants.hpp>
#include<glm/gtx/quaternion.hpp>
2021-09-24 01:51:32 +08:00
2019-08-19 19:19:58 +08:00
namespace hgl
{
using Quatf=glm::quat;
constexpr const Quatf IdentityQuatf=Quatf(1,0,0,0);
#define DEFINE_MATRIX(num) using Matrix##num##f=glm::mat##num; \
constexpr const Matrix##num##f Identity##num##f=Matrix##num##f(1.0f); \
2024-08-04 00:56:35 +08:00
inline bool IsIdentityMatrix(const Matrix##num##f &m){return(hgl_cmp(m,Identity##num##f)==0);} \
inline int FastMatrixComp(const Matrix##num##f &m1,const Matrix##num##f &m2){return hgl_cmp(m1,m2);}
DEFINE_MATRIX(2)
DEFINE_MATRIX(3)
DEFINE_MATRIX(4)
DEFINE_MATRIX(2x4)
DEFINE_MATRIX(3x4)
DEFINE_MATRIX(4x2)
DEFINE_MATRIX(4x3)
2024-08-04 00:56:35 +08:00
//#undef DEFINE_MATRIX
2019-08-19 19:19:58 +08:00
inline Matrix4f inverse(const Matrix4f &m)
{
2021-09-24 01:51:32 +08:00
return glm::inverse(m);
}
inline Matrix4f transpose(const Matrix4f &m)
{
return glm::transpose(m);
2019-08-19 19:19:58 +08:00
}
/**
*
* @param left
* @param right
* @param top
* @param bottom
* @param znear z值
* @param zfar z值
*/
Matrix4f ortho( float left,
float right,
float bottom,
float top,
float znear,
float zfar );
2019-08-19 19:19:58 +08:00
/**
*
* @param width
* @param height
* @param znear z值
* @param zfar z值
*/
Matrix4f ortho(float width,float height,float znear,float zfar);
2019-08-19 19:19:58 +08:00
/**
*
* @param width
* @param height
*/
Matrix4f ortho(float width,float height);
2019-08-19 19:19:58 +08:00
/**
*
* @param aspect_ratio
* @param field_of_view
* @param znear
* @param zfar
*/
Matrix4f perspective( float field_of_view,
float aspect_ratio,
float znear,
float zfar);
/**
* lookat变换矩阵
* @param eye
* @param target
* @param up
*/
Matrix4f lookat(const Vector3f &eye,const Vector3f &target,const Vector3f &up);
2019-08-19 19:19:58 +08:00
inline Matrix4f translate(const Vector3f &v)
{
2021-09-24 01:51:32 +08:00
return glm::translate(Matrix4f(1.0f),v);
2019-08-19 19:19:58 +08:00
}
inline Matrix4f translate(float x,float y,float z)
{
2021-09-24 01:51:32 +08:00
return glm::translate(Matrix4f(1.0f),Vector3f(x,y,z));
2019-08-19 19:19:58 +08:00
}
inline Matrix4f translate(float x,float y)
{
2021-09-24 01:51:32 +08:00
return translate(x,y,1.0f);
}
2019-08-19 19:19:58 +08:00
inline Matrix4f scale(const Vector3f &v)
{
2021-09-24 01:51:32 +08:00
return glm::scale(Matrix4f(1.0f),v);
2019-08-19 19:19:58 +08:00
}
inline Matrix4f scale(float x,float y,float z)
{
2021-09-24 01:51:32 +08:00
return glm::scale(Matrix4f(1.0f),Vector3f(x,y,z));
2019-08-19 19:19:58 +08:00
}
inline Matrix4f scale(float x,float y)
{
2021-09-24 01:51:32 +08:00
return scale(x,y,1.0f);
}
2019-08-19 19:19:58 +08:00
inline Matrix4f scale(float s)
{
2021-09-24 01:51:32 +08:00
return glm::scale(Matrix4f(1.0f),Vector3f(s,s,s));
2019-08-19 19:19:58 +08:00
}
inline Matrix4f rotate(float angle,const Vector3f &axis)
{
2021-09-24 01:51:32 +08:00
return glm::rotate(Matrix4f(1.0f),angle,axis);
2019-08-19 19:19:58 +08:00
}
inline Matrix4f rotate(float angle,float x,float y,float z)
{
2021-09-24 01:51:32 +08:00
return glm::rotate(Matrix4f(1.0f),angle,Vector3f(x,y,z));
2019-08-19 19:19:58 +08:00
}
inline Matrix4f rotate(float angle,float x,float y)
{
2021-09-24 01:51:32 +08:00
return rotate(angle,x,y,1.0f);
}
2019-08-19 19:19:58 +08:00
inline Matrix4f rotate(float angle,const Vector4f &axis)
{
return rotate(angle,Vector3f(axis.x,axis.y,axis.z));
}
inline Vector3f rotate(const Vector3f &v3f,float angle,const Vector3f &axis)
{
Vector4f result = rotate(angle, axis)*Vector4f(v3f, 1.0f);
2019-08-19 19:19:58 +08:00
return Vector3f(result.x,result.y,result.z);
}
inline Quatf RotationQuat(const float angle,const Vector3f &axis)
{
return glm::angleAxis(glm::radians(angle),axis);
}
inline Matrix4f ToMatrix(const Quatf &quat)
{
return glm::toMat4(quat);
}
inline void ExtractedQuat(const Quatf &quat,Vector3f &axis,float &angle)
{
angle=glm::degrees(glm::angle(quat));
axis=glm::axis(quat);
}
inline Quatf LerpQuat(const Quatf &from,const Quatf &to,const float t)
{
return glm::lerp(from,to,t);
}
inline Quatf SLerpQuat(const Quatf &from,const Quatf &to,const float t)
{
return glm::slerp(from,to,t);
}
2024-07-29 13:38:25 +08:00
inline Vector3f TransformPosition(const Matrix4f &m,const Vector3f &v)
{
return Vector3f(m*Vector4f(v,1.0f));
}
inline Vector3f TransformDirection(const Matrix4f &m,const Vector3f &v)
{
return Vector3f(m*Vector4f(v,0.0f));
}
inline Vector3f TransformNormal(const Matrix4f &m,const Vector3f &v)
{
return normalize(Vector3f(transpose(inverse(m))*Vector4f(v,0.0f)));
}
inline Vector3f TransformNormal(const Matrix3f &m,const Vector3f &v)
{
return normalize(m*v);
2019-08-19 19:19:58 +08:00
}
inline Matrix3f TransformMatrix(const Matrix3f &root,const Matrix3f &child)
{
return root*child;
}
inline Matrix3f TransformMatrix(const Matrix4f &root,const Matrix3f &child)
{
return Matrix3f(root*Matrix4f(child));
}
inline Matrix4f TransformMatrix(const Matrix4f &root,const Matrix4f &child)
{
return root*child;
}
2024-08-02 22:39:21 +08:00
const Matrix4f GetRotateMatrix(const Vector3f &world_position,const Vector3f &old_direction,const Vector3f &new_direction);
const Quatf GetRotateQuat(const Vector3f &world_position,const Vector3f &old_direction,const Vector3f &new_direction);
2024-08-01 01:27:23 +08:00
// 函数用于从 glm::mat4 中提取平移、旋转和缩放
bool DecomposeTransform(const Matrix4f & transform, Vector3f & outTranslation, Quatf & outRotation, Vector3f & outScale);
2019-08-19 19:19:58 +08:00
}//namespace hgl
#endif//HGL_ALGORITHM_MATH_VECTOR_MATRIX_INCLUDE