CMCore/inc/hgl/math/Matrix.h
2022-03-09 18:39:39 +08:00

134 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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
#include<glm/gtc/matrix_transform.hpp>
namespace hgl
{
using Matrix3f=glm::mat3;
using Matrix4f=glm::mat4;
using Matrix3x4f=glm::mat3x4;
inline Matrix4f inverse(const Matrix4f &m)
{
return glm::inverse(m);
}
inline Matrix4f transpose(const Matrix4f &m)
{
return glm::transpose(m);
}
/**
* 生成一个正角视图矩阵
* @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 );
/**
* 生成一个正角视图矩阵
* @param width 宽
* @param height 高
* @param znear 近平面z值
* @param zfar 远平台z值
*/
Matrix4f ortho(float width,float height,float znear,float zfar);
/**
* 生成一个正角视图矩阵
* @param width 宽
* @param height 高
*/
Matrix4f ortho(float width,float height);
/**
* 生成一个透视矩阵
* @param aspect_ratio 宽高比
* @param field_of_view 视野
* @param znear 近截面
* @param zfar 远截面
*/
Matrix4f perspective( float field_of_view,
float aspect_ratio,
float znear,
float zfar);
inline Matrix4f translate(const Vector3f &v)
{
return glm::translate(Matrix4f(1.0f),v);
}
inline Matrix4f translate(float x,float y,float z)
{
return glm::translate(Matrix4f(1.0f),Vector3f(x,y,z));
}
inline Matrix4f translate(float x,float y)
{
return translate(x,y,1.0f);
}
inline Matrix4f scale(const Vector3f &v)
{
return glm::scale(Matrix4f(1.0f),v);
}
inline Matrix4f scale(float x,float y,float z)
{
return glm::scale(Matrix4f(1.0f),Vector3f(x,y,z));
}
inline Matrix4f scale(float x,float y)
{
return scale(x,y,1.0f);
}
inline Matrix4f scale(float s)
{
return glm::scale(Matrix4f(1.0f),Vector3f(s,s,s));
}
inline Matrix4f rotate(float angle,const Vector3f &axis)
{
return glm::rotate(Matrix4f(1.0f),angle,axis);
}
inline Matrix4f rotate(float angle,float x,float y,float z)
{
return glm::rotate(Matrix4f(1.0f),angle,Vector3f(x,y,z));
}
inline Matrix4f rotate(float angle,float x,float y)
{
return rotate(angle,x,y,1.0f);
}
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);
return Vector3f(result.x,result.y,result.z);
}
}//namespace hgl
#endif//HGL_ALGORITHM_MATH_VECTOR_MATRIX_INCLUDE