From f56d1e558efe36a700a8fe67927a2f2e44f88eb8 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Wed, 19 Feb 2020 21:54:31 -0500 Subject: [PATCH] Add an MVP projection matrix with adjustable FoV --- README.md | 3 +++ src/client/client_imgui.cpp | 19 +++++++++++++++---- src/shaders/defaultVertexShader.glsl | 2 ++ 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..35a615c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Prerequestes + +` sudo apt install libglm-dev` diff --git a/src/client/client_imgui.cpp b/src/client/client_imgui.cpp index cd66bee..2d6577c 100644 --- a/src/client/client_imgui.cpp +++ b/src/client/client_imgui.cpp @@ -9,7 +9,7 @@ #include #include - +#include #include "common/logging.hpp" #include "common/messaging/bus.hpp" @@ -78,7 +78,7 @@ int main(int argc, char** argv) int height = 0, width = 0; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); - + float fov = 45.0; // Triangle example // @see http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ GLuint VertexArrayID; @@ -109,6 +109,11 @@ int main(int argc, char** argv) static std::vector shader_program_registry; loadShaderProgramsFromFile("shaders/shader_programs.txt", &shader_registry, &shader_program_registry); messager.registerCallback(&debug_messagebus_callback); + int window_height, window_width; + glm::mat4 projection; + glm::mat4 view = glm::lookAt(glm::vec3(4,3,3), glm::vec3(0,0,0), glm::vec3(0,1,0)); + glm::mat4 model_default = glm::mat4(1.0f); + glm::mat4 mvp_matrix; while(!glfwWindowShouldClose(window)) { glfwPollEvents(); // Process input. @@ -127,10 +132,17 @@ int main(int argc, char** argv) glClear(GL_COLOR_BUFFER_BIT); // Render game geometries + glfwGetWindowSize(window, &window_width, &window_height); + // 45d FoV + projection = glm::perspective(glm::radians(fov), (float) window_width / (float) window_height, + 0.1f, 100.0f); + mvp_matrix = projection * view * model_default; // @see https://blog.conan.io/2019/06/26/An-introduction-to-the-Dear-ImGui-library.html // 1st attribute buffer : vertices GLuint p = find_shader_program_by_name("default", &shader_program_registry); glUseProgram(p); + GLuint matrix_id = glGetUniformLocation(p, "MVP"); + glUniformMatrix4fv(matrix_id, 1, GL_FALSE, &mvp_matrix[0][0]); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( @@ -151,12 +163,11 @@ int main(int argc, char** argv) ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); { - static float f = 0.0f; static int counter = 0; ImGui::Begin("Misc"); // Create a window called "Hello, world!" and append into it. - ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f + ImGui::SliderFloat("Field of View (FOV)", &fov, 45.0f, 180.0f); // Edit 1 float using a slider from 0.0f to 1.0f ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); diff --git a/src/shaders/defaultVertexShader.glsl b/src/shaders/defaultVertexShader.glsl index 67b0e7b..91573de 100644 --- a/src/shaders/defaultVertexShader.glsl +++ b/src/shaders/defaultVertexShader.glsl @@ -1,6 +1,8 @@ #version 300 es layout(location = 0) in vec3 vertexPosition_modelspace; +uniform mat4 MVP; void main(){ gl_Position.xyz = vertexPosition_modelspace; gl_Position.w = 1.0; + gl_Position = MVP * gl_Position; }