Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. // Phong fragment shader phong-tex.frag matched with phong-tex.vert
  2. #version 330
  3.  
  4. // Some drivers require the following
  5. precision highp float;
  6.  
  7. struct lightStruct
  8. {
  9. vec4 ambient;
  10. vec4 diffuse;
  11. vec4 specular;
  12. };
  13.  
  14. struct materialStruct
  15. {
  16. vec4 ambient;
  17. vec4 diffuse;
  18. vec4 specular;
  19. float shininess;
  20. };
  21.  
  22. uniform float attConst;
  23. uniform float attLinear;
  24. uniform float attQuadratic;
  25. in float ex_D;
  26.  
  27. uniform lightStruct light;
  28. uniform materialStruct material;
  29. uniform sampler2D textureUnit0;
  30.  
  31. in vec3 ex_N;
  32. in vec3 ex_V;
  33. in vec3 ex_L;
  34. in vec2 ex_TexCoord;
  35. layout(location = 0) out vec4 out_Color;
  36.  
  37. void main(void) {
  38.  
  39. // Ambient intensity
  40. vec4 ambientI = light.ambient * material.ambient;
  41.  
  42. // Diffuse intensity
  43. vec4 diffuseI = light.diffuse * material.diffuse;
  44. diffuseI = diffuseI * max(dot(normalize(ex_N),normalize(ex_L)),0);
  45.  
  46. // Specular intensity
  47. // Calculate R - reflection of light
  48. vec3 R = normalize(reflect(normalize(-ex_L),normalize(ex_N)));
  49.  
  50. vec4 specularI = light.specular * material.specular;
  51. specularI = specularI * pow(max(dot(R,ex_V),0), material.shininess);
  52.  
  53. float attenuation = attConst + attLinear * ex_D + attQuadratic * ex_D*ex_D;
  54. attenuation = 1/attenuation;
  55. vec4 tmp_Color = (diffuseI + specularI);//attenuation does not
  56. //affect ambient light
  57. vec4 litColour = ambientI+vec4(tmp_Color.rgb / attenuation, 1.0);
  58.  
  59. // Fragment colour
  60. out_Color = litColour * texture(textureUnit0, ex_TexCoord);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement