Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #version 330
  2.  
  3. // TODO: get color value from vertex shader
  4. in vec3 world_position;
  5. in vec3 world_normal;
  6.  
  7. // Uniforms for light properties
  8. uniform vec3 light_direction;
  9. uniform vec3 light_position;
  10. uniform vec3 eye_position;
  11.  
  12. uniform float material_kd;
  13. uniform float material_ks;
  14. uniform int material_shininess;
  15.  
  16. uniform vec3 object_color;
  17.  
  18. uniform int spotlight;
  19. uniform float cut_off;
  20.  
  21. layout(location = 0) out vec4 out_color;
  22.  
  23. void main()
  24. {
  25. vec3 V = normalize(eye_position - world_position);
  26. vec3 L = normalize(light_position - world_position);
  27. vec3 H = normalize(L + V);
  28.  
  29. // TODO: define ambient light component
  30. float ambient_light = 0.25;
  31.  
  32. // TODO: compute diffuse light component
  33. float diffuse_light = material_kd*max(0,(dot(world_normal, L)));
  34.  
  35. // TODO: compute specular light component
  36. float specular_light = 0;
  37. if(diffuse_light > 0)
  38. specular_light = material_ks * pow(max(dot(world_normal, H), 0), material_shininess);
  39.  
  40. // TODO: compute light
  41. float light_att_factor;
  42. if(spotlight == 1) {
  43. float cut_off = radians(30);
  44. float spot_light = dot(L, light_direction);
  45. float spot_light_limit = cos(radians(cut_off));
  46.  
  47. if (spot_light > spot_light_limit)
  48. {
  49. float linear_att = (spot_light - spot_light_limit) / (1 - spot_light_limit);
  50. light_att_factor = pow(linear_att, 2);
  51.  
  52. diffuse_light *= light_att_factor;
  53. specular_light *= light_att_factor;
  54. }
  55. }
  56. else {
  57. light_att_factor = 1 / pow(distance(light_position, world_position), 2);
  58. }
  59. vec3 color = object_color * (ambient_light + light_att_factor * (diffuse_light + specular_light));
  60.  
  61. // TODO: write pixel out color
  62. out_color = vec4(color, 1);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement