TA159

Notas, resueltos y trabajos practicos de la materia Sistemas Gráficos
Index Commits Files Refs Submodules README LICENSE
commit c0caee6deadf483548200f186b34b15402793650
parent f88ecdf4460ef265e632a6f65be1961c3a0ccd17
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Thu,  1 Aug 2024 20:50:38 -0300

add rainboy effects to triangle

Diffstat:
Mopengl-example/main.c | 14++++++++++----
Mopengl-example/main.frag | 10+++++++++-
2 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/opengl-example/main.c b/opengl-example/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/opengl-example/main.frag b/opengl-example/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;
 }