Advertisement
Guest User

Untitled

a guest
Jan 21st, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #version 330 core
  2.  
  3. struct Attenuation
  4. {
  5. int constant;
  6. int linear;
  7. int exponential;
  8. };
  9.  
  10. struct BaseLight
  11. {
  12. vec4 colour;
  13. float ambientIntensity;
  14. float diffuseIntensity;
  15. };
  16.  
  17. struct DirectionalLight
  18. {
  19. BaseLight base;
  20. vec3 direction;
  21. };
  22.  
  23. struct PointLight
  24. {
  25. BaseLight base;
  26. vec3 position;
  27. Attenuation attenuation;
  28. };
  29.  
  30. vec3 CalcLightInternal(BaseLight _light, vec3 _lightDirection, vec3 _normal);
  31. vec3 CalcDirectionalLight(vec3 _normal);
  32. vec3 CalcPointLight(int _index, vec3 _normal);
  33.  
  34. const int MAX_POINT_LIGHTS = 5;
  35.  
  36. in vec2 uv0;
  37. in vec3 normal0;
  38. in vec3 worldPos0;
  39.  
  40. out vec3 finalColor;
  41.  
  42. uniform DirectionalLight directionalLight;
  43. uniform PointLight pointLights[MAX_POINT_LIGHTS];
  44.  
  45. uniform int numPointLights;
  46.  
  47. uniform int lightingEnabled;
  48.  
  49. uniform vec3 cameraEyeWorldPosition;
  50.  
  51. uniform float specularIntensity;
  52. uniform float specularPower;
  53.  
  54. uniform sampler2D diffusetexture;
  55.  
  56. void main()
  57. {
  58. vec3 MaterialDiffuseColor = texture2D(diffusetexture, uv0).rgb;
  59.  
  60. vec3 Normal = normalize(normal0);
  61.  
  62. if(lightingEnabled == 1)
  63. {
  64. vec3 TotalLight = CalcDirectionalLight(Normal);
  65.  
  66. for (int i = 0; i < numPointLights; i++)
  67. {
  68. TotalLight += CalcPointLight(i, Normal);
  69. }
  70.  
  71. finalColor = MaterialDiffuseColor * TotalLight;
  72. }
  73. else
  74. {
  75. finalColor = MaterialDiffuseColor;
  76. }
  77. }
  78.  
  79. vec3 CalcLightInternal(BaseLight _light, vec3 _lightDirection, vec3 _normal)
  80. {
  81. vec3 AmbientColor = _light.colour.rgb * _light.ambientIntensity;
  82.  
  83. float DiffuseFactor = dot(normalize(_normal), _lightDirection);
  84.  
  85. vec3 DiffuseColor = vec3(0, 0, 0);
  86. vec3 SpecularColor = vec3(0, 0, 0);
  87.  
  88. if (DiffuseFactor > 0)
  89. {
  90. DiffuseColor = _light.colour.rgb * _light.diffuseIntensity * DiffuseFactor;
  91.  
  92. vec3 VertexToEye = normalize(cameraEyeWorldPosition - worldPos0);
  93. vec3 LightReflect = normalize(reflect(_lightDirection, _normal));
  94. float SpecularFactor = dot(VertexToEye, LightReflect);
  95. SpecularFactor = pow(SpecularFactor, specularPower);
  96.  
  97. if (SpecularFactor > 0)
  98. {
  99. SpecularColor = _light.colour.rgb * specularIntensity * SpecularFactor;
  100. }
  101. }
  102.  
  103. return (AmbientColor + DiffuseColor + SpecularColor);
  104. }
  105.  
  106. vec3 CalcDirectionalLight(vec3 _normal)
  107. {
  108. return CalcLightInternal(directionalLight.base, directionalLight.direction, _normal);
  109. }
  110.  
  111. vec3 CalcPointLight(int _index, vec3 _normal)
  112. {
  113. vec3 LightDirection = worldPos0 - pointLights[_index].position;
  114. float Distance = length(LightDirection);
  115. LightDirection = normalize(LightDirection);
  116.  
  117. vec3 Color = CalcLightInternal(pointLights[_index].base, LightDirection, _normal);
  118. float Attenuation = pointLights[_index].attenuation.constant +
  119. pointLights[_index].attenuation.linear * Distance +
  120. pointLights[_index].attenuation.exponential * Distance * Distance;
  121.  
  122. return Color / Attenuation;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement