Waterflow
Visualize water in terrain
src/shaders/advectVelocityXShader.comp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 #version 430
00006 
00007 layout(local_size_x = 16, local_size_y = 16) in; 
00008 
00009 layout  (std430,binding = 4) readonly buffer height0 
00010 {
00011     float water0[];
00012     
00013 };
00014 
00015 layout (std430, binding = 5) readonly buffer height1 
00016 {
00017     float water1[]; 
00018 };
00019 
00020 layout (std430,binding = 6) readonly buffer velocityX 
00021 {
00022     float vxin[];
00023     
00024 };
00025 
00026 layout (std430,binding = 7) writeonly buffer velocityXout 
00027 {
00028     float vxout[];
00029     
00030 };
00031 
00032 
00033 layout (std430,binding = 8) readonly buffer velocityY 
00034 {   
00035     float vyin[];
00036     
00037 };
00038 
00039 layout (std430,binding = 10) readonly buffer Terrain 
00040 {   
00041     float terr[];
00042     
00043 };
00044 
00045 
00046 uniform ivec2 size; 
00047 uniform float dt; 
00048 
00052 float average(int i, int j){ 
00053     i = clamp(i,0,size.x-1);
00054     j = clamp(j,0,size.y-1);
00055     int ipos = clamp(i+1,0,size.x-1);
00056     int jpos = clamp(j+1,0,size.y-1);
00057     int imin = clamp(i-1,0,size.x-1);
00058     int jmin = clamp(j-1,0,size.y-1);
00059         
00060     int offset = i+ j*size.x;
00061     int offsete = ipos+j*size.x;
00062     int offsetw = imin+j*size.x;
00063     int offsetn = i+jpos*size.x;
00064     int offsets = i+jmin*size.x;
00065 
00066     return 0.2f*(vxin[offset] +vxin[offsete] +vxin[offsetw] +vxin[offsetn] +vxin[offsets]); 
00067 
00068 }
00069 
00073 float bilinjearInterpolation(float point_x, float point_y)
00074 {
00075     //picking out the nearby points
00076     int x = int(point_x);
00077     int y = int(point_y);
00078 
00079     //picking out the distances to the point from nearby points
00080     float s1 = point_x - float(x);
00081     float s0 = 1.0f - s1;
00082 
00083     float t1 = point_y - float(y);
00084     float t0 = 1.0f - t1;
00085 
00086     int ypos = clamp(y+1,0,size.y-1);
00087     int xpos = clamp(x+1,0,size.x-1);
00088     
00089     
00090     int offset0 = x +y*size.x;
00091     int offset1 = x +(ypos)*size.x;
00092     int offset2 = xpos +y*size.x;
00093     int offset3 = xpos +(ypos)*size.x;
00094 
00095 
00096     return s0*(t0*vxin[offset0] + t1*vxin[offset1]) +
00097         s1*(t0*vxin[offset2] + t1*vxin[offset3]);
00098     
00099 }
00100 
00101 
00102 void main(){
00103     //determine where to sample
00104     ivec2 storePos = ivec2(gl_GlobalInvocationID.xy);
00105     int i = storePos.x;
00106     int j = storePos.y;
00107     int offset = (i + j*size.x);
00108 
00109     //Change these to clamps.
00110     if(i < size.x && j < size.y) {
00111         int ipos = clamp(i+1,0,size.x-1);
00112         int jpos = clamp(j+1,0,size.y-1);
00113         int imin = clamp(i-1,0,size.x-1);
00114         int jmin = clamp(j-1,0,size.y-1);
00115         
00116         float u = 0.0f; // Temporary velocity in x
00117         float v = 0.0f; // Temporary velocity in y 
00118         
00119         //Coordinate system HERE! <  is to the right and up.
00120 
00121         int offsetright = ipos+j*size.x;
00122         int offsetleft = imin+j*size.x;
00123         int offsetup = i+jpos*size.x;
00124         int offsetdown = i+jmin*size.x;
00125         int offsettopright = ipos+jpos*size.x;
00126         int sampX,sampY;
00127         
00128         
00129         //v = (vyin[offset] + vyin[offsetright] + vyin[offsetup] +vyin[offsettopright])*0.25f;
00130         v = (vyin[offset] + vyin[offsetright] + vyin[offsetup] + vyin[offsetleft] + vyin[offsetdown])*0.2f;
00131         u = vxin[offset];
00132 
00133         
00134         float source_point_x = clamp(float(i)-u*dt,0.0f,float(size.x)-1.0f);//   clamp(float(i)-u*dt, float(i)-1.0f,float(i)+1.0f); // 
00135         float source_point_y = clamp(float(j)-v*dt,0.0f,float(size.y)-1.0f); // clamp(float(j)-v*dt, float(j)-1.0f,float(j)+1.0f); //
00136         vxout[offset] = bilinjearInterpolation(source_point_x, source_point_y);
00137         
00138         
00139         
00140     }
00141     
00142     if(j < 2 || j ==size.y -1){
00143         vxout[offset] = 0.0f;
00144     }
00145 }
 All Classes Files Functions Variables Enumerations