diff --git a/CMakeLists.txt b/CMakeLists.txt index 09f2e61..5a3de00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.21 FATAL_ERROR) project(Game) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_CXX_CLANG_TIDY) file(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.c diff --git a/src/PlayerCamera.cpp b/src/PlayerCamera.cpp index a9ddde5..494568d 100644 --- a/src/PlayerCamera.cpp +++ b/src/PlayerCamera.cpp @@ -7,6 +7,51 @@ PlayerCamera::PlayerCamera() { PlayerCamera::~PlayerCamera() {} -void PlayerCamera::tick() {} +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); -glm::mat4 PlayerCamera::getMVP() {} + // Calculate rotation + pos.yaw -= mouseX * mouseSensitivity; + pos.pitch -= mouseX * mouseSensitivity; + + // 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; + + 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; + + // 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); + + MVP = projection * view * transform; +} + +glm::mat4 PlayerCamera::getMVP() { return MVP; } diff --git a/src/PlayerCamera.h b/src/PlayerCamera.h index e6e88bf..b100a10 100644 --- a/src/PlayerCamera.h +++ b/src/PlayerCamera.h @@ -1,22 +1,26 @@ #pragma once +#include "Error.h" #include #include #include +#include #include #include class PlayerCamera { private: + // Error reporting class + Error error = Error("PlayerCamera"); // Constants for speed and sensitivity - const float movementSpeed = 0.05f; - const float mouseSensitivity = 0.05f; + const float movementSpeed = 0.005f; + const float mouseSensitivity = 0.005f; // Player position struct position { - glm::vec4 location; + glm::vec4 vectorPos; float pitch; float yaw; - }; + } pos; // Mouse position int mouseX, mouseY; diff --git a/src/main.cpp b/src/main.cpp index 04c55e9..4ac0e53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,4 @@ // Include Config header generated by GNU autotools -//#include "../config.h" -//#include "../config_file_paths.h" #include // Make sure Glew is loaded first #include @@ -16,6 +14,8 @@ #include // Shader #include "ShaderLoader.h" +// Camera +#include "PlayerCamera.h" // Include error class #include "Error.h" @@ -155,24 +155,26 @@ int main(int argc, char **argv) { // Mess with perspective // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 // unit <-> 100 units - float width = 4; - float height = 3; + float width = 800; + float height = 600; glm::mat4 Projection = glm::perspective( glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f); - // Camera matrix - glm::mat4 View = glm::lookAt( - glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space - glm::vec3(0, 0, 0), // and looks at the origin - glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down) - ); - // Model matrix : an identity matrix (model will be at the origin) - glm::mat4 Model = glm::mat4(1.0f); + //// Camera matrix + // glm::mat4 View = glm::lookAt( + // glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space + // glm::vec3(0, 0, 0), // and looks at the origin + // glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down) + //); + //// Model matrix : an identity matrix (model will be at the origin) + // glm::mat4 Model = glm::mat4(1.0f); - // Our ModelViewProjection : multiplication of our 3 matrices - glm::mat4 mvp = - Projection * View * - Model; // Remember, matrix multiplication is the other way around + //// Our ModelViewProjection : multiplication of our 3 matrices + // glm::mat4 mvp = + // Projection * View * + // Model; // Remember, matrix multiplication is the other way around + + PlayerCamera camera; // Game loop bool running = true; @@ -185,6 +187,8 @@ int main(int argc, char **argv) { } // TODO: Do something with keys lol }; + camera.tick(); + // Clear screen ready for next loop glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -192,7 +196,7 @@ int main(int argc, char **argv) { // glUseProgram(shaderProgram); // Send our glsl shader our mvp - shader.setMat4("MVP", mvp); + shader.setMat4("MVP", camera.getMVP()); shader.use(); // I think this is meant to be here but it breaks...