diff --git a/CMakeLists.txt b/CMakeLists.txt index 221174e..0ff31f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(Fall-Fever ShaderProgram.cpp VertexBuffer.cpp Texture.cpp + Camera.cpp ) target_link_libraries( diff --git a/Camera.cpp b/Camera.cpp new file mode 100644 index 0000000..4bf867e --- /dev/null +++ b/Camera.cpp @@ -0,0 +1,29 @@ +#include "Camera.h" + +#include +#include + +Camera::Camera(float fov, int width, int height) { + //projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f); + this->fov = fov; + projectionMatrix = glm::perspective(this->fov/2.0f, (float)width / (float)height, 0.1f, 1000.0f); + viewMatrix = glm::mat4(1.0f); + updateVPM(); +} + +glm::mat4 Camera::getViewProj() { + return viewProjectionMatrix; +} + +void Camera::updateVPM() { + viewProjectionMatrix = projectionMatrix * viewMatrix; +} + +void Camera::updateAspectRatio(int width, int height) { + projectionMatrix = glm::perspective(fov/2.0f, (float)width / (float)height, 0.1f, 1000.0f); + updateVPM(); +} + +void Camera::translate(glm::vec3 vector) { + viewMatrix = glm::translate(viewMatrix, vector * -1.0f); +} diff --git a/Camera.h b/Camera.h new file mode 100644 index 0000000..363d932 --- /dev/null +++ b/Camera.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +class Camera { + +public: + + Camera(float fov, int width, int height); + ~Camera() = default; + + glm::mat4 getViewProj(); + void updateVPM(); + void updateAspectRatio(int width, int height); + + void translate(glm::vec3 vector); + + +private: + + glm::mat4 viewMatrix; + glm::mat4 projectionMatrix; + + glm::mat4 viewProjectionMatrix; + + float fov; + +}; diff --git a/Controller.cpp b/Controller.cpp index ee45cde..4c7f0bc 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -13,6 +13,7 @@ #endif #include "Controller.h" +#include "Camera.h" #include "Texture.h" Controller::Controller() { @@ -49,7 +50,7 @@ void Controller::run() { 1.0f, 0.0f, 0.0f, 1.0f}, Vertex{0.5f, -0.5f, 0.0f, 1.0f, 0.0f, - 0.0f, 1.0f,0.0f, 1.0f}, + 0.0f, 1.0f, 0.0f, 1.0f}, Vertex{-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f}, @@ -70,6 +71,10 @@ void Controller::run() { Texture tex1("res/tex.png", shaderProgram.getShaderProgramId()); + glm::mat4 model = glm::mat4(1.0f); + Camera cam1(90.0f, gameWindow->getWindowWidth(), gameWindow->getWindowHeight()); + cam1.translate(glm::vec3(0.0f, 0.0f, 1.5f)); + // This is the game loop while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) { // Timing @@ -85,9 +90,10 @@ void Controller::run() { // Update game // ... - glm::mat4 trans_mat = glm::mat4(1.0f); - trans_mat = glm::rotate(trans_mat, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f)); - shaderProgram.setUniform("transform", trans_mat); + model = glm::rotate(model, (float)this->deltaTime, glm::vec3(0.0f, 1.0f, 0.0f)); + cam1.updateVPM(); + glm::mat4 modelViewProj = cam1.getViewProj() * model; + shaderProgram.setUniform("u_modelViewProj", modelViewProj); // Render and buffer swap glClear(GL_COLOR_BUFFER_BIT); @@ -102,6 +108,13 @@ void Controller::run() { // Check events, handle input gameEventHandler->handleEvents(); + + // Update window size + int new_window_width, new_window_height; + glfwGetFramebufferSize(gameWindow->getGLFWwindow(), &new_window_width, &new_window_height); + gameWindow->setWindowDimensions(new_window_width, new_window_height); + glViewport(0, 0, new_window_width, new_window_height); + cam1.updateAspectRatio(new_window_width, new_window_height); } } diff --git a/ShaderProgram.cpp b/ShaderProgram.cpp index 2ef1892..72b3ecf 100644 --- a/ShaderProgram.cpp +++ b/ShaderProgram.cpp @@ -9,10 +9,10 @@ ShaderProgram::ShaderProgram(const char* vertexShaderPath, const char* fragmentS shaderProgramId = createShader(vertexShaderPath, fragmentShaderPath); // Set transformation matrix as default to identity matrix - this->bind(); + bind(); glm::mat4 identity_matrix = glm::mat4(1.0f); - this->setUniform("transform", identity_matrix); - this->unbind(); + setUniform("transform", identity_matrix); + unbind(); } ShaderProgram::~ShaderProgram() { diff --git a/Window.cpp b/Window.cpp index ea10a7c..6fd6d24 100644 --- a/Window.cpp +++ b/Window.cpp @@ -30,7 +30,8 @@ Window::Window() { glViewport(0, 0, width, height); // Tell GLFW which function to call when window is resized - glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + // Currently not used... + // glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); } diff --git a/Window.h b/Window.h index 7523fca..c91cf3d 100644 --- a/Window.h +++ b/Window.h @@ -11,6 +11,11 @@ public: GLFWwindow * getGLFWwindow() { return window; } + void setWindowDimensions(int width, int height) {this->width = width; this->height = height;} + + int getWindowWidth() { return width; } + int getWindowHeight() { return height; } + private: static void framebuffer_size_callback(GLFWwindow* window, int width, int height); static void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam); diff --git a/res/shaders/basic.vs b/res/shaders/basic.vs index b2bb314..4e415be 100644 --- a/res/shaders/basic.vs +++ b/res/shaders/basic.vs @@ -7,10 +7,10 @@ layout(location = 2) in vec4 a_color; out vec4 v_color; out vec2 v_texCoord; -uniform mat4 transform; +uniform mat4 u_modelViewProj; void main() { - gl_Position = transform * vec4(a_position, 1.0f); + gl_Position = u_modelViewProj * vec4(a_position, 1.0f); v_texCoord = a_texCoord; v_color = a_color; }