From 0c1bd92d173f036e04a6ab368c60663caf462c3d Mon Sep 17 00:00:00 2001 From: 0xdeadbeer <64986162+0xdeadbeer@users.noreply.github.com> Date: Thu, 12 Oct 2023 19:43:06 +0200 Subject: [PATCH] Main: implement hotreloading shortcut for shaders --- main.c | 96 ++++++++++++++++++++++++++++++++++------------------- shader.frag | 6 ++-- shader.vert | 4 +++ 3 files changed, 70 insertions(+), 36 deletions(-) diff --git a/main.c b/main.c index 87b1f0d..1ca0630 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,9 @@ #include #include #include +#include +#include +#include unsigned int vao; unsigned int vbo; @@ -8,13 +11,14 @@ unsigned int shader_program; unsigned int vertex_shader; unsigned int fragment_shader; -const char *vertex_shader_location = "./shader.vert"; -const char *fragment_shader_location = "./shader.frag"; +const char *vertex_shader_location = "../shader.vert"; +const char *fragment_shader_location = "../shader.frag"; float vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f + // positions // colors + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top }; int load_shader(const char *path, unsigned int shader) { @@ -69,7 +73,23 @@ int load_shader(const char *path, unsigned int shader) { return 0; } -int setup() { +void display() { + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(shader_program); + glBindVertexArray(vao); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glutSwapBuffers(); +} + +int load_shaders() { + glDeleteProgram(shader_program); + shader_program = glCreateProgram(); + + // create and load new shaders vertex_shader = glCreateShader(GL_VERTEX_SHADER); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); @@ -81,7 +101,6 @@ int setup() { return -1; } - shader_program = glCreateProgram(); glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); glLinkProgram(shader_program); @@ -100,29 +119,14 @@ int setup() { return -1; } - glGenVertexArrays(1, &vao); - glGenBuffers(1, &vbo); - - // RUD glDeleteShader(vertex_shader); glDeleteShader(fragment_shader); + display(); + return 0; } -int post_setup() { - glBindVertexArray(vao); - - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*) 0); - glEnableVertexAttribArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); -} - void keyboard(unsigned char key, int x, int y) { switch (key) { case '\x1B': @@ -130,20 +134,47 @@ void keyboard(unsigned char key, int x, int y) { exit(EXIT_SUCCESS); break; } + case 'r': + case 'R': + if (load_shaders() != 0) { + fprintf(stderr, "Error: reloading shaders"); + break; + } + + fprintf(stdout, "Status: successfully reloaded shaders"); + + break; + default: + break; } } +int setup() { + if (load_shaders() != 0) { + fprintf(stderr, "Error: loading shaders\n"); + return -1; + } -void display() { - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT); + glGenVertexArrays(1, &vao); + glGenBuffers(1, &vbo); - glUseProgram(shader_program); + return 0; +} + +void post_setup() { glBindVertexArray(vao); - glDrawArrays(GL_TRIANGLES, 0, 3); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glutSwapBuffers(); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) 0); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *) (3 * sizeof(float))); + glEnableVertexAttribArray(1); + + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); } int main(int argc, char **argv) { @@ -165,10 +196,7 @@ int main(int argc, char **argv) { return -1; } - if (post_setup() == -1) { - fprintf(stderr, "Error: Failed to post-setup\n"); - return -1; - } + post_setup(); glutMainLoop(); diff --git a/shader.frag b/shader.frag index 6a826ed..38e8927 100644 --- a/shader.frag +++ b/shader.frag @@ -1,6 +1,8 @@ #version 330 core -out vec4 FragColor; +in vec3 color; +out vec4 output; void main() { - FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f); +// output = vec4(color.xyz, 1.0f); + output = vec4(1.0, 1.0, 1.0, 1.0); } \ No newline at end of file diff --git a/shader.vert b/shader.vert index 3c19984..7c49743 100644 --- a/shader.vert +++ b/shader.vert @@ -1,6 +1,10 @@ #version 330 core layout (location = 0) in vec3 pos; +layout (location = 1) in vec3 col; + +out vec3 color; void main() { gl_Position = vec4(pos.x, pos.y, pos.z, 1.0f); + color = col; } \ No newline at end of file