From 089d4992016b39cefc399723ea162bd4de7bd310 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 26 Sep 2021 20:48:34 +0800 Subject: [PATCH] fixed matrix --- inc/hgl/math/MathConst.h | 26 ++++++++++++------------ inc/hgl/math/Matrix.h | 5 ----- src/Math/Matrix4f.cpp | 44 ++++++++++++++++------------------------ 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/inc/hgl/math/MathConst.h b/inc/hgl/math/MathConst.h index e0186e3..f6ed403 100644 --- a/inc/hgl/math/MathConst.h +++ b/inc/hgl/math/MathConst.h @@ -29,24 +29,24 @@ namespace hgl constexpr double HGL_COS_ANG_270=0; constexpr double HGL_COS_ANG_315=0.707106781187; - #define HGL_DEF_ANG2RAD(ang) constexpr double HGL_RAD_##ang=double(ang)*(HGL_PI/180.0f); + #define HGL_DEF_DEG2RAD(ang) constexpr double HGL_RAD_##ang=double(ang)*(HGL_PI/180.0f); - HGL_DEF_ANG2RAD(0) - HGL_DEF_ANG2RAD(45) - HGL_DEF_ANG2RAD(90) - HGL_DEF_ANG2RAD(135) - HGL_DEF_ANG2RAD(180) - HGL_DEF_ANG2RAD(225) - HGL_DEF_ANG2RAD(270) - HGL_DEF_ANG2RAD(315) - HGL_DEF_ANG2RAD(360) + HGL_DEF_DEG2RAD(0) + HGL_DEF_DEG2RAD(45) + HGL_DEF_DEG2RAD(90) + HGL_DEF_DEG2RAD(135) + HGL_DEF_DEG2RAD(180) + HGL_DEF_DEG2RAD(225) + HGL_DEF_DEG2RAD(270) + HGL_DEF_DEG2RAD(315) + HGL_DEF_DEG2RAD(360) - #undef HGL_DEF_ANG2RAD + #undef HGL_DEF_DEG2RAD /** * 角度转弧度 */ - inline double hgl_ang2rad(const double ang) + inline double hgl_deg2rad(const double ang) { return ang*(HGL_PI/180.0f); } @@ -54,7 +54,7 @@ namespace hgl /** * 弧度转角度 */ - inline double hgl_rad2ang(const double rad) + inline double hgl_rad2deg(const double rad) { return rad*(180.0f/HGL_PI); } diff --git a/inc/hgl/math/Matrix.h b/inc/hgl/math/Matrix.h index 0e17c4c..a8d734d 100644 --- a/inc/hgl/math/Matrix.h +++ b/inc/hgl/math/Matrix.h @@ -13,11 +13,6 @@ namespace hgl using Matrix3f=glm::mat3; using Matrix4f=glm::mat4; - inline Matrix4f identity() - { - return Matrix4f(); - } - inline Matrix4f inverse(const Matrix4f &m) { return glm::inverse(m); diff --git a/src/Math/Matrix4f.cpp b/src/Math/Matrix4f.cpp index d5efcee..0070979 100644 --- a/src/Math/Matrix4f.cpp +++ b/src/Math/Matrix4f.cpp @@ -80,14 +80,16 @@ namespace hgl float znear, float zfar) { + float f = 1.0f / tan( hgl_deg2rad( 0.5f * field_of_view ) ); + return Matrix4f( - field_of_view / aspect_ratio, + f / aspect_ratio, 0.0f, 0.0f, 0.0f, 0.0f, - -field_of_view, + -f, 0.0f, 0.0f, @@ -105,41 +107,31 @@ namespace hgl Matrix4f lookat(const Vector4f &eye,const Vector4f &target,const Vector4f &up) { - Vector4f forward=target-eye; - - normalize(forward); - - Vector4f right=cross(forward,up); - - normalize(right); + Vector4f forward=normalize(target-eye); + Vector4f right=normalize(cross(forward,up)); Vector4f nup=cross(right,forward); - //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); - Matrix4f result( right.x, nup.x, -forward.x, 0.0f, - right.y, - nup.y, + right.y, + nup.y, -forward.y, - 0.0f, + 0.0f, - right.z, - nup.z, - -forward.z/2.0f, - 0.0f, + right.z, + nup.z, + -forward.z/2.0f, + 0.0f, - 1.0f, - 1.0f, - 1.0f, - 1.0f); + dot(eye,right ), + dot(eye,nup ), + dot(eye,forward), + 1.0f); - return result*translate(-Vector3f(eye)); + return result; } }//namespace hgl