opengl-triangle

Basic application that draws triangle on a window using opengl
Index Commits Files Refs
commit f5c66e8493b7da7ad0feead22efca3524eafba6c
parent ba07863a98d7844324f799f27c7352d589149316
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Thu,  1 Aug 2024 20:50:38 -0300

add rainboy effects to triangle

Diffstat:
Mmain.c | 14++++++++++----
Mmain.frag | 10+++++++++-
2 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/main.c b/main.c
@@ -220,20 +220,26 @@ int main (void) {
     glGenVertexArrays(1, &VAO);
     glBindVertexArray(VAO);
 
+    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
+    glEnableVertexAttribArray(0);
+
     if(!new_program_from_shaders_source(&current_program, "main.vert", "main.frag")) {
         glfwDestroyWindow(window);
         glfwTerminate();
         return -1;
     }
 
-    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
-    glEnableVertexAttribArray(0);
-
+    float time;
+    int time_uniform = glGetUniformLocation(current_program, "time");
     while(!glfwWindowShouldClose(window)) {
-        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
+        // glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
+        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
         glClear(GL_COLOR_BUFFER_BIT);
 
+        time = glfwGetTime();
         glUseProgram(current_program);
+        glUniform1f(time_uniform, time);
+        
         glBindVertexArray(VAO);
         glDrawArrays(GL_TRIANGLES, 0, 3);
         glfwSwapBuffers(window);
diff --git a/main.frag b/main.frag
@@ -2,6 +2,14 @@
 
 // out vec4 fragColor;
 
+uniform float time;
+
+vec3 hsl2rgb(vec3 c) {
+    vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0);
+    return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));
+}
+
 void main() {
-    gl_FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
+    vec4 rainbow = vec4(hsl2rgb(vec3(time*0.10, 1.0, 0.5)), 1.0);
+    gl_FragColor = rainbow;
 }