From 21502286c1a266cef74eb8258bded4e868403a84 Mon Sep 17 00:00:00 2001 From: Tobias Frisch <tfrisch@uni-koblenz.de> Date: Thu, 12 Aug 2021 16:18:18 +0200 Subject: [PATCH] [#106] Refactored some of the Camera code Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de> --- .../vkcv/camera/PilotCameraController.hpp | 36 ----------- .../camera/src/vkcv/camera/CameraManager.cpp | 4 +- .../src/vkcv/camera/PilotCameraController.cpp | 61 +++++-------------- .../vkcv/camera/TrackballCameraController.cpp | 15 ++--- 4 files changed, 22 insertions(+), 94 deletions(-) diff --git a/modules/camera/include/vkcv/camera/PilotCameraController.hpp b/modules/camera/include/vkcv/camera/PilotCameraController.hpp index 2b64cdc0..67388818 100644 --- a/modules/camera/include/vkcv/camera/PilotCameraController.hpp +++ b/modules/camera/include/vkcv/camera/PilotCameraController.hpp @@ -29,42 +29,6 @@ namespace vkcv::camera { float m_fov_min; float m_fov_max; - /** - * @brief Indicates forward movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveForward(int action); - - /** - * @brief Indicates backward movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveBackward(int action); - - /** - * @brief Indicates left movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveLeft(int action); - - /** - * @brief Indicates right movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveRight(int action); - - /** - * @brief Indicates upward movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveUpward(int action); - - /** - * @brief Indicates downward movement of the camera depending on the performed @p action. - * @param[in] action The performed action. - */ - void moveDownward(int action); - public: /** diff --git a/modules/camera/src/vkcv/camera/CameraManager.cpp b/modules/camera/src/vkcv/camera/CameraManager.cpp index f129f3a2..c8aa4f7e 100644 --- a/modules/camera/src/vkcv/camera/CameraManager.cpp +++ b/modules/camera/src/vkcv/camera/CameraManager.cpp @@ -52,8 +52,8 @@ namespace vkcv::camera { } void CameraManager::mouseMoveCallback(double x, double y){ - auto xoffset = static_cast<float>(x - m_lastX); - auto yoffset = static_cast<float>(y - m_lastY); + auto xoffset = static_cast<float>(x - m_lastX) / m_window.getWidth(); + auto yoffset = static_cast<float>(y - m_lastY) / m_window.getHeight(); m_lastX = x; m_lastY = y; getActiveController().mouseMoveCallback(xoffset, yoffset, getActiveCamera()); diff --git a/modules/camera/src/vkcv/camera/PilotCameraController.cpp b/modules/camera/src/vkcv/camera/PilotCameraController.cpp index 28ef7c69..4af8eec9 100644 --- a/modules/camera/src/vkcv/camera/PilotCameraController.cpp +++ b/modules/camera/src/vkcv/camera/PilotCameraController.cpp @@ -50,11 +50,11 @@ namespace vkcv::camera { } // handle yaw rotation - float yaw = camera.getYaw() + static_cast<float>(xOffset) * m_cameraSpeed; + float yaw = camera.getYaw() + static_cast<float>(xOffset) * 90.0f * m_cameraSpeed; camera.setYaw(yaw); // handle pitch rotation - float pitch = camera.getPitch() - static_cast<float>(yOffset) * m_cameraSpeed; + float pitch = camera.getPitch() - static_cast<float>(yOffset) * 90.0f * m_cameraSpeed; pitch = glm::clamp(pitch, -89.0f, 89.0f); camera.setPitch(pitch); } @@ -82,22 +82,22 @@ namespace vkcv::camera { void PilotCameraController::keyCallback(int key, int scancode, int action, int mods, Camera &camera) { switch (key) { case GLFW_KEY_W: - moveForward(action); + m_forward = static_cast<bool>(action); break; case GLFW_KEY_S: - moveBackward(action); + m_backward = static_cast<bool>(action); break; case GLFW_KEY_A: - moveLeft(action); + m_left = static_cast<bool>(action); break; case GLFW_KEY_D: - moveRight(action); + m_right = static_cast<bool>(action); break; case GLFW_KEY_E: - moveUpward(action); + m_upward = static_cast<bool>(action); break; case GLFW_KEY_Q: - moveDownward(action); + m_downward = static_cast<bool>(action); break; default: break; @@ -109,24 +109,18 @@ namespace vkcv::camera { } void PilotCameraController::mouseMoveCallback(double xoffset, double yoffset, Camera &camera) { - if(!m_rotationActive){ - return; - } - - float sensitivity = 0.05f; - xoffset *= sensitivity; - yoffset *= sensitivity; + xoffset *= static_cast<float>(m_rotationActive); + yoffset *= static_cast<float>(m_rotationActive); panView(xoffset , yoffset, camera); } void PilotCameraController::mouseButtonCallback(int button, int action, int mods, Camera &camera) { - if(button == GLFW_MOUSE_BUTTON_2 && m_rotationActive == false && action == GLFW_PRESS){ - m_rotationActive = true; - } - else if(button == GLFW_MOUSE_BUTTON_2 && m_rotationActive == true && action == GLFW_RELEASE){ - m_rotationActive = false; - } + if (button == GLFW_MOUSE_BUTTON_2) { + if (m_rotationActive != (action == GLFW_PRESS)) { + m_rotationActive = (action == GLFW_PRESS); + } + } } void PilotCameraController::gamepadCallback(int gamepadIndex, Camera &camera, double frametime) { @@ -162,29 +156,4 @@ namespace vkcv::camera { * -copysign(1.0, stickLeftX); } - - void PilotCameraController::moveForward(int action){ - m_forward = static_cast<bool>(action); - } - - void PilotCameraController::moveBackward(int action){ - m_backward = static_cast<bool>(action); - } - - void PilotCameraController::moveLeft(int action){ - m_left = static_cast<bool>(action); - } - - void PilotCameraController::moveRight(int action){ - m_right = static_cast<bool>(action); - } - - void PilotCameraController::moveUpward(int action){ - m_upward = static_cast<bool>(action); - } - - void PilotCameraController::moveDownward(int action){ - m_downward = static_cast<bool>(action); - } - } \ No newline at end of file diff --git a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp index b149a168..cc72e94b 100644 --- a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp +++ b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp @@ -23,10 +23,10 @@ namespace vkcv::camera { } // handle yaw rotation - m_yaw = m_yaw + static_cast<float>(xOffset) * m_cameraSpeed; + m_yaw = m_yaw + static_cast<float>(xOffset) * 90.0f * m_cameraSpeed; // handle pitch rotation - m_pitch = m_pitch + static_cast<float>(yOffset) * m_cameraSpeed; + m_pitch = m_pitch + static_cast<float>(yOffset) * 90.0f * m_cameraSpeed; } void TrackballCameraController::updateRadius(double offset, Camera &camera) { @@ -67,15 +67,10 @@ namespace vkcv::camera { } void TrackballCameraController::mouseMoveCallback(double xoffset, double yoffset, Camera &camera) { - if(!m_rotationActive){ - return; - } - - float sensitivity = 0.025f; - xoffset *= sensitivity; - yoffset *= sensitivity; + xoffset *= static_cast<float>(m_rotationActive); + yoffset *= static_cast<float>(m_rotationActive); - panView(xoffset , yoffset, camera); + panView(xoffset, yoffset, camera); } void TrackballCameraController::mouseButtonCallback(int button, int action, int mods, Camera &camera) { -- GitLab