Add uniform values

This commit is contained in:
Abdelrahman Said 2024-09-22 14:46:12 +01:00
parent ad6b809b86
commit abef6c368f
3 changed files with 25 additions and 2 deletions

View File

@ -3,6 +3,8 @@
in vec3 vert_colours; in vec3 vert_colours;
out vec4 color; out vec4 color;
uniform float u_offset;
void main() { void main() {
color = vec4(vert_colours.r, vert_colours.g, vert_colours.b, 1.0f); color = vec4(vert_colours.r, vert_colours.g + u_offset, vert_colours.b, 1.0f);
}; };

View File

@ -3,9 +3,11 @@
layout(location=0) in vec3 position; layout(location=0) in vec3 position;
layout(location=1) in vec3 vert_colour; layout(location=1) in vec3 vert_colour;
uniform float u_offset;
out vec3 vert_colours; out vec3 vert_colours;
void main() { void main() {
vert_colours = vert_colour; vert_colours = vert_colour;
gl_Position = vec4(position.x, position.y, position.z, 1.0f); gl_Position = vec4(position.x, position.y + u_offset, position.z, 1.0f);
}; };

View File

@ -1,3 +1,4 @@
#include "SDL_keycode.h"
#include "glad/glad.h" #include "glad/glad.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_error.h> #include <SDL2/SDL_error.h>
@ -41,6 +42,11 @@ struct App {
// OpenGL refers to as element, hence the name Element Buffer Object // OpenGL refers to as element, hence the name Element Buffer Object
GLuint ebo; GLuint ebo;
// offset value passed to shaders as uniform
float offset;
// offset uniform index
GLint u_offset_idx;
GLuint shader_program; GLuint shader_program;
bool running; bool running;
}; };
@ -191,6 +197,12 @@ void create_graphics_pipeline(App &app) {
const std::string fs_source = load_shader("shaders/frag.glsl"); const std::string fs_source = load_shader("shaders/frag.glsl");
app.shader_program = create_shader_program(vs_source, fs_source); 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);
}
} }
void run_main_loop(App &app) { void run_main_loop(App &app) {
@ -202,6 +214,12 @@ void run_main_loop(App &app) {
case SDL_QUIT: case SDL_QUIT:
app.running = false; app.running = false;
break; break;
case SDL_KEYDOWN:
if (app.event.key.keysym.sym == SDLK_UP) {
app.offset += 0.01f;
} else if (app.event.key.keysym.sym == SDLK_DOWN) {
app.offset -= 0.01f;
}
} }
} }
@ -214,6 +232,7 @@ void run_main_loop(App &app) {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glUseProgram(app.shader_program); glUseProgram(app.shader_program);
glUniform1f(app.u_offset_idx, app.offset);
// End pre draw setup // End pre draw setup
// Draw call // Draw call