diff --git a/apps/Viewer/Camera.cpp b/apps/Viewer/Camera.cpp index b93460eb1..b217c2d6b 100644 --- a/apps/Viewer/Camera.cpp +++ b/apps/Viewer/Camera.cpp @@ -103,12 +103,30 @@ void Camera::SetFOV(double _fov) Eigen::Vector3d Camera::GetPosition() const { - const Eigen::Matrix3d r(rotation.toRotationMatrix()); + const Eigen::Matrix3d R(rotation.toRotationMatrix()); const Eigen::Vector3d eye(0, 0, dist); - return r * eye + center; + return R * eye + center; } -void Camera::GetLookAt(Eigen::Vector3d& _eye, Eigen::Vector3d& _center, Eigen::Vector3d& _up) +Eigen::Matrix4d Camera::GetLookAt() const +{ + const Eigen::Matrix3d R(rotation.toRotationMatrix()); + const Eigen::Vector3d eye(R.col(2) * dist + center); + const Eigen::Vector3d up(R.col(1)); + + const Eigen::Vector3d n((center-eye).normalized()); + const Eigen::Vector3d s(n.cross(up)); + const Eigen::Vector3d v(s.cross(n)); + + Eigen::Matrix4d m; + m << + s(0), s(1), s(2), -eye.dot(s), + v(0), v(1), v(2), -eye.dot(v), + -n(0), -n(1), -n(2), eye.dot(n), + 0.0, 0.0, 0.0, 1.0; + return m; +} +void Camera::GetLookAt(Eigen::Vector3d& _eye, Eigen::Vector3d& _center, Eigen::Vector3d& _up) const { const Eigen::Matrix3d R(rotation.toRotationMatrix()); const Eigen::Vector3d eye(0, 0, dist); diff --git a/apps/Viewer/Camera.h b/apps/Viewer/Camera.h index 64973d35b..37d982386 100644 --- a/apps/Viewer/Camera.h +++ b/apps/Viewer/Camera.h @@ -68,7 +68,8 @@ class Camera void SetFOV(double _fov); Eigen::Vector3d GetPosition() const; - void GetLookAt(Eigen::Vector3d& eye, Eigen::Vector3d& center, Eigen::Vector3d& up); + Eigen::Matrix4d GetLookAt() const; + void GetLookAt(Eigen::Vector3d& eye, Eigen::Vector3d& center, Eigen::Vector3d& up) const; void Rotate(const Eigen::Vector2d& pos, const Eigen::Vector2d& prevPos); protected: diff --git a/apps/Viewer/Window.cpp b/apps/Viewer/Window.cpp index 56cccb3da..f39784f61 100644 --- a/apps/Viewer/Window.cpp +++ b/apps/Viewer/Window.cpp @@ -124,7 +124,6 @@ void Window::Reset() void Window::UpdateView(const ImageArr& images, const MVS::ImageArr& sceneImages) { glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); if (camera->prevCamID != camera->currentCamID && camera->currentCamID != NO_ID) { // enable camera view mode // apply current camera transform @@ -132,13 +131,12 @@ void Window::UpdateView(const ImageArr& images, const MVS::ImageArr& sceneImages const MVS::Image& imageData = sceneImages[image.idx]; const MVS::Camera& camera = imageData.camera; const Eigen::Matrix4d trans(TransW2L((const Matrix3x3::EMat)camera.R, camera.GetT())); - glMultMatrixf((GLfloat*)gs_convert); + glLoadMatrixf((GLfloat*)gs_convert); glMultMatrixd((GLdouble*)trans.data()); } else { // apply view point transform - Eigen::Vector3d eye, center, up; - camera->GetLookAt(eye, center, up); - gluLookAt(eye.x(), eye.y(), eye.z(), center.x(), center.y(), center.z(), up.x(), up.y(), up.z()); + const Eigen::Matrix4d trans(camera->GetLookAt()); + glLoadMatrixd((GLdouble*)trans.data()); } }