light.frag 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Version du GLSL
  2. #version 150 core
  3. // Entrées
  4. in vec3 vertex;
  5. in vec3 normale;
  6. in vec2 coordTexture;
  7. // Uniform
  8. uniform sampler2D texture;
  9. uniform vec3 eyePos;
  10. uniform vec3 posLight[10];
  11. uniform int overLight; // Dépassement de la couleur originale du pixel
  12. uniform int nbLight; // Nombre de lumičres (maxi 10)
  13. // Sortie
  14. out vec4 out_Color;
  15. // Fonction main
  16. void main()
  17. {
  18. out_Color = vec4(0.0, 0.0, 0.0, 1.0);
  19. vec4 pxColor = texture2D(texture, coordTexture);
  20. float ambientColor = 0.2;
  21. float loin;
  22. float cosTheta;
  23. vec4 colorLight;
  24. if ( overLight ) colorLight = vec4(1.0, 0.5, 0.0, 1.0);
  25. else colorLight = vec4(1.0, 1.0, 1.0, 1.0);
  26. colorLight *= pxColor;
  27. int i;
  28. for ( i=0; i<nbLight; i++)
  29. {
  30. /// Cosinus
  31. cosTheta = clamp( dot( normale, posLight[i]-vertex ), 0.0, 1.0 );
  32. //float cosAlpha = clamp( dot( reflect( normalize(vertex-posLight[i]), normale), normalize(eyePos-vertex) ), 0.0, 1.0 );
  33. //float viewDistance = 5.0f;
  34. /// Distance
  35. loin = distance( vertex, posLight[i] );
  36. //loin = (0.5-(0.5/(viewDistance*viewDistance*viewDistance))*loin*loin*loin);
  37. /// Couleurs
  38. /// Calcule si nécessaire
  39. if ( loin < 30 )
  40. {
  41. /// Calcul lumičre diffuse
  42. float power = cosTheta * 100 / (loin*loin);
  43. if ( overLight == 0 ) power = clamp( power, 0.0, 1.0 );
  44. out_Color += colorLight * power * 0.4;
  45. /// Calcul lumičre spéculaire
  46. /*power = pow(cosAlpha,5) * 1000 / (loin*loin);
  47. if ( overLight == 0 ) power = clamp( power, 0.0, 1.0 );
  48. out_Color += colorLight * power * 0.4;*/ // Lumičre spéculaire
  49. }
  50. }
  51. /// Lumičre ambiente
  52. out_Color += pxColor * ambientColor;
  53. /// Correction ransparence
  54. out_Color.w = pxColor.w;
  55. }