From a89a5745ea40cfbbcaa0c5ea21d77aef768fce42 Mon Sep 17 00:00:00 2001 From: Warwick Date: Tue, 14 Jun 2022 15:01:27 +0100 Subject: [PATCH] Fixed camera looking at last frame's forward target. --- src/PlayerCamera.cpp | 38 +++++++++++++++++++------------------- src/PlayerCamera.h | 4 ++-- src/main.cpp | 33 ++++++++------------------------- 3 files changed, 29 insertions(+), 46 deletions(-) diff --git a/src/PlayerCamera.cpp b/src/PlayerCamera.cpp index c3bac61..641d2a1 100644 --- a/src/PlayerCamera.cpp +++ b/src/PlayerCamera.cpp @@ -8,13 +8,27 @@ PlayerCamera::PlayerCamera() { PlayerCamera::~PlayerCamera() {} void PlayerCamera::tick() { + + // handle mouse + SDL_GetRelativeMouseState(&mouseX, &mouseY); + cameraYaw += mouseX * glm::radians(mouseSensitivity); + cameraPitch -= mouseY * glm::radians(mouseSensitivity); + // lock pitch to certain range + if (cameraPitch > 89.0f) + cameraPitch = 89.0f; + if (cameraPitch < -89.0f) + cameraPitch = -89.0f; + + // calculate camera rotation + glm::vec3 direction; + direction.x = cos(cameraYaw) * cos(cameraPitch); + direction.y = sin(cameraPitch); + direction.z = sin(cameraYaw) * cos(cameraPitch); + cameraForward = glm::normalize(direction); // get camera right glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); glm::vec3 cameraRight = glm::normalize(glm::cross(up, cameraForward)); - // get position to look at - glm::vec3 cameraTarget = cameraPosition + cameraForward; - // TODO Handle movement speed based on delta time // handle keyboard input const Uint8 *keyboardState = SDL_GetKeyboardState(nullptr); @@ -27,22 +41,8 @@ void PlayerCamera::tick() { if (keyboardState[SDL_SCANCODE_D]) cameraPosition -= cameraRight * movementSpeed; - // handle mouse - SDL_GetRelativeMouseState(&mouseX, &mouseY); - cameraYaw += mouseX * mouseSensitivity; - cameraPitch -= mouseY * mouseSensitivity; - // lock pitch to certain range - if (cameraPitch > 89.0f) - cameraPitch = 89.0f; - if (cameraPitch < -89.0f) - cameraPitch = -89.0f; - - // calculate camera rotation - glm::vec3 direction; - direction.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)); - direction.y = sin(glm::radians(cameraPitch)); - direction.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)); - cameraForward = glm::normalize(direction); + // get position to look at + glm::vec3 cameraTarget = cameraPosition + cameraForward; // MVP stuff glm::mat4 model = glm::mat4(1.0f); diff --git a/src/PlayerCamera.h b/src/PlayerCamera.h index 57f184c..7bb61c6 100644 --- a/src/PlayerCamera.h +++ b/src/PlayerCamera.h @@ -20,8 +20,8 @@ private: 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); - float cameraYaw = -90.0f; - float cameraPitch = 0.0f; + float cameraYaw = glm::radians(-90.0f); + float cameraPitch = glm::radians(0.0f); // Mouse position int mouseX, mouseY; diff --git a/src/main.cpp b/src/main.cpp index 73e7221..b2c5fda 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,12 +3,7 @@ // Make sure Glew is loaded first #include #include -// Not used yet -#include -#include // File reader -#include -#include #include // Shader #include "ShaderLoader.h" @@ -18,7 +13,6 @@ // Objects #include "Mesh.h" #include "Model.h" -#include // Include error class #include "Error.h" @@ -34,7 +28,7 @@ int main(int argc, char **argv) { // Make OpenGL use double buffering (Render game first then shove to output) SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - // TODO: Understand what a depth buffer is + // Initialise depth buffer SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); // TODO: Discover if this is necessary for linux and what values they need be @@ -54,10 +48,11 @@ int main(int argc, char **argv) { // Create glContext SDL_GLContext glContext = SDL_GL_CreateContext(window); - // TODO: Test that glContext was created successfully + if (!glContext) { + error.crash("No SDL OpenGL Context initialised."); + } // Tell us the number of vertex attributes allowed in bug reports - // int nrAttributes; glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes); error.log("Maximum num of vertex attributes supported: " + @@ -74,18 +69,11 @@ int main(int argc, char **argv) { ShaderLoader shader(ROOT_DIR "data/shaders/vertex.glsl", ROOT_DIR "data/shaders/fragment.glsl"); - Model backpack(ROOT_DIR "data/models/backpack/backpack.obj"); + Model backpack(std::string(ROOT_DIR) + + std::string("data/models/backpack/backpack.obj")); Model cube(ROOT_DIR "data/models/cube/cube.obj"); cube.translate(glm::vec3(3.0f, 0.0f, -1.0f)); - // Mess with perspective - // 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); - // Create player camera object PlayerCamera camera; @@ -112,8 +100,9 @@ int main(int argc, char **argv) { if (keys[SDL_SCANCODE_ESCAPE]) running = false; } - // TODO: Do something with keys lol }; + + // TODO: Run game here lol camera.tick(); // Clear screen ready for next loop @@ -131,12 +120,6 @@ int main(int argc, char **argv) { // Finally render everything shader.use(); - // I think this is meant to be here but it breaks... - // shove vertex array into buffer - // glBindVertexArray(VAO); - - // TODO: Run game here lol - SDL_GL_SwapWindow(window); };