Extract getting uniform location into a function

This commit is contained in:
Abdelrahman Said 2024-09-29 16:59:16 +01:00
parent deed9a42af
commit cb26a0c9ef

View File

@ -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) {