diff --git a/modules/camera/include/vkcv/camera/TrackballCameraController.hpp b/modules/camera/include/vkcv/camera/TrackballCameraController.hpp index 31aecf13538db03b86d7005dded488fa9970cb70..56ae4732d4bff2396a1cc6de2622458c108e12d1 100644 --- a/modules/camera/include/vkcv/camera/TrackballCameraController.hpp +++ b/modules/camera/include/vkcv/camera/TrackballCameraController.hpp @@ -20,13 +20,13 @@ namespace vkcv { * @brief Updates the position of the camera. * @return The updated camera position. */ - glm::vec3 updatePosition(); + glm::vec3 updatePosition(Camera &camera); /** * @brief Updates the view matrix of the camera. * @return The updated view matrix of the camera. */ - glm::mat4 updateView(); + glm::mat4 updateView(Camera &camera); /** * @brief Updates the current radius in respect to the @p offset. @@ -58,13 +58,13 @@ namespace vkcv { * @param[in] xOffset The offset added to the yaw value. * @param[in] yOffset The offset added to the pitch value. */ - void panView(double xOffset, double yOffset); + void panView(double xOffset, double yOffset, Camera &camera); /** * @brief Updates the camera object in respect to @p deltaTime. * @param deltaTime The time that has passed since last update. */ - void updateCamera(double deltaTime); + void updateCamera(double deltaTime, Camera &camera); /** * @brief A callback function for key events. Currently, the trackball camera does not support camera movement. @@ -74,7 +74,7 @@ namespace vkcv { * @param[in] action The key action. * @param[in] mods The modifier bits. */ - void keyCallback(int key, int scancode, int action, int mods); + void keyCallback(int key, int scancode, int action, int mods, Camera &camera); /** * @brief A callback function for mouse scrolling events. Currently, this leads to changes in the field of view @@ -82,7 +82,7 @@ namespace vkcv { * @param[in] offsetX The offset in horizontal direction. * @param[in] offsetY The offset in vertical direction. */ - void scrollCallback(double offsetX, double offsetY); + void scrollCallback(double offsetX, double offsetY, Camera &camera); /** * @brief A callback function for mouse movement events. Currently, this leads to panning the view of the @@ -90,7 +90,7 @@ namespace vkcv { * @param[in] x The horizontal mouse position. * @param[in] y The vertical mouse position. */ - void mouseMoveCallback(double x, double y); + void mouseMoveCallback(double xoffset, double yoffset, Camera &camera); /** * @brief A callback function for mouse button events. Currently, the right mouse button enables panning the @@ -99,7 +99,7 @@ namespace vkcv { * @param[in] action The button action. * @param[in] mods The modifier bits. */ - void mouseButtonCallback(int button, int action, int mods); + void mouseButtonCallback(int button, int action, int mods, Camera &camera); }; diff --git a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp index 0acd113905ed01147eb64d5a1f82870f3f306f2f..e36d0c9583008bee3138fc6669587610fa9e8457 100644 --- a/modules/camera/src/vkcv/camera/TrackballCameraController.cpp +++ b/modules/camera/src/vkcv/camera/TrackballCameraController.cpp @@ -6,7 +6,7 @@ namespace vkcv { TrackballCameraController::TrackballCameraController() { m_rotationActive = false; - m_radius = 3.0f; + m_radius = 3.0f; // TODO: Needs to be removed. Radius should only depend on camera m_cameraSpeed = 2.5f; m_scrollSensitivity = 0.2f; } @@ -20,31 +20,31 @@ namespace vkcv { } } - void TrackballCameraController::panView(double xOffset, double yOffset) { + void TrackballCameraController::panView(double xOffset, double yOffset, Camera &camera) { // handle yaw rotation - float yaw = m_camera->getYaw() + xOffset * m_cameraSpeed; + float yaw = camera.getYaw() + xOffset * m_cameraSpeed; if (yaw < 0.0f) { yaw += 360.0f; } else if (yaw > 360.0f) { yaw -= 360.0f; } - m_camera->setYaw(yaw); + camera.setYaw(yaw); // handle pitch rotation - float pitch = m_camera->getPitch() + yOffset * m_cameraSpeed; + float pitch = camera.getPitch() + yOffset * m_cameraSpeed; if (pitch < 0.0f) { pitch += 360.0f; } else if (pitch > 360.0f) { pitch -= 360.0f; } - m_camera->setPitch(pitch); + camera.setPitch(pitch); } - glm::vec3 TrackballCameraController::updatePosition() { - float yaw = m_camera->getYaw(); - float pitch = m_camera->getPitch(); + glm::vec3 TrackballCameraController::updatePosition(Camera &camera) { + float yaw = camera.getYaw(); + float pitch = camera.getPitch(); glm::vec3 yAxis = glm::vec3(0.0f, 1.0f, 0.0f); glm::vec3 xAxis = glm::vec3(1.0f, 0.0f, 0.0f); @@ -52,44 +52,38 @@ namespace vkcv { glm::mat4 rotationX = glm::rotate(rotationY, glm::radians(pitch), xAxis); glm::vec3 translate = glm::vec3(0.0f, 0.0f, m_radius); translate = glm::vec3(rotationX * glm::vec4(translate, 0.0f)); - glm::vec3 center = m_camera->getCenter(); + glm::vec3 center = camera.getCenter(); glm::vec3 position = center +translate; - m_camera->setPosition(position); + camera.setPosition(position); glm::vec3 up = glm::vec3(rotationX * glm::vec4(glm::vec3(0.0f, 1.0f, 0.0f), 0.0f)); - m_camera->setUp(up); + camera.setUp(up); return position; } - glm::mat4 TrackballCameraController::updateView() { - updatePosition(); - glm::vec3 position = m_camera->getPosition(); - glm::vec3 center = m_camera->getCenter(); - glm::vec3 up = m_camera->getUp(); - m_camera->lookAt(position, center, up); - return m_camera->getView(); + glm::mat4 TrackballCameraController::updateView(Camera &camera) { + updatePosition(camera); + glm::vec3 position = camera.getPosition(); + glm::vec3 center = camera.getCenter(); + glm::vec3 up = camera.getUp(); + camera.lookAt(position, center, up); + return camera.getView(); } void TrackballCameraController::updateRadius(double offset) { setRadius(m_radius - offset * m_scrollSensitivity); } - void TrackballCameraController::updateCamera(double deltaTime) { - updateView(); + void TrackballCameraController::updateCamera(double deltaTime, Camera &camera) { + updateView(camera); } - void TrackballCameraController::keyCallback(int key, int scancode, int action, int mods) {} + void TrackballCameraController::keyCallback(int key, int scancode, int action, int mods, Camera &camera) {} - void TrackballCameraController::scrollCallback(double offsetX, double offsetY) { + void TrackballCameraController::scrollCallback(double offsetX, double offsetY, Camera &camera) { updateRadius(offsetY); } - void TrackballCameraController::mouseMoveCallback(double x, double y) { - auto xoffset = static_cast<float>(x - m_lastX); - auto yoffset = static_cast<float>(y - m_lastY); - - m_lastX = x; - m_lastY = y; - + void TrackballCameraController::mouseMoveCallback(double xoffset, double yoffset, Camera &camera) { if(!m_rotationActive){ return; } @@ -98,16 +92,14 @@ namespace vkcv { xoffset *= sensitivity; yoffset *= sensitivity; - panView(xoffset , yoffset); + panView(xoffset , yoffset, camera); } - void TrackballCameraController::mouseButtonCallback(int button, int action, int mods) { + void TrackballCameraController::mouseButtonCallback(int button, int action, int mods, Camera &camera) { if(button == GLFW_MOUSE_BUTTON_2 && m_rotationActive == false && action == GLFW_PRESS){ - glfwSetInputMode(m_window->getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); m_rotationActive = true; } else if(button == GLFW_MOUSE_BUTTON_2 && m_rotationActive == true && action == GLFW_RELEASE){ - glfwSetInputMode(m_window->getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); m_rotationActive = false; } }