commit f88ecdf4460ef265e632a6f65be1961c3a0ccd17
parent 8aec80e25dc2d5e2c759239e36e080ba54f3a09b
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Thu, 1 Aug 2024 12:00:54 -0300
bind key to reload shaders
Diffstat:
1 file changed, 25 insertions(+), 5 deletions(-)
diff --git a/opengl-example/main.c b/opengl-example/main.c
@@ -136,6 +136,24 @@ bool new_program_from_shaders_source(GLuint *shader_program,
return true;
}
+GLuint current_program;
+
+bool reload_shaders(const char *vert_shader_file_path, const char *frag_shader_file_path) {
+ LOG_INFO("Reloading shaders");
+
+ GLuint new_program;
+ if(!new_program_from_shaders_source(&new_program,
+ vert_shader_file_path, frag_shader_file_path)) {
+
+ LOG_ERROR("Could not recompile shaders. Keeping previous version");
+ return false;
+ }
+ glDeleteProgram(current_program);
+ current_program = new_program;
+ LOG_INFO("Done reloading shaders");
+ return true;
+}
+
// https://www.glfw.org/docs/3.3/group__keys.html
static void key_handler(GLFWwindow *window,
int key, int scancode, int action, int mods) {
@@ -146,13 +164,15 @@ static void key_handler(GLFWwindow *window,
case GLFW_KEY_Q:
glfwSetWindowShouldClose(window, GLFW_TRUE);
break;
+ case GLFW_KEY_F5:
+ reload_shaders("main.vert", "main.frag");
+ break;
default:
break;
}
}
}
-
bool setup_glfw(GLFWwindow **window) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@@ -182,7 +202,6 @@ bool setup_glfw(GLFWwindow **window) {
}
int main (void) {
- GLuint shader_program;
GLuint VBO, VAO; // Vertex Buffer Objects, Vertex Array Object
GLFWwindow *window;
@@ -201,7 +220,7 @@ int main (void) {
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
- if(!new_program_from_shaders_source(&shader_program, "main.vert", "main.frag")) {
+ if(!new_program_from_shaders_source(¤t_program, "main.vert", "main.frag")) {
glfwDestroyWindow(window);
glfwTerminate();
return -1;
@@ -209,11 +228,12 @@ int main (void) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
- glUseProgram(shader_program);
while(!glfwWindowShouldClose(window)) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
+
+ glUseProgram(current_program);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
@@ -221,7 +241,7 @@ int main (void) {
}
LOG_INFO("Freeing resources");
- glDeleteProgram(shader_program);
+ glDeleteProgram(current_program);
glfwDestroyWindow(window);
glfwTerminate();
return 0;