ULRE/inc/hgl/math/Matrix.h

159 lines
5.1 KiB
C
Raw Normal View History

2019-04-24 00:35:56 +08:00
#ifndef HGL_ALGORITHM_MATH_VECTOR_MATRIX_INCLUDE
#define HGL_ALGORITHM_MATH_VECTOR_MATRIX_INCLUDE
#include<hgl/math/Vector.h>
2019-05-21 21:28:33 +08:00
#include<hgl/TypeFunc.h>
2019-04-24 00:35:56 +08:00
//注GLM/CML(OpenGLMode)是列矩阵,计算坐标matrix*pos
// 而MGL是行矩阵需要反过来pos*matrix
namespace hgl
{
using Matrix3f=float3x3;
using Matrix4f=float4x4;
struct WorldMatrix
{
2019-07-17 04:49:49 +08:00
alignas(8) Vector2f vp_size; //viewport尺寸
alignas(16) Matrix4f ortho; //2D正角视图矩阵
2019-06-20 21:41:21 +08:00
alignas(16) Matrix4f projection;
// alignas(16) Matrix4f inverse_projection;
alignas(16) Matrix4f modelview;
alignas(16) Matrix4f mvp;
alignas(16) Vector4f view_pos; //眼睛坐标
2019-06-25 01:36:47 +08:00
};//struct WorldMatrix
2019-04-24 00:35:56 +08:00
inline Matrix4f identity()
{
return Matrix4f::identity;
}
inline Matrix4f inverse(const Matrix4f &m)
{
return m.Inverted();
}
inline Matrix4f ortho( float left,
float right,
float bottom,
float top,
float znear,
float zfar )
{
return Matrix4f(
2.0f / (right - left), 0.0f, 0.0f, -(right + left) / (right - left),
0.0f, 2.0f / (bottom - top), 0.0f, -(bottom + top) / (bottom - top),
0.0f, 0.0f, 1.0f / (znear - zfar), znear / (znear - zfar),
0.0f, 0.0f, 0.0f, 1.0f);
}
/**
*
* @param width
* @param height
* @param znear z值
* @param zfar z值
*/
inline Matrix4f ortho(float width,float height,float znear,float zfar)
{
return Matrix4f(
2.0f / width, 0.0f, 0.0f, -1,
0.0f, 2.0f / height, 0.0f, -1,
0.0f, 0.0f, 1.0f / (znear - zfar), znear / (znear - zfar),
0.0f, 0.0f, 0.0f, 1.0f);
}
/**
*
* @param width
* @param height
*/
inline Matrix4f ortho(float width,float height)
{
return Matrix4f(
2.0f / width, 0.0f, 0.0f, -1,
0.0f, 2.0f / height, 0.0f, -1,
0.0f, 0.0f, -1.0f , 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
/**
*
* @param aspect_ratio
* @param field_of_view
* @param znear
* @param zfar
*/
inline Matrix4f perspective(float field_of_view,
float aspect_ratio,
2019-04-24 00:35:56 +08:00
float znear,
float zfar)
{
const float f = 1.0f / tan( hgl_ang2rad( 0.5f * field_of_view ) );
// float scaleX, shearXy, shearXz, x;
//float shearYx, scaleY, shearYz, y;
//float shearZx, shearZy, scaleZ, z;
//float shearWx, shearWy, shearWz, w;
2019-04-24 00:35:56 +08:00
return Matrix4f(
f / aspect_ratio, 0.0f, 0.0f, 0.0f,
0.0f, -f, 0.0f, 0.0f,
0.0f, 0.0f, zfar / (znear - zfar), (znear * zfar) / (znear - zfar),
2019-07-12 10:13:01 +08:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 某些引擎这两项会乘0.5,那是因为他们是 -1 to 1 的Z值设定而我们是0 to 1所以这里不用乘
// 同理camera的znear为接近0的正数zfar为一个较大的正数默认使用16/256
2019-07-12 10:13:01 +08:00
2019-04-24 00:35:56 +08:00
0.0f, 0.0f, -1.0f, 0.0f);
}
inline Matrix4f translate(const Vector3f &v)
{
return Matrix4f::Translate(v);
}
inline Matrix4f translate(float x,float y,float z)
{
return Matrix4f::Translate(x,y,z);
}
inline Matrix4f scale(const Vector3f &v)
{
return Matrix4f::Scale(v,Vector3f::zero);
}
inline Matrix4f scale(float x,float y,float z)
{
return Matrix4f::Scale(Vector3f(x,y,z),Vector3f::zero);
}
inline Matrix4f scale(float s)
{
return Matrix4f::Scale(Vector3f(s,s,s),Vector3f::zero);
}
inline Matrix4f rotate(float angle,const Vector3f &axis)
{
return Matrix4f::RotateAxisAngle(axis.Normalized(),angle);
}
inline Matrix4f rotate(float angle,float x,float y,float z)
{
return rotate(angle,Vector3f(x,y,z));
}
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 result.xyz();
}
}//namespace hgl
#endif//HGL_ALGORITHM_MATH_VECTOR_MATRIX_INCLUDE