Waterflow
Visualize water in terrain
|
00001 00002 00003 00004 #version 430 00005 00006 layout(local_size_x = 16, local_size_y = 16) in; 00007 00008 layout (std430, binding = 0) readonly buffer height0 00009 { 00010 float terrHeight[]; 00011 }; 00012 00013 layout (std430, binding = 1) writeonly buffer inNormals 00014 { 00015 float normals[]; 00016 }; 00017 00018 uniform ivec2 size; 00019 00020 void main(void) { 00021 00022 ivec2 storePos = ivec2(gl_GlobalInvocationID.xy); 00023 00024 int x = clamp(storePos.x, 0, size.x-1); 00025 int y = clamp(storePos.y, 0, size.y-1); 00026 int xplus = clamp(x + 1, 0, size.x-1); 00027 int xminus = clamp(x - 1, 0, size.x-1); 00028 int yplus = clamp(y + 1, 0, size.y-1); 00029 int yminus = clamp(y - 1, 0, size.y-1); 00030 00031 float sobelX = 0; 00032 float sobelZ = 0; 00033 00034 // Sobel x 00035 sobelX += 2.0 * terrHeight[xplus + y*size.x]; // texture(texUnit, outTexCoord + vec2(+offsetX, 0)).y; 00036 sobelX += 1.0 * terrHeight[xplus + yplus*size.x]; 00037 sobelX += 1.0 * terrHeight[xplus + yminus*size.x]; 00038 sobelX -= 2.0 * terrHeight[xminus + y*size.x]; 00039 sobelX -= 1.0 * terrHeight[xminus + yplus*size.x]; 00040 sobelX -= 1.0 * terrHeight[xminus + yminus*size.x]; 00041 // Sobel z 00042 sobelZ += 2.0 * terrHeight[x + yplus*size.x]; 00043 sobelZ += 1.0 * terrHeight[xplus + yplus*size.x]; 00044 sobelZ += 1.0 * terrHeight[xminus + yplus*size.x]; 00045 sobelZ -= 2.0 * terrHeight[x + yminus*size.x]; 00046 sobelZ -= 1.0 * terrHeight[xplus + yminus*size.x]; 00047 sobelZ -= 1.0 * terrHeight[xminus + yminus*size.x]; 00048 00049 // Height scaling (0.005f is an arbitrary value) 00050 float heightScale = 5.0f; 00051 00052 // Combine to normal 00053 vec3 normal = normalize(vec3(-sobelX, heightScale, -sobelZ)); 00054 00055 int offset = (x + y*size.x)*3; 00056 00057 normals[offset +0] = normal.x; 00058 normals[offset +1] = normal.y; 00059 normals[offset +2] = normal.z; 00060 }