switch to glm

This commit is contained in:
2021-09-24 01:51:32 +08:00
parent 2493989c5e
commit e469041733
3 changed files with 96 additions and 100 deletions

View File

@@ -6,20 +6,26 @@
//注GLM/CML(OpenGLMode)是列矩阵,计算坐标matrix*pos
// 而MGL是行矩阵需要反过来pos*matrix
#include<glm/gtc/matrix_transform.hpp>
namespace hgl
{
using Matrix3f=float3x3;
using Matrix4f=float4x4;
using Matrix3x4f=float3x4;
using Matrix3f=glm::mat3;
using Matrix4f=glm::mat4;
inline Matrix4f identity()
{
return Matrix4f::identity;
return Matrix4f();
}
inline Matrix4f inverse(const Matrix4f &m)
{
return m.Inverted();
return glm::inverse(m);
}
inline Matrix4f transpose(const Matrix4f &m)
{
return glm::transpose(m);
}
/**
@@ -76,52 +82,52 @@ namespace hgl
inline Matrix4f translate(const Vector3f &v)
{
return Matrix4f::Translate(v);
return glm::translate(Matrix4f(1.0f),v);
}
inline Matrix4f translate(float x,float y,float z)
{
return Matrix4f::Translate(x,y,z);
return glm::translate(Matrix4f(1.0f),Vector3f(x,y,z));
}
inline Matrix4f translate(float x,float y)
{
return Matrix4f::Translate(x,y,1.0f);
return translate(x,y,1.0f);
}
inline Matrix4f scale(const Vector3f &v)
{
return Matrix4f::Scale(v,Vector3f::zero);
return glm::scale(Matrix4f(1.0f),v);
}
inline Matrix4f scale(float x,float y,float z)
{
return Matrix4f::Scale(Vector3f(x,y,z),Vector3f::zero);
return glm::scale(Matrix4f(1.0f),Vector3f(x,y,z));
}
inline Matrix4f scale(float x,float y)
{
return Matrix4f::Scale(Vector3f(x,y,1.0f),Vector3f::zero);
return scale(x,y,1.0f);
}
inline Matrix4f scale(float s)
{
return Matrix4f::Scale(Vector3f(s,s,s),Vector3f::zero);
return glm::scale(Matrix4f(1.0f),Vector3f(s,s,s));
}
inline Matrix4f rotate(float angle,const Vector3f &axis)
{
return Matrix4f::RotateAxisAngle(axis.Normalized(),angle);
return glm::rotate(Matrix4f(1.0f),angle,axis);
}
inline Matrix4f rotate(float angle,float x,float y,float z)
{
return rotate(angle,Vector3f(x,y,z));
return glm::rotate(Matrix4f(1.0f),angle,Vector3f(x,y,z));
}
inline Matrix4f rotate(float angle,float x,float y)
{
return rotate(angle,Vector3f(x,y,1.0f));
return rotate(angle,x,y,1.0f);
}
inline Matrix4f rotate(float angle,const Vector4f &axis)
@@ -131,9 +137,9 @@ namespace hgl
inline Vector3f rotate(const Vector3f &v3f,float angle,const Vector3f &axis)
{
Vector4f result=rotate(angle,axis)*Vector4f(v3f,1.0f);
Vector4f result = rotate(angle, axis)*Vector4f(v3f, 1.0f);
return result.xyz();
return Vector3f(result.x,result.y,result.z);
}
}//namespace hgl
#endif//HGL_ALGORITHM_MATH_VECTOR_MATRIX_INCLUDE

View File

@@ -3,37 +3,16 @@
#ifdef _MSC_VER
#pragma warning(disable:4244) // double -> int 精度丢失警告
#pragma warning(disable:4996) // sprintf may be unsafe, Consider using sprintf_s instead
#endif//_MSC_VER
#include<hgl/math/FastTriangle.h>
#include<MathGeoLib.h>
/**
* MathGeoLib
* Game Math and Geometry Library
*
* My C++ library for 3D mathematics and geometry manipulation.
* Jukka Jylänki
*
* offical web: http://clb.demon.fi/MathGeoLib/nightly/
*
* License:
*
* This library is licensed under the Apache 2 license. I am not a lawyer, but to me that
* license means that you can use this code for any purpose, both commercial and closed source.
* You are however restricted from claiming you wrote it yourself, and cannot hold me liable
* for anything over this code.
* I acknowledge that most of the non-trivial math routines are taken off a book or a
* research paper. In all places, I have tried to be diligent to properly attribute the original
* source. Please contact me if you feel I have misattributed something.
*/
#include<glm/glm.hpp>
namespace hgl
{
using Vector2f=float2;
using Vector3f=float3;
using Vector4f=float4;
using Vector2f=glm::vec2;
using Vector3f=glm::vec3;
using Vector4f=glm::vec4;
inline bool operator == (const Vector2f &lhs,const Vector2f &rhs)
{
@@ -117,37 +96,37 @@ namespace hgl
template<typename T>
inline T normalized(const T &v)
{
return v.Normalized();
return glm::normalize(v);
}
template<typename T>
inline void normalize(T &v)
{
v.Normalize();
v=glm::normalize(v);
}
template<typename T>
inline T cross(const T &v1,const T &v2)
{
return v1.Cross(v2);
return glm::cross(v1,v2);
}
template<typename T>
inline float dot(const T &v1,const T &v2)
{
return v1.Dot(v2);
return glm::dot(v1,v2);
}
template<typename T>
inline float dot2(const T &v)
{
return v.Dot(v);
}
//template<typename T>
//inline float dot2(const T &v)
//{
// return v.Dot(v);
//}
inline float ray_angle_cos(const Ray &ray,const vec &pos)
{
return ray.dir.Dot((pos-ray.pos).Normalized());
}
//inline float ray_angle_cos(const Ray &ray,const vec &pos)
//{
// return ray.dir.Dot((pos-ray.pos).Normalized());
//}
inline float length_squared(const Vector2f &v)
{

View File

@@ -22,11 +22,13 @@ namespace hgl
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);
//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);
return glm::orthoRH_ZO(left,right,bottom,top,znear,zfar);
}
/**
@@ -38,26 +40,28 @@ namespace hgl
*/
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);
//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);
return glm::orthoRH_ZO(0.0f,width,height,0.0f,znear,zfar);
}
/**
* 生成一个正角视图矩阵
* @param width 宽
* @param height 高
*/
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 width 宽
// * @param height 高
// */
//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);
//}
/**
* 生成一个透视矩阵
@@ -71,39 +75,46 @@ namespace hgl
float znear,
float zfar)
{
const float f = 1.0f / tan( hgl_ang2rad( 0.5f * field_of_view ) );
glm::perspectiveRH_ZO(field_of_view,aspect_ratio,znear,zfar);
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),
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 某些引擎这两项会乘0.5,那是因为他们是 -1 to 1 的Z值设定而我们是 0 to 1所以这里不用乘
// 同理camera的znear为接近0的正数zfar为一个较大的正数默认使用16/256
//const float f = 1.0f / tan( hgl_ang2rad( 0.5f * field_of_view ) );
0.0f, 0.0f, -1.0f, 0.0f);
//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),
// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// // 某些引擎这两项会乘0.5,那是因为他们是 -1 to 1 的Z值设定而我们是 0 to 1所以这里不用乘
// // 同理camera的znear为接近0的正数zfar为一个较大的正数默认使用16/256
// 0.0f, 0.0f, -1.0f, 0.0f);
}
Matrix4f lookat(const Vector4f &eye,const Vector4f &target,const Vector4f &up)
{
Vector4f forward=target-eye;
Vector3f e(eye.x,eye.y,eye.z);
Vector3f c(target.x,target.y,target.z);
Vector3f u(up.x,up.y,up.z);
normalize(forward);
glm::lookAtRH(e,c,u);
//Vector4f forward=target-eye;
Vector4f right=cross(forward,up);
//normalize(forward);
normalize(right);
//Vector4f right=cross(forward,up);
Vector4f nup=cross(right,forward);
//normalize(right);
Matrix4f result( right.x, right.y, right.z, 1.0f,
nup.x, nup.y, nup.z, 1.0f,
-forward.x, -forward.y, -forward.z/2.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f);
// ^^^^^^
// 某些引擎这里为0.5,那是因为他们是 -1 to 1 的Z值设定而我们是0 to 1所以这里不用乘
// 同理camera的znear为接近0的正数zfar为一个较大的正数默认使用16/256
//Vector4f nup=cross(right,forward);
return result*translate(-eye.xyz());
//Matrix4f result( right.x, right.y, right.z, 1.0f,
// nup.x, nup.y, nup.z, 1.0f,
// -forward.x, -forward.y, -forward.z/2.0f, 1.0f,
// 0.0f, 0.0f, 0.0f, 1.0f);
// // ^^^^^^
// // 某些引擎这里为0.5,那是因为他们是 -1 to 1 的Z值设定而我们是0 to 1所以这里不用乘
// // 同理camera的znear为接近0的正数zfar为一个较大的正数默认使用16/256
//return result*translate(-eye.xyz());
}
}//namespace hgl