From 8738f57c383ec1eaf26dc877fe88c39b5cbbf217 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 22 Sep 2024 15:56:07 +0100 Subject: [PATCH] Switch to translating the quad using a translation matrix --- shaders/frag.glsl | 4 +--- shaders/vert.glsl | 4 ++-- src/main.cc | 35 ++++++++++++++++++++--------------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/shaders/frag.glsl b/shaders/frag.glsl index a1f2128..2730cb0 100644 --- a/shaders/frag.glsl +++ b/shaders/frag.glsl @@ -3,8 +3,6 @@ in vec3 vert_colours; out vec4 color; -uniform float u_offset; - void main() { - color = vec4(vert_colours.r, vert_colours.g + u_offset, vert_colours.b, 1.0f); + color = vec4(vert_colours.r, vert_colours.g, vert_colours.b, 1.0f); }; diff --git a/shaders/vert.glsl b/shaders/vert.glsl index 526d9a3..2c9821a 100644 --- a/shaders/vert.glsl +++ b/shaders/vert.glsl @@ -3,11 +3,11 @@ layout(location=0) in vec3 position; layout(location=1) in vec3 vert_colour; -uniform float u_offset; +uniform mat4 u_model; out vec3 vert_colours; void main() { vert_colours = vert_colour; - gl_Position = vec4(position.x, position.y + u_offset, position.z, 1.0f); + gl_Position = u_model * vec4(position, 1.0f); }; diff --git a/src/main.cc b/src/main.cc index 87c6427..b7d6a3b 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,9 +1,13 @@ #include "glad/glad.h" +#include "glm/gtc/type_ptr.hpp" #include #include #include #include #include +#include +#include +#include #include #include #include @@ -41,13 +45,13 @@ struct App { // OpenGL refers to as element, hence the name Element Buffer Object GLuint ebo; - float offset_modifier; - // offset value passed to shaders as uniform - float offset; - // offset uniform index - GLint u_offset_idx; - GLuint shader_program; + + float speed; + // Model matrix to be sent as uniform to the vertex shader + glm::mat4 model_mat; + GLint u_model_idx; + bool running; }; @@ -119,7 +123,8 @@ int init(App &app) { glGetString(GL_SHADING_LANGUAGE_VERSION) ); - app.offset_modifier = 0.02f; + app.speed = 0.02f; + app.model_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f)); return EXIT_CODE_SUCCESS; } @@ -200,10 +205,10 @@ void create_graphics_pipeline(App &app) { app.shader_program = create_shader_program(vs_source, fs_source); - const char *u_offset = "u_offset"; - app.u_offset_idx = glGetUniformLocation(app.shader_program, u_offset); - if (app.u_offset_idx < 0) { - printf("Can't find uniform %s", u_offset); + const char *u_model = "u_model"; + app.u_model_idx = glGetUniformLocation(app.shader_program, u_model); + if (app.u_model_idx < 0) { + printf("Failed to find uniform %s\n", u_model); } } @@ -218,9 +223,9 @@ void run_main_loop(App &app) { break; case SDL_KEYDOWN: if (app.event.key.keysym.sym == SDLK_UP) { - app.offset += app.offset_modifier; + app.model_mat = glm::translate(app.model_mat, glm::vec3(0.0f, app.speed, 0.0f)); } else if (app.event.key.keysym.sym == SDLK_DOWN) { - app.offset -= app.offset_modifier; + app.model_mat = glm::translate(app.model_mat, glm::vec3(0.0f, -app.speed, 0.0f)); } } } @@ -234,8 +239,8 @@ void run_main_loop(App &app) { glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glUseProgram(app.shader_program); - if (app.u_offset_idx >= 0) { - glUniform1f(app.u_offset_idx, app.offset); + if (app.u_model_idx >= 0) { + glUniformMatrix4fv(app.u_model_idx, 1, GL_FALSE, glm::value_ptr(app.model_mat)); } // End pre draw setup