From a29a35eceaa9bc19f3e4a64ebebec0a2f6bd081f Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 12 Mar 2024 22:29:40 +0800 Subject: [PATCH] resume lookat what to change in furture --- inc/hgl/math/Matrix.h | 7 +++++++ src/Math/Matrix4f.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/inc/hgl/math/Matrix.h b/inc/hgl/math/Matrix.h index dc85474..3610913 100644 --- a/inc/hgl/math/Matrix.h +++ b/inc/hgl/math/Matrix.h @@ -75,6 +75,13 @@ namespace hgl 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); inline Matrix4f translate(const Vector3f &v) { diff --git a/src/Math/Matrix4f.cpp b/src/Math/Matrix4f.cpp index 0259319..0eb4b13 100644 --- a/src/Math/Matrix4f.cpp +++ b/src/Math/Matrix4f.cpp @@ -102,4 +102,33 @@ namespace hgl 0.0f ); } + + Matrix4f lookat(const Vector3f &eye,const Vector3f &target,const Vector3f &up) + { + Vector3f forward=normalize(target-eye); + Vector3f right=normalize(cross(forward,up)); + + Vector3f nup=cross(right,forward); + + return Matrix4f( right.x, + nup.x, + -forward.x, + 0.0f, + + right.y, + nup.y, + -forward.y, + 0.0f, + + right.z, + nup.z, + -forward.z/2.0f, + 0.0f, + + -dot(eye,right ), + -dot(eye,nup ), + dot(eye,forward), + 1.0f + ); + } }//namespace hgl