diff --git a/CMakeLists.txt b/CMakeLists.txt index 764ade4..73331ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.21 FATAL_ERROR) project(Game) +set(CMAKE_CXX_STANDARD 17) file(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.c diff --git a/data/shaders/fragment.glsl b/data/shaders/fragment.glsl index cca90e6..85b1b60 100644 --- a/data/shaders/fragment.glsl +++ b/data/shaders/fragment.glsl @@ -1,4 +1,4 @@ -#version 330 core +#version 430 core out vec4 FragColor; in vec2 ourTexCoord; diff --git a/data/shaders/vertex.glsl b/data/shaders/vertex.glsl index ab367cb..3e70e4f 100644 --- a/data/shaders/vertex.glsl +++ b/data/shaders/vertex.glsl @@ -1,11 +1,14 @@ -#version 330 core +#version 430 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; +// View +layout (location = 2) uniform mat4 mvp; + out vec2 ourTexCoord; void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = mvp * vec4(aPos, 1.0); ourTexCoord = aTexCoord; }; diff --git a/src/Error.cpp b/src/Error.cpp index 789b296..aef26c8 100644 --- a/src/Error.cpp +++ b/src/Error.cpp @@ -14,5 +14,6 @@ void Error::warn(std::string msg) { // throw object + ": " + msg; } void Error::log(std::string msg) { - std::cout << object << ": \n" << msg << std::endl; + std::cout << object << std::endl; + std::cout << msg << std::endl; } diff --git a/src/PlayerCamera.cpp b/src/PlayerCamera.cpp index a9ddde5..3c08974 100644 --- a/src/PlayerCamera.cpp +++ b/src/PlayerCamera.cpp @@ -1,12 +1,41 @@ #include "PlayerCamera.h" PlayerCamera::PlayerCamera() { + this->error = new Error("PlayerCamera"); SDL_SetRelativeMouseMode(SDL_TRUE); SDL_GetRelativeMouseState(nullptr, nullptr); } -PlayerCamera::~PlayerCamera() {} +PlayerCamera::~PlayerCamera() { delete this->error; } -void PlayerCamera::tick() {} +void PlayerCamera::tick() { + SDL_GetRelativeMouseState(&mouseX, &mouseY); -glm::mat4 PlayerCamera::getMVP() {} + // adjust rotation based off mouse input + pos.yaw -= mouseX * mouseSensitivity; + pos.pitch -= mouseY * mouseSensitivity; + if (pos.pitch > maxPitch) + pos.pitch = maxPitch; + if (pos.pitch < -maxPitch) + pos.pitch = -maxPitch; + + // Get player forward vector and rotation matrix + glm::vec4 playerForward; + glm::mat4 playerRotation; + + playerRotation = glm::rotate(playerRotation, pos.yaw, glm::vec3(0, 1, 0)); + playerRotation = glm::rotate(playerRotation, pos.pitch, glm::vec3(1, 0, 0)); + playerForward = glm::vec4(0, 0, -1, 0) * playerRotation; + + // move camera + glm::mat4 view = + glm::lookAt(glm::vec3(pos.location), + glm::vec3(pos.location + playerForward), glm::vec3(0, 1, 0)); + glm::mat4 projection = + glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 1000.0f); + glm::mat4 transform; + + MVP = projection * view * transform; +} + +glm::mat4 PlayerCamera::getMVP() { return MVP; } diff --git a/src/PlayerCamera.h b/src/PlayerCamera.h index e6e88bf..bf9bb8d 100644 --- a/src/PlayerCamera.h +++ b/src/PlayerCamera.h @@ -1,8 +1,9 @@ #pragma once +#include "Error.h" #include #include +#include #include -#include #include class PlayerCamera { @@ -10,13 +11,14 @@ private: // Constants for speed and sensitivity const float movementSpeed = 0.05f; const float mouseSensitivity = 0.05f; + const float maxPitch = glm::radians(89.0f); // Player position - struct position { + struct CameraPosition { glm::vec4 location; float pitch; float yaw; - }; + } pos; // Mouse position int mouseX, mouseY; @@ -24,6 +26,9 @@ private: // Camera position glm::mat4 MVP; + // Error + Error *error = nullptr; + public: PlayerCamera(); diff --git a/src/ShaderLoader.cpp b/src/ShaderLoader.cpp index fc34daa..fd40df9 100644 --- a/src/ShaderLoader.cpp +++ b/src/ShaderLoader.cpp @@ -88,3 +88,4 @@ void ShaderLoader::setInt(const std::string &name, int value) const { void ShaderLoader::setFloat(const std::string &name, float value) const { glUniform1f(glGetUniformLocation(ID, name.c_str()), value); } +unsigned int ShaderLoader::getShaderProgramID() { return ID; } diff --git a/src/ShaderLoader.h b/src/ShaderLoader.h index 38a9d01..2a58a51 100644 --- a/src/ShaderLoader.h +++ b/src/ShaderLoader.h @@ -21,6 +21,9 @@ public: // use the shader void use(); + // Get shader id until I figure out uniforms + unsigned int getShaderProgramID(); + // utility uniform functions void setBool(const std::string &name, bool value) const; void setInt(const std::string &name, int value) const; diff --git a/src/main.cpp b/src/main.cpp index 8716c97..c7d3e7d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,10 +11,13 @@ //#include // File reader #include -#include #include // Shader #include "ShaderLoader.h" +#include + +// Camera postioning +#include "PlayerCamera.h" // Include error class #include "Error.h" @@ -53,31 +56,34 @@ int main(int argc, char **argv) { // TODO: Test that glContext was created successfully // 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: " + std::to_string(nrAttributes)); // Initialise Glew - if (glewInit() != GLEW_OK) { + GLenum glewStatus = glewInit(); + if (glewStatus != GLEW_OK) { + // TODO make this work. I mean ffs if glew brakes how will I debug it. + // auto errorMsg = glewGetErrorString(glewStatus); + // error.log(errorMsg); error.crash("Unable to initialise GLEW (OpenGL Extention Wrangler)"); } // Create event handling struct SDL_Event input; - ShaderLoader shader("../data/shaders" + ShaderLoader shader("./data/shaders" "/vertex.glsl", - "../data/shaders" + "./data/shaders" "/fragment.glsl"); float vertices[] = { // positions // texture Co-ords - 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // top right - 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // top left + 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, // top right + 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, // bottom right + -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // bottom left + -0.5f, 0.5f, 1.0f, 0.0f, 1.0f // top left }; unsigned int indices[] = { @@ -125,7 +131,7 @@ int main(int argc, char **argv) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Load texture image - SDL_Surface *image = IMG_Load("../data" + SDL_Surface *image = IMG_Load("./data" "/container.jpg"); if (image == nullptr) { error.crash("SDL2_image was unable to load a texture", IMG_GetError()); @@ -151,6 +157,11 @@ int main(int argc, char **argv) { SDL_FreeSurface(image); image = nullptr; + // Finally create camera object + PlayerCamera camera; + + GLuint mvpLocation = glGetUniformLocation(shader.getShaderProgramID(), "mvp"); + // Game loop bool running = true; while (running) { @@ -162,12 +173,18 @@ int main(int argc, char **argv) { } // TODO: Do something with keys lol }; - // Clear screen ready for next loop + // Perform game logic + camera.tick(); + + // Clear screen ready for this loop glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Make every shader/rendering call from this point on use our shader // glUseProgram(shaderProgram); + glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, + glm::value_ptr(camera.getMVP())); + // use our shader shader.use(); // I think this is meant to be here but it breaks...