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