From 5e086c884f9ed18572c43724701dfaf4c4532748 Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 6 Sep 2020 14:17:00 +0200 Subject: [PATCH] Updated camera class --- src/main.cpp | 41 +++++++++++++++-------------------------- src/objects/Camera.cpp | 42 +++++++++++++++++------------------------- src/objects/Camera.hpp | 8 ++++++-- 3 files changed, 38 insertions(+), 53 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index caf4626..3aafde0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,9 @@ int height = 800; bool trapCursor = true; +float movementSpeed = 6.0f; +float sensitivity = 0.1f; + std::function handleMouseCallback; void LogGlfwError(const char* message) @@ -64,7 +67,7 @@ void FramebufferSizeCallback(GLFWwindow* window, int w, int h) void CursorPosCallback(GLFWwindow* window, double x, double y) { if (trapCursor) - handleMouseCallback(window, x, y); + handleMouseCallback(window, (float)x, (float)y); } void KeyCallback(GLFWwindow* window, int key, int mode, int action, int mods) @@ -79,13 +82,11 @@ void KeyCallback(GLFWwindow* window, int key, int mode, int action, int mods) void HandleKeyboard(GLFWwindow* window, Camera& cam, float frametime) { - static float movementSpeed = 4.5f; - // Movement if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) - cam.Move(cam.front * movementSpeed * frametime); + cam.Move(glm::normalize(glm::vec3(cam.front.x, 0.0f, cam.front.z)) * movementSpeed * frametime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) - cam.Move(-cam.front * movementSpeed * frametime); + cam.Move(-glm::normalize(glm::vec3(cam.front.x, 0.0f, cam.front.z)) * movementSpeed * frametime); if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) cam.Move(-cam.right * movementSpeed * frametime); @@ -100,20 +101,7 @@ void HandleKeyboard(GLFWwindow* window, Camera& cam, float frametime) void HandleMouse(GLFWwindow* window, Camera* cam, float x, float y) { - static const float sensitivity = 0.1f; - - static float pitch = 0.0f; - static float yaw = -90.0f; - - pitch += (height / 2 - y) * sensitivity; - yaw += (x - width / 2) * sensitivity; - - if (pitch > 89.0f) - pitch = 89.0f; - else if (pitch < -89.0f) - pitch = -89.0f; - - cam->SetRotation(glm::vec2(pitch, yaw)); + cam->Rotate(sensitivity * glm::vec2((height / 2 - y), (x - width / 2))); glfwSetCursorPos(window, width / 2.0f, height / 2.0f); } @@ -208,26 +196,21 @@ int main(int argc, char** argv) glEnable(GL_DEPTH_TEST); - float lastTime = glfwGetTime(); + float lastTime = (float)glfwGetTime(); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); - float currentTime = glfwGetTime(); + float currentTime = (float)glfwGetTime(); HandleKeyboard(window, cam, currentTime - lastTime); lastTime = currentTime; - float t = glfwGetTime(); - static float PI = glm::pi(); static float fov = glm::radians(45.0f); static float zNear = 0.1f; static float zFar = 100.0f; - - static float orbitSpeed = 1.0f; - static float orbitRadius = 10.0f; glClearColor(0.1f, 0.4f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -246,6 +229,12 @@ int main(int argc, char** argv) ImGui::SliderFloat("zFar", &zFar, 10.0f, 100.0f); } + if (ImGui::CollapsingHeader("Movement")) + { + ImGui::SliderFloat("Speed", &movementSpeed, 0.01f, 10.0f); + ImGui::SliderFloat("Sensitivity", &sensitivity, 0.01, 10.0f); + } + ImGui::End(); perspective = glm::perspective(fov, (float)width/(float)height, zNear, zFar); diff --git a/src/objects/Camera.cpp b/src/objects/Camera.cpp index 3c2729d..212f7c0 100644 --- a/src/objects/Camera.cpp +++ b/src/objects/Camera.cpp @@ -2,20 +2,8 @@ #include -Camera::Camera(glm::vec3 position, glm::vec3 direction) : - position(position), direction(direction) -{ - CalculateCamera(); -} - Camera::Camera(glm::vec3 position, glm::vec2 pitchYaw) : - position(position), direction( - glm::vec3( - cos(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)), - sin(glm::radians(pitchYaw.x)), - sin(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)) - ) - ) + position(position), rotation(pitchYaw) { CalculateCamera(); } @@ -34,24 +22,25 @@ void Camera::Move(glm::vec3 movement) void Camera::SetRotation(glm::vec2 pitchYaw) { - direction = glm::vec3( - cos(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)), - sin(glm::radians(pitchYaw.x)), - sin(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)) - ); + rotation = pitchYaw; CalculateCamera(); } void Camera::Rotate(glm::vec2 pitchYaw) { - direction += glm::vec3( - cos(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)), - sin(glm::radians(pitchYaw.x)), - sin(glm::radians(pitchYaw.y)) * cos(glm::radians(pitchYaw.x)) - ); + rotation += pitchYaw; + if (rotation.x > 89.0f) + rotation.x = 89.0f; + else if (rotation.x < -89.0f) + rotation.x = -89.0f; CalculateCamera(); } +void Camera::Constrain(glm::vec4 constraints) +{ + rotationConstraints = constraints; +} + void Camera::Use(const Shader& program) { program.SetUniformMat4("view", &viewMatrix[0][0]); @@ -59,9 +48,12 @@ void Camera::Use(const Shader& program) void Camera::CalculateCamera() { - front = glm::normalize(direction); + front = glm::normalize(glm::vec3( + cos(glm::radians(rotation.y)) * cos(glm::radians(rotation.x)), + sin(glm::radians(rotation.x)), + sin(glm::radians(rotation.y)) * cos(glm::radians(rotation.x)) + )); right = glm::normalize(glm::cross(front, glm::vec3(0.0f, 1.0f, 0.0f))); - // right = glm::normalize(glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), front)); up = glm::cross(right, front); viewMatrix = glm::lookAt(position, position + front, up); diff --git a/src/objects/Camera.hpp b/src/objects/Camera.hpp index 1fffe0a..f3bcc2a 100644 --- a/src/objects/Camera.hpp +++ b/src/objects/Camera.hpp @@ -4,11 +4,11 @@ #include "Shader.hpp" +// TODO: reimplement, make it better class Camera { public: Camera() = default; - Camera(glm::vec3 position, glm::vec3 direction); Camera(glm::vec3 position, glm::vec2 pitchYaw); void SetPosition(glm::vec3 newPos); @@ -17,6 +17,8 @@ public: void SetRotation(glm::vec2 pitchYaw); void Rotate(glm::vec2 pitchYaw); + void Constrain(glm::vec4 constraints); + void Use(const Shader& program); public: @@ -25,7 +27,9 @@ public: private: void CalculateCamera(); - glm::vec3 position, direction; + glm::vec3 position; + glm::vec2 rotation; + glm::vec4 rotationConstraints; glm::mat4 viewMatrix; }; \ No newline at end of file