]> git.tdb.fi Git - libs/gl.git/blobdiff - demos/forestpond/data/fluidsim_integrate.glsl
Implement water simulation and surface shader for forest pond
[libs/gl.git] / demos / forestpond / data / fluidsim_integrate.glsl
diff --git a/demos/forestpond/data/fluidsim_integrate.glsl b/demos/forestpond/data/fluidsim_integrate.glsl
new file mode 100644 (file)
index 0000000..f3805e8
--- /dev/null
@@ -0,0 +1,35 @@
+import fluidsim;
+
+#pragma MSP stage(compute)
+void main()
+{
+       ivec2 size = imageSize(surface_out);
+       ivec2 coord = ivec2(gl_GlobalInvocationID.xy)+1;
+       if(coord.x>=size.x-1 || coord.y>=size.y-1)
+               return;
+
+       float surface = texelFetch(surface_in, coord, 0).x;
+       float bottom = texelFetch(bottom_in, coord, 0).x;
+       float depth = surface-bottom;
+       vec2 velocity = texelFetch(velocity_in, coord, 0).xy;
+       float clamping = texelFetch(clamping_in, coord, 0).x;
+       float left_depth = get_depth(coord-ivec2(1, 0));
+       float left_clamping = texelFetch(clamping_in, coord-ivec2(1, 0), 0).x;
+       float left_vx = texelFetch(velocity_in, coord-ivec2(1, 0), 0).x;
+       float right_depth = get_depth(coord+ivec2(1, 0));
+       float right_clamping = texelFetch(clamping_in, coord+ivec2(1, 0), 0).x;
+       float down_depth = get_depth(coord-ivec2(0, 1));
+       float down_clamping = texelFetch(clamping_in, coord-ivec2(0, 1), 0).x;
+       float down_vy = texelFetch(velocity_in, coord-ivec2(0, 1), 0).y;
+       float up_depth = get_depth(coord+ivec2(0, 1));
+       float up_clamping = texelFetch(clamping_in, coord+ivec2(0, 1), 0).x;
+
+       // Integration step: change surface level based on velocities
+       float total_flow = left_vx*mix(left_depth*left_clamping, depth*clamping, left_vx<0)-
+               velocity.x*mix(depth*clamping, right_depth*right_clamping, velocity.x<0)+
+               down_vy*mix(down_depth*down_clamping, depth*clamping, down_vy<0)-
+               velocity.y*mix(depth*clamping, up_depth*up_clamping, velocity.y<0);
+       float new_surface = surface+total_flow*delta_time;
+
+       imageStore(surface_out, coord, vec4(new_surface, 0.0, 0.0, 0.0));
+}