daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330
  2.  
  3. in vec2 outTexCoord;
  4. out vec4 fragColor;
  5.  
  6. uniform vec3 colour;
  7. uniform int useColour;
  8.  
  9. in vec3 mvVertexNormal;
  10. in vec3 mvVertexPos;
  11.  
  12. struct Attenuation
  13. {
  14.     float constant;
  15.     float linear;
  16.     float exponent;
  17. };
  18.  
  19. struct PointLight
  20. {
  21.     vec3 colour;
  22.     // Light position is assumed to be in view coordinates
  23.     vec3 position;
  24.     float intensity;
  25.     Attenuation att;
  26. };
  27.  
  28. uniform sampler2D texture_sampler;
  29. uniform vec3 ambientLight;
  30. uniform float specularPower;
  31. uniform PointLight pointLight;
  32.  
  33.  
  34. vec4 calcPointLight(PointLight light, vec3 position, vec3 normal)
  35. {
  36.     vec4 diffuseColour = vec4(0, 0, 0, 0);
  37.     vec4 specColour = vec4(0, 0, 0, 0);
  38.  
  39.     // Diffuse Light
  40.     vec3 light_direction = light.position - position;
  41.     vec3 to_light_source  = normalize(light_direction);
  42.     float diffuseFactor = max(dot(normal, to_light_source ), 0.0);
  43.     diffuseColour = vec4(light.colour, 1.0) * light.intensity * diffuseFactor;
  44.  
  45.     // Specular Light
  46.     vec3 camera_direction = normalize(-position);
  47.     vec3 from_light_source = -to_light_source;
  48.     vec3 reflected_light = normalize(reflect(from_light_source, normal));
  49.     float specularFactor = max( dot(camera_direction, reflected_light), 0.0);
  50.     specularFactor = pow(specularFactor, specularPower);
  51.     specColour = specularFactor * vec4(light.colour, 1.0);
  52.  
  53.     // Attenuation
  54.     float distance = length(light_direction);
  55.     float attenuationInv = light.att.constant + light.att.linear * distance +
  56.         light.att.exponent * distance * distance;
  57.     return (diffuseColour + specColour) / attenuationInv;
  58. }
  59.  
  60. void main()
  61. {
  62.     if ( useColour == 1 )
  63.     {
  64.         fragColor = vec4(colour, 1);
  65.     }
  66.     else
  67.     {
  68.         fragColor = texture(texture_sampler, outTexCoord);
  69.     }
  70.  
  71.         vec4 diffuseSpecularComp = calcPointLight(pointLight, mvVertexPos, mvVertexNormal);
  72.    
  73.     fragColor = vec4(ambientLight, 1) + diffuseSpecularComp;
  74. }
RAW Paste Data
Top