From cb26a0c9efa4ed1d1e81fbd7a41b0171f5823e36 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 29 Sep 2024 16:59:16 +0100 Subject: [PATCH] Extract getting uniform location into a function --- src/main.cc | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/main.cc b/src/main.cc index 07f6bda..9a8b589 100644 --- a/src/main.cc +++ b/src/main.cc @@ -120,6 +120,7 @@ void render_model (const Model &model, const Camera &camera) GLuint create_shader_program (const std::string &vertex_shader_source, const std::string &fragment_shader_source); GLuint compile_shader (GLuint type, const std::string &source); std::string load_shader (const std::string &filepath); +GLint get_uniform_location (GLint shader_program, const char *name); void handle_object_movement (App &app); void handle_camera_movement (App &app); glm::vec3 rotation_to_view_direction(const Camera &camera); @@ -313,23 +314,9 @@ void create_graphics_pipeline(Model &model, const char *vs_file, const char *fs_ model.mesh.shader_program = create_shader_program(vs_source, fs_source); - const char *u_model = "u_model"; - model.u_model_idx = glGetUniformLocation(model.mesh.shader_program, u_model); - if (model.u_model_idx < 0) { - printf("Failed to find uniform %s\n", u_model); - } - - const char *u_view = "u_view"; - model.u_view_idx = glGetUniformLocation(model.mesh.shader_program, u_view); - if (model.u_view_idx < 0) { - printf("Failed to find uniform %s\n", u_view); - } - - const char *u_projection = "u_projection"; - model.u_projection_idx = glGetUniformLocation(model.mesh.shader_program, u_projection); - if (model.u_projection_idx < 0) { - printf("Failed to find uniform %s\n", u_projection); - } + model.u_model_idx = get_uniform_location(model.mesh.shader_program, "u_model"); + model.u_view_idx = get_uniform_location(model.mesh.shader_program, "u_view"); + model.u_projection_idx = get_uniform_location(model.mesh.shader_program, "u_projection"); } void run_main_loop(App &app) { @@ -463,6 +450,16 @@ std::string load_shader(const std::string &filepath) { return output; } +GLint get_uniform_location(GLuint shader_program, const char *name) { + GLint location = glGetUniformLocation(shader_program, name); + if (location < 0) { + printf("Failed to find uniform %s\n", name); + return -1; + } + + return location; +} + // Example of moving an object void handle_object_movement(App &app) { switch (app.event.key.keysym.sym) {