Wednesday, April 8, 2009

Screen space water on GPU

I saw an interesting demo from NVIDIA in the OpenGL sdk using render to 3D texture functionality to run a realtime water simulation. I thought I would replicate the same thing in 2D atleast to get started. So here it is.

The reference for this was this wonderful article. The snapshot from the application is on the top. Here is how I implemented this on OpenGL. I created 3 textures and attached them to the three color attachments using the FBO. The first color attachement was for the 3D scene (the teapot and the plane). The second and third color attachments were for the water simulation. Basically I have simply converted the main code given in the article. See it for details in this regard. In the display function, first the scene is rendered and then the water simulation is rendered by ping pongging btw color attachment 1 and 2. The complete fragment shader for water is given below


fragment_out fragment_water( frag_vertex IN,
uniform sampler2D currentMap,
uniform sampler2D prevMap)
fragment_out OUT;
float4 smoothed;
float4 prev = tex2D(prevMap, IN.UV);

smoothed = tex2D(currentMap, IN.UV + float2(s.x, 0));
smoothed += tex2D(currentMap, IN.UV + float2(-s.x,0));
smoothed += tex2D(currentMap, IN.UV + float2(0, s.y));
smoothed += tex2D(currentMap, IN.UV + float2(0,-s.y));
smoothed /= 4.0;
OUT.Color = (smoothed*2.0 - prev)*damping;
return OUT;

After this, we have three rendered outputs the first one containing the rendering result, the second and third contain the water buffer. These textures are then used in another fragment shader to show the final output. The fragment shader is given below.


fragment_out fragment_main( frag_vertex IN,
uniform sampler2D buffer,
uniform sampler2D textureMap)
fragment_out OUT;
float Xoffset = tex2D(buffer, IN.UV+half2(-s.x, 0)) -
tex2D(buffer, IN.UV+half2(s.x, 0));
float Yoffset = tex2D(buffer, IN.UV+half2(0, -s.y)) -
tex2D(buffer, IN.UV+half2(0, s.y));
half4 t = tex2D(textureMap, IN.UV+half2(Xoffset,Yoffset));
OUT.Color = t+half4(Xoffset);
return OUT;

This gives the result shown in the image below.
Screen space water on GPU

Rest is just openGL bits and pieces.

Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.