Waterflow
Visualize water in terrain
src/shaders/heightMap.comp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #version 430
00010 
00011 layout(local_size_x = 16, local_size_y = 16) in; 
00012 
00013 layout (std430, binding = 0) writeonly buffer Pos  
00014 {
00015     float position[];
00016 };
00017     
00018 layout (std430, binding = 1) writeonly buffer TexCoord  
00019 {
00020     vec2 texCoord[];
00021 };
00022 
00023 layout (std430, binding = 2) writeonly buffer index  
00024 {   
00025     int indices[];
00026 };
00027 
00028 layout (std430, binding = 3) readonly buffer height  
00029 {   
00030     float terrHeight[];
00031 };
00032 
00033 uniform ivec2 size; 
00034 uniform float scale; 
00035 layout (rgba32f, binding = 0) readonly uniform image2D textureOut; 
00036 
00037 void main() {
00038     //determine where to sample
00039     ivec2 storePos = ivec2(gl_GlobalInvocationID.xy);  
00040 
00041     int x = clamp(storePos.x, 0, size.x-1);  
00042     int y = clamp(storePos.y, 0, size.y-1);  
00043     int xplus = clamp(x + 1, 0, size.x-1);  
00044     int xminus = clamp(x - 1, 0, size.x-1);  
00045     int yplus = clamp(y + 1, 0, size.y-1);  
00046     int yminus = clamp(y - 1, 0, size.y-1);   
00047 
00048     int offset = (x + y * size.x) * 3; 
00049     
00050     float terr = imageLoad(textureOut,ivec2(x,y)).w; 
00051     
00052     float ourTerr = terrHeight[x+y*size.x]; 
00053     
00054     //write to position (x,z) from where we read, y is data we read.
00055     position[offset + 0] = float(x); 
00056     if(scale  < 0.0f){
00057         position[offset + 1] =  ourTerr;
00058     }else{
00059         position[offset + 1] =  (ourTerr  <  terr*scale ) ? ourTerr-100.0f  : ourTerr;
00060     }
00061     position[offset + 2] = float(y);
00062 
00063     //texCoord = x/width, z/height (typ)
00064     texCoord[offset / 3] = vec2(float(x) / float(size.x), float(y) / float(size.y));
00065 
00066     //problem remaining is indices. 
00067     if(x != 0 && y != 0){
00068         int offsetIndex = (xminus + yminus * (size.x - 1)) * 6; 
00069             
00070         int vert1 = xminus + yminus * size.x; 
00071         int vert2 = xminus + y * size.x; 
00072         int vert3 = x + y * size.x; 
00073         int vert4 = x + yminus * size.x; 
00074 
00075         indices[offsetIndex + 0] = vert4; 
00076         indices[offsetIndex + 1] = vert1;
00077         indices[offsetIndex + 2] = vert3;
00078 
00079         indices[offsetIndex + 3] = vert1;
00080         indices[offsetIndex + 4] = vert2;
00081         indices[offsetIndex + 5] = vert3;
00082     }
00083 }
 All Classes Files Functions Variables Enumerations