Advertisement
ChocoMan

Untitled

Apr 28th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 330
  2.  
  3. const int MAX_POINT_LIGHTS = 2;
  4.  
  5. in vec2 TexCoord0;
  6. in vec3 Normal0;
  7. in vec3 WorldPos0;
  8.  
  9. out vec4 outputColor;
  10.  
  11. struct BaseLight
  12. {
  13.     vec3 Color;
  14.     float AmbientIntensity;
  15.     float DiffuseIntensity;
  16. };
  17.  
  18. struct DirectionalLight
  19. {
  20.     BaseLight Base;
  21.     vec3 Direction;
  22. };
  23.  
  24. struct Attenuation
  25. {
  26.     float Constant;
  27.     float Linear;
  28.     float Exp;
  29. };
  30.  
  31. struct PointLight
  32. {
  33.     BaseLight Base;
  34.     vec3 Position;
  35.     Attenuation Atten;
  36. };
  37.  
  38. uniform int gNumPointLights;
  39. uniform DirectionalLight gDirectionalLight;
  40. uniform PointLight gPointLights[MAX_POINT_LIGHTS];
  41. uniform sampler2D Tex1;
  42.  
  43. uniform vec3 gEyeWorldPos;
  44. uniform float gMatSpecularIntensity;
  45. uniform float gSpecularPower;
  46.  
  47.  
  48. vec4 CalcLightInternal(BaseLight Light, vec3 LightDirection, vec3 Normal)
  49. {
  50.     vec4 AmbientColor = vec4(Light.Color, 1.0f) * Light.AmbientIntensity;
  51.     float DiffuseFactor = dot(Normal, -LightDirection);
  52.  
  53.     vec4 DiffuseColor = vec4(0, 0, 0, 0);
  54.     vec4 SpecularColor = vec4(0, 0, 0, 0);
  55.  
  56.     if (DiffuseFactor > 0)
  57.     {
  58.         DiffuseColor = vec4(Light.Color, 1.0f) * Light.DiffuseIntensity * DiffuseFactor;
  59.  
  60.         vec3 VertexToEye = normalize(gEyeWorldPos - WorldPos0);
  61.         vec3 LightReflect = normalize(reflect(LightDirection, Normal));
  62.         float SpecularFactor = dot(VertexToEye, LightReflect);
  63.         SpecularFactor = pow(SpecularFactor, gSpecularPower);
  64.  
  65.         if (SpecularFactor > 0)
  66.         {
  67.             SpecularColor = vec4(Light.Color, 1.0f) *
  68.                 gMatSpecularIntensity * SpecularFactor;
  69.         }
  70.     }
  71.  
  72.     return (AmbientColor + DiffuseColor + SpecularColor);
  73. }
  74.  
  75.  
  76. vec4 CalcDirectionalLight(vec3 Normal)
  77. {
  78.     return CalcLightInternal(gDirectionalLight.Base, gDirectionalLight.Direction, Normal);
  79. }
  80.  
  81.  
  82. vec4 CalcPointLight(int Index, vec3 Normal)
  83. {
  84.     vec3 LightDirection = WorldPos0 - gPointLights[Index].Position;
  85.     float Distance = length(LightDirection);
  86.     LightDirection = normalize(LightDirection);
  87.    
  88.  
  89.     vec4 Color = CalcLightInternal(gPointLights[Index].Base, LightDirection, Normal);
  90.     float Attenuation = gPointLights[Index].Atten.Constant +
  91.                         gPointLights[Index].Atten.Linear * Distance +
  92.                         gPointLights[Index].Atten.Exp * Distance * Distance;
  93.  
  94.     return Color / Attenuation;
  95. }
  96.  
  97.  
  98. void main()
  99. {
  100.     vec3 Normal = normalize(Normal0);
  101.     vec4 TotalLight = CalcDirectionalLight(Normal);
  102.  
  103.     for (int i = 0; i < gNumPointLights; i++)
  104.     {
  105.         TotalLight += CalcPointLight(i, Normal);
  106.     }
  107.    
  108.     outputColor = texture2D(Tex1, TexCoord0.xy) * TotalLight;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement