From 13ecebfa6f3a590d8c96458c69205afc828bb22f Mon Sep 17 00:00:00 2001 From: Matteo De Carlo Date: Thu, 6 Nov 2025 23:05:45 +0100 Subject: [PATCH] Fix lookAt functions - to work with RowMajor (RowMajor vectors are a compilation error in Eigen) - lookatRh had an extra `-`, fixed by flipping subtraction to define the vector `f` --- include/eigen_graphics/all.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/include/eigen_graphics/all.h b/include/eigen_graphics/all.h index edb54c8..9f89f52 100644 --- a/include/eigen_graphics/all.h +++ b/include/eigen_graphics/all.h @@ -399,36 +399,36 @@ Eigen::Matrix rotateLh(const E // BEGIN: view matrix creation functions // ---------------------------------------------------------------------------- -template -Eigen::Matrix lookAtRh(const Eigen::MatrixBase& eye, - const Eigen::MatrixBase& target, - const Eigen::MatrixBase& worldUp) { +template +Eigen::Matrix lookAtRh(const Eigen::MatrixBase& eye, + const Eigen::MatrixBase& target, + const Eigen::MatrixBase& worldUp) { static_assert(Derived::RowsAtCompileTime == 3 && Derived::ColsAtCompileTime == 1, "lookAtRh requires 3x1 vectors."); using Scalar = typename Derived::Scalar; - using Vector3 = Eigen::Matrix; - using Matrix4 = Eigen::Matrix; + using Vector3 = Eigen::Matrix; + using Matrix4 = Eigen::Matrix; - Vector3 f = (target - eye).normalized(); + Vector3 f = (eye - target).normalized(); Vector3 r = worldUp.cross(f).normalized(); Vector3 u = f.cross(r); Matrix4 viewMatrix; - if constexpr (Derived::Options == Eigen::RowMajor) { + if constexpr (Options == Eigen::RowMajor) { // clang-format off viewMatrix << - r.x(), u.x(), -f.x(), Scalar(0), - r.y(), u.y(), -f.y(), Scalar(0), - r.z(), u.z(), -f.z(), Scalar(0), + r.x(), u.x(), f.x(), Scalar(0), + r.y(), u.y(), f.y(), Scalar(0), + r.z(), u.z(), f.z(), Scalar(0), -r.dot(eye), -u.dot(eye), -f.dot(eye), Scalar(1); // clang-format on - } else if constexpr (Derived::Options == Eigen::ColMajor) { + } else if constexpr (Options == Eigen::ColMajor) { // clang-format off viewMatrix << r.x(), r.y(), r.z(), -r.dot(eye), u.x(), u.y(), u.z(), -u.dot(eye), - -f.x(), -f.y(), -f.z(), -f.dot(eye), + f.x(), f.y(), f.z(), -f.dot(eye), Scalar(0), Scalar(0), Scalar(0), Scalar(1); // clang-format on } @@ -436,15 +436,15 @@ Eigen::Matrix lookAtRh(const E return viewMatrix; } -template +template Eigen::Matrix lookAtLh(const Eigen::MatrixBase& eye, const Eigen::MatrixBase& target, const Eigen::MatrixBase& worldUp) { static_assert(Derived::RowsAtCompileTime == 3 && Derived::ColsAtCompileTime == 1, "lookAtLh requires 3x1 vectors."); using Scalar = typename Derived::Scalar; - using Vector3 = Eigen::Matrix; - using Matrix4 = Eigen::Matrix; + using Vector3 = Eigen::Matrix; + using Matrix4 = Eigen::Matrix; Vector3 f = (target - eye).normalized(); Vector3 r = worldUp.cross(f).normalized(); @@ -1448,4 +1448,4 @@ Eigen::Matrix transformVector(const Eigen::Matrix