Advertisement
Guest User

OpenGL Fragment Shader

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