Add an MVP projection matrix with adjustable FoV

This commit is contained in:
Kienan Stewart 2020-02-19 21:54:31 -05:00
parent baf1b1b808
commit f56d1e558e
3 changed files with 20 additions and 4 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Prerequestes
` sudo apt install libglm-dev`

View File

@ -9,7 +9,7 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#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<RegisteredShaderProgram> 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);

View File

@ -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;
}