From b11df0262e762d03ef4c6da9d0fc07e4589aa9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Sobczy=C5=84ski?= Date: Thu, 5 Nov 2020 14:53:42 +0100 Subject: [PATCH] camera refactor --- src/engine/include/modules/camera_base.hpp | 26 +++++++++++++++++++++- src/engine/modules/camera_base.cpp | 13 +++++------ src/samples/ari/camera.cpp | 5 ++--- src/samples/floors/camera.cpp | 3 +-- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/engine/include/modules/camera_base.hpp b/src/engine/include/modules/camera_base.hpp index 0544e6f..342acc7 100644 --- a/src/engine/include/modules/camera_base.hpp +++ b/src/engine/include/modules/camera_base.hpp @@ -24,13 +24,37 @@ public: CameraBase(ScreenSettings *t_screen, Vector3 *t_position, Vector3 *t_up); virtual ~CameraBase(){}; - void updatePlanes(Vector3 t_target); + /** + * Frustum planes. + * Set by lookAt() method. + */ Plane planes[6]; + + /** + * Projection matrix + * Set by lookAt() method. + */ Matrix worldView; protected: + /** Pointer to screen settings */ ScreenSettings *screen; + /** + * Adaption of OpenGL lookAt camera. + * Calculates worldView matrix and update frustum planes + * Should be called every frame. + */ + void lookAt(Vector3 &t_target); + + /** + * Do not call this method unless you know what you do. + * Called via lookAt(). + * Calculate and update frustum planes. + * http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-extracting-the-planes/ + */ + void updatePlanes(Vector3 t_target); + private: Vector3 *p_position, *p_up; float farPlaneDist, nearPlaneDist, nearHeight, nearWidth, farHeight, farWidth; diff --git a/src/engine/modules/camera_base.cpp b/src/engine/modules/camera_base.cpp index e6823ba..124ee77 100644 --- a/src/engine/modules/camera_base.cpp +++ b/src/engine/modules/camera_base.cpp @@ -18,10 +18,6 @@ // Constructors/Destructors // ---- -/** Initializes vars and calculate width/height of near and far plane - * @param fov (FOV in radians)/2 - * @param ratio Aspect ratio -*/ CameraBase::CameraBase(ScreenSettings *t_screen, Vector3 *t_position, Vector3 *t_up) : screen(t_screen) { @@ -42,9 +38,12 @@ CameraBase::CameraBase(ScreenSettings *t_screen, Vector3 *t_position, Vector3 *t // Methods // ---- -/** Calculates and updates frustum planes - * http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-extracting-the-planes/ - */ +void CameraBase::lookAt(Vector3 &t_target) +{ + updatePlanes(t_target); + worldView.lookAt(*p_up, *p_position, t_target); +} + void CameraBase::updatePlanes(Vector3 t_target) { Vector3 nearCenter, farCenter, X, Y, Z; diff --git a/src/samples/ari/camera.cpp b/src/samples/ari/camera.cpp index 045c656..9c506a0 100644 --- a/src/samples/ari/camera.cpp +++ b/src/samples/ari/camera.cpp @@ -39,9 +39,8 @@ void Camera::update(Pad &t_pad, Mesh &t_mesh) { rotate(t_pad); followBy(t_mesh); - updatePlanes(t_mesh.position); // TODO move it out - Vector3 newPos = Vector3(t_mesh.position.x, t_mesh.position.y + 10.0F, t_mesh.position.z); - worldView.lookAt(up, position, newPos); + Vector3 lookPos = Vector3(t_mesh.position.x, t_mesh.position.y + 10.0F, t_mesh.position.z); + lookAt(lookPos); } /** Set camera rotation by pad right joy and update unit circle diff --git a/src/samples/floors/camera.cpp b/src/samples/floors/camera.cpp index d0919a9..b31d307 100644 --- a/src/samples/floors/camera.cpp +++ b/src/samples/floors/camera.cpp @@ -39,8 +39,7 @@ void Camera::update(Pad &t_pad, Mesh &t_mesh) { rotate(t_pad); followBy(t_mesh); - updatePlanes(t_mesh.position); - worldView.lookAt(up, position, t_mesh.position); + lookAt(t_mesh.position); } /** Set camera rotation by pad right joy and update unit circle