Browse Source

Add mouse support to shaders

rexim 4 years ago
parent
commit
d2cd5f84f8
2 changed files with 14 additions and 4 deletions
  1. 3 0
      main.c
  2. 11 4
      main.frag

+ 3 - 0
main.c

@@ -286,6 +286,9 @@ int main()
             glfwGetWindowSize(window, &width, &height);
             glfwGetWindowSize(window, &width, &height);
             glUniform2f(main_uniforms[RESOLUTION_UNIFORM], width, height);
             glUniform2f(main_uniforms[RESOLUTION_UNIFORM], width, height);
             glUniform1f(main_uniforms[TIME_UNIFORM], time);
             glUniform1f(main_uniforms[TIME_UNIFORM], time);
+            double xpos, ypos;
+            glfwGetCursorPos(window, &xpos, &ypos);
+            glUniform2f(main_uniforms[MOUSE_UNIFORM], xpos, height - ypos);
             glDrawArraysInstancedEXT(GL_TRIANGLE_STRIP, 0, 4, 1);
             glDrawArraysInstancedEXT(GL_TRIANGLE_STRIP, 0, 4, 1);
         }
         }
 
 

+ 11 - 4
main.frag

@@ -4,14 +4,21 @@ precision mediump float;
 
 
 uniform vec2 resolution;
 uniform vec2 resolution;
 uniform float time;
 uniform float time;
+uniform vec2 mouse;
 
 
 out vec4 color;
 out vec4 color;
 
 
+#define R 500.0
+
 void main(void) {
 void main(void) {
-    vec2 coord = gl_FragCoord.xy / resolution;
+    vec2 coord_uv = gl_FragCoord.xy / resolution;
+    vec2 mouse_uv = mouse / resolution;
+
+    float t = 1.0 - min(length(coord_uv - mouse_uv), R) / R;
+
     color = vec4(
     color = vec4(
-        (sin(coord.x + time) + 1.0) / 2.0,
-        (cos(coord.y + time) + 1.0) / 2.0,
-        (cos(coord.x + coord.y + time) + 1.0) / 2.0,
+        (sin(t*(coord_uv.x + time)) + 1.0) / 2.0,
+        (cos(t*(coord_uv.y + time)) + 1.0) / 2.0,
+        (cos(t*(coord_uv.x + coord_uv.y + time)) + 1.0) / 2.0,
         1.0);
         1.0);
 }
 }