Waterflow
Visualize water in terrain
src/shaders/terrainshader.frag
Go to the documentation of this file.
00001 
00002 
00003 
00004 #version 430
00005 
00006 // ===== Uniform Buffers =====
00007 
00013 struct LightParam {
00014     vec3 pos;       
00015     float isDir;    
00016     vec3 color;     
00017     float specExp;  
00018 };
00019 
00020 layout(std140, binding = 0) uniform LightInfo 
00021 {
00022     LightParam lights[2];
00023 };
00024 
00025 
00026 // ===== Uniforms =====
00027 
00028 uniform vec3 camPos;                
00029 uniform vec3 size;                  
00030 uniform sampler2D terr_texUnit;     
00031 uniform sampler2D height_texUnit;   
00032 
00033 
00034 // ===== In/Out params =====
00035 
00036 in vec3 out_Normal;                 
00037 in vec2 out_TexCoord;               
00038 in vec3 out_ObjPos;                 
00039 
00040 out vec4 out_Color;                 
00041 
00042 
00043 // ===== Variables needed =====
00044 
00045 // Light vectors
00046 vec3 r;                             
00047 vec3 s;                             
00048 vec3 eye;                           
00049 
00050 // Lighting (the Phong model).
00051 float kamb;                         
00052 float kdiff;                        
00053 float kspec;                        
00054 
00055 vec3 ambLight;                      
00056 vec3 diffLight;                     
00057 vec3 specLight;                     
00058 vec3 totalLight;                    
00059 
00060 
00061 void main(void)
00062 {
00063     // Incident and reflected light is calculated for the light source.
00064     s = normalize(lights[0].pos - (1 - lights[0].isDir) * out_ObjPos);
00065     r = normalize(2 * out_Normal * dot(s, normalize(out_Normal)) - s);
00066 
00067     // eye vector is calculated.
00068     eye = normalize(camPos - out_ObjPos);
00069 
00070     // Light according to the Phong model.
00071     kamb = 0.1;
00072     kdiff = 0.5;
00073     kspec = 0.5;
00074     ambLight = kamb * vec3(1.0, 1.0, 1.0);
00075     diffLight = vec3(0.0, 0.0, 0.0);
00076     specLight = vec3(0.0, 0.0, 0.0);
00077     // Diffuse light.
00078     diffLight += kdiff * lights[0].color * max(0.0, dot(s, normalize(out_Normal)));
00079     // Specular light.
00080     specLight += kspec * lights[0].color * max(0.0, pow(dot(r, eye), lights[0].specExp));
00081 
00082     totalLight = vec3(0.0, 0.0, 0.0);
00083     // The different light components are added to the total light.
00084     totalLight += ambLight;
00085     totalLight += diffLight;
00086     totalLight += specLight;
00087 
00088     vec3 terrainData = vec3(texture(terr_texUnit, out_TexCoord)) * totalLight;
00089     out_Color = vec4(terrainData, 1.0f);
00090 }
 All Classes Files Functions Variables Enumerations