From 853978004335d158dae6c1867e1d71ead9b36911 Mon Sep 17 00:00:00 2001 From: Warwick Date: Thu, 3 Feb 2022 16:16:32 +0000 Subject: [PATCH] Using a different set of maths to manipulate the camera --- src/PlayerCamera.cpp | 49 ++++++++++---------------------------------- src/PlayerCamera.h | 8 +++----- 2 files changed, 14 insertions(+), 43 deletions(-) diff --git a/src/PlayerCamera.cpp b/src/PlayerCamera.cpp index 494568d..cde03a9 100644 --- a/src/PlayerCamera.cpp +++ b/src/PlayerCamera.cpp @@ -8,50 +8,23 @@ PlayerCamera::PlayerCamera() { PlayerCamera::~PlayerCamera() {} void PlayerCamera::tick() { - pos.vectorPos = glm::vec4(0.0f, 0.0f, 5.0f, 1.0f); - // Get the mouse movement - int mouseX, mouseY; - SDL_GetRelativeMouseState(&mouseX, &mouseY); + // get camera right + glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); + glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraForward)); - // Calculate rotation - pos.yaw -= mouseX * mouseSensitivity; - pos.pitch -= mouseX * mouseSensitivity; + // get position to look at + glm::vec3 cameraTarget = cameraPosition + cameraForward; - // Stop camera from looking too far up/down - const float maxPitch = glm::radians(89.0f); - if (pos.pitch > maxPitch) - pos.pitch = maxPitch; - if (pos.pitch < -maxPitch) - pos.pitch = -maxPitch; + // MVP stuff + glm::mat4 model = glm::mat4(1.0f); - glm::vec4 playerForward(0.0f, 0.0f, -1.0f, 0.0f); - glm::mat4 playerRotation; - // Rotate yaw - playerRotation = - glm::rotate(playerRotation, pos.yaw, glm::vec3(0.0f, 1.0f, 0.0f)); - // Rotate pitch - playerRotation = - glm::rotate(playerRotation, pos.pitch, glm::vec3(1.0f, 0.0f, 0.0f)); - playerForward = playerRotation * playerForward; + glm::mat4 view; + view = glm::lookAt(cameraPosition, cameraTarget, up); - // until we add keyboard stuff set the position to a static value - - // create MVP - glm::mat4 view = glm::lookAt( - glm::vec3(pos.vectorPos), - glm::vec3(pos.vectorPos + - playerForward /* glm::vec4(0.0f, 0.0f, -1.0f, 0.0f) */), - glm::vec3(0.0f, 1.0f, 0.0f)); - - // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 - // unit <-> 100 units - float width = 800; - float height = 600; glm::mat4 projection = glm::perspective( - glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f); - glm::mat4 transform = glm::mat4(1.0f); + glm::radians(45.0f), (float)800 / (float)600, 0.1f, 100.0f); - MVP = projection * view * transform; + MVP = projection * view * model; } glm::mat4 PlayerCamera::getMVP() { return MVP; } diff --git a/src/PlayerCamera.h b/src/PlayerCamera.h index b100a10..5c9b9f9 100644 --- a/src/PlayerCamera.h +++ b/src/PlayerCamera.h @@ -16,11 +16,9 @@ private: const float mouseSensitivity = 0.005f; // Player position - struct position { - glm::vec4 vectorPos; - float pitch; - float yaw; - } pos; + glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 3.0f); + glm::vec3 cameraForward = glm::vec3(0.0f, 0.0f, -1.0f); + glm::vec3 cameraUp = glm::vec3(0.0f, 0.0f, -1.0f); // Mouse position int mouseX, mouseY;