Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #version 330
  2.  
  3. in vec4 position_in_viewspace;
  4. in vec3 normal_in_viewspace;
  5. in vec2 tex_coord;
  6.  
  7. out vec4 color;
  8.  
  9. uniform sampler2D diffuse_map;
  10. uniform sampler2D tree_normal;
  11. uniform vec4 diffuse = vec4(0,0,0,1);
  12.  
  13. uniform struct SLight {
  14. vec3 position_in_viewspace;
  15. float radius;
  16. vec3 material_color_diffuse;
  17. vec3 material_color_specular;
  18. vec3 material_color_ambient;
  19. } light[64];
  20. uniform int light_count;
  21.  
  22. float compute_blinn_term(in vec3 N, in vec3 L, in vec3 V, in float NdotL, in float shininess)
  23. {
  24. vec3 H = normalize(L + V);
  25. float term = dot(N,H);
  26. term = max(clamp(term, 0.0, 1.0), 0.0);
  27. term = pow(term, shininess);
  28. return term;
  29. }
  30.  
  31. vec3 color_from_light(in int light_index, in vec3 N, in vec3 V, in vec3 diffuse)
  32. {
  33. vec3 L = (light[light_index].position_in_viewspace - position_in_viewspace.xyz);
  34. vec3 L_rad = L / light[light_index].radius;
  35. float light_attenuation = max(0, 1.0 - dot(L_rad,L_rad));
  36. vec3 light_diffuse = light[light_index].material_color_diffuse;
  37. vec3 light_specular = light[light_index].material_color_specular;
  38. vec3 light_ambient = light[light_index].material_color_ambient;
  39.  
  40. float NdotL = max(dot(N,L), 0.0);
  41. float shininess = 0.75;
  42. float term = compute_blinn_term(N, L, V, NdotL, shininess);
  43.  
  44. //Uncomment to debug light attenuation
  45. //return vec4(1,1,1,1) * light_attenuation;
  46.  
  47. return vec3(
  48. (diffuse * NdotL * light_diffuse * light_attenuation) +
  49. (diffuse * term * light_specular * light_attenuation) +
  50. (diffuse * light_ambient * 0.5)
  51. );
  52. }
  53.  
  54. void main() {
  55.  
  56. vec3 N = normalize(normal_in_viewspace);
  57. //vec3 N = normalize(texture(tree_normal, tex_coord).rgb * 1.0 - vec3(1.0));
  58. vec3 V = normalize(-position_in_viewspace.xyz);
  59.  
  60.  
  61. vec4 texel = diffuse * texture(diffuse_map, tex_coord);
  62. if(texel.a < 0.5){
  63. discard;
  64. }
  65. color = texel;
  66.  
  67. for(int i = 0; i < light_count; i++)
  68. {
  69. color += vec4(color_from_light(i, N, V, diffuse.rgb * vec3(0.05)),1);
  70. }
  71. if(color == vec4(0,0,0,1)) {
  72. color = diffuse;
  73. }
  74.  
  75. const float LOG2 = 1.442695;
  76. float fog_density = 0.0045;
  77. vec4 fog_color = vec4(0.431372,0.458823,0.486274, 0.5);
  78. float z = gl_FragCoord.z / gl_FragCoord.w;
  79. float fog_factor = exp2( -fog_density*fog_density * z*z * LOG2 );
  80. fog_factor = clamp(fog_factor, 0.0, 1.0);
  81.  
  82. color = mix(fog_color, color, fog_factor);
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement