Advertisement
Guest User

fShader.glsl

a guest
May 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 150
  2.  
  3. in vec3 fL1, fL2;
  4. in vec3 fE;
  5. in vec3 fN;
  6. in vec2 texCoord;  // The third coordinate is always 0.0 and is discarded
  7.  
  8. out vec4 fColor;
  9.  
  10. uniform vec3 LightColor1, LightColor2;
  11. uniform float LightBrightness1, LightBrightness2;
  12. uniform vec3 AmbientProduct, DiffuseProduct, SpecularProduct;
  13. uniform float Shininess;
  14. uniform sampler2D texture;
  15. uniform float texScale;
  16.  
  17. void main() {
  18.     // Unit direction vectors for Blinn-Phong shading calculation
  19.     vec3 L1 = normalize(fL1);  // Direction to the light source
  20.     vec3 L2 = normalize(fL2);
  21.     vec3 E = normalize(fE);  // Direction to the eye/camera
  22.     vec3 H1 = normalize(L1 + E);  // Halfway vector
  23.     vec3 H2 = normalize(L2 + E);
  24.     vec3 N = normalize(fN);  // Normal vector
  25.  
  26.     // [G] Compute terms in the illumination equation
  27.     vec3 ambient1 = (LightColor1 * LightBrightness1) * AmbientProduct;
  28.     vec3 ambient2 = (LightColor2 * LightBrightness2) * AmbientProduct;
  29.  
  30.     float Kd1 = max(dot(L1, N), 0.0);
  31.     float Kd2 = max(dot(L2, N), 0.0);
  32.     vec3 diffuse1 = Kd1 * (LightColor1 * LightBrightness1) * DiffuseProduct;
  33.     vec3 diffuse2 = Kd2 * (LightColor2 * LightBrightness2) * DiffuseProduct;
  34.  
  35.     // [H] Only use brightness for specular highlights
  36.     float Ks1 = pow(max(dot(N, H1), 0.0), Shininess);
  37.     float Ks2 = pow(max(dot(N, H2), 0.0), Shininess);
  38.     vec3 specular1 = Ks1 * LightBrightness1 * SpecularProduct;
  39.     vec3 specular2 = Ks2 * LightBrightness2 * SpecularProduct;
  40.  
  41.     if (dot(L1, N) < 0.0) {
  42.         specular1 = vec3(0.0, 0.0, 0.0);
  43.     }
  44.     if (dot(L2, N) < 0.0) {
  45.         specular2 = vec3(0.0, 0.0, 0.0);
  46.     }
  47.  
  48.     // globalAmbient is independent of distance from the light source
  49.     vec3 globalAmbient = vec3(0.1, 0.1, 0.1);
  50.  
  51.     // [F] Reduce point light with distance
  52.     float len = 0.01 + length(fL1);
  53.     vec4 color = vec4(globalAmbient + ((ambient1 + diffuse1) / len) + ambient2 + diffuse2, 1.0);
  54.     fColor = color * texture2D(texture, texCoord * texScale) + vec4((specular1 / len) + specular2, 1.0);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement