Guest User

Shader

a guest
Nov 12th, 2013
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #version 130
  3. #define lights_no 8
  4.  
  5.  
  6. in vec3 fNormal;
  7. in vec2 fTexCoord;
  8. in vec3 fCoord;
  9. out vec4 color;
  10.  
  11. struct Light {
  12.     vec3 ambient;
  13.     vec3 diffuse;
  14.     vec3 specular;
  15.     vec4 position;
  16.     vec3 direction;
  17.     float cutoff;
  18.     float exponent;
  19. };
  20.  
  21. struct Material {  
  22.   vec3  ambient;        
  23.   vec3  diffuse;        
  24.   vec3  specular;        
  25.   float shininess;
  26. };
  27.  
  28. uniform Material material;
  29. uniform Light lights [lights_no];
  30. uniform vec3 camera_position;
  31. uniform bool      useTexture;  
  32. uniform sampler2D texSampler;  
  33.  
  34.  
  35. vec3 directionLight(Light light){
  36.     vec3 L = normalize(light.position.xyz);
  37.     vec3 s = normalize(L+normalize((camera_position-fCoord)));
  38.     float diffuse = max(0., dot(L, normalize(fNormal)));
  39.     float specular = max(0., pow(dot(s, normalize(fNormal)), material.shininess));
  40.  
  41.     vec3 ret = vec3(0.0f);
  42.     ret += material.ambient * light.ambient;
  43.     ret += material.diffuse * light.diffuse * diffuse;
  44.     ret += material.specular * light.specular * specular;
  45.  
  46.     return ret;
  47. }
  48.  
  49. vec3 pointLight(Light light){
  50.     vec3 L = normalize(light.position.xyz-fCoord);
  51.     vec3 s = normalize(L+normalize((camera_position-fCoord)));
  52.     float diffuse = max(0., dot(L, normalize(fNormal)));
  53.     float specular = max(0., pow(dot(s, normalize(fNormal)), material.shininess));
  54.  
  55.     vec3 ret = vec3(0.0f);
  56.     ret += material.ambient * light.ambient;
  57.     ret += material.diffuse * light.diffuse * diffuse;
  58.     ret += material.specular * light.specular * specular;
  59.  
  60.     return ret;
  61. }
  62.  
  63. vec3 reflectionLight(Light light){
  64.     vec3 L = normalize(light.position.xyz-fCoord);
  65.     vec3 s = normalize(L+normalize((camera_position-fCoord)));
  66.     float diffuse = max(0., dot(L, normalize(fNormal)));
  67.     float specular = max(0., pow(dot(s, normalize(fNormal)), material.shininess));
  68.  
  69.     vec3 ret = vec3(0.0f);
  70.     ret += material.ambient * light.ambient;
  71.     ret += material.diffuse * light.diffuse * diffuse;
  72.     ret += material.specular * light.specular * specular;
  73.  
  74.     float center = 0.;
  75.     vec3 v = -L;
  76.     float cos_a = dot(v, light.direction);
  77.     if (cos_a >= light.cutoff)
  78.         center = pow(max(0., cos_a), light.exponent);
  79.  
  80.     return ret*center;
  81.  
  82. }
  83.  
  84. vec3 computeLight(int light_index){
  85.     Light light = lights[light_index];
  86.     if(light.position == vec4(0.0)) { return vec3(0.0);}
  87.     if(light.position.w == 0.0 ) {return directionLight(light);}
  88.     if(light.cutoff == 0) {return pointLight(light);}
  89.     return reflectionLight(light);
  90. }
  91.  
  92.  
  93. void main() {
  94.     // adds color
  95.     color = vec4(1.);
  96.     if(useTexture) {
  97.         color = texture(texSampler, fTexCoord);
  98.     }
  99.    
  100.     vec3 lighting = vec3(0.);
  101.     for(int i = 0; i < lights_no; i++) {
  102.         lighting += computeLight(i);
  103.     }
  104.     color = vec4(lighting,1.) * color;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment