Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330
- const int MAX_POINT_LIGHTS = 2;
- in vec2 TexCoord0;
- in vec3 Normal0;
- in vec3 WorldPos0;
- out vec4 outputColor;
- struct BaseLight
- {
- vec3 Color;
- float AmbientIntensity;
- float DiffuseIntensity;
- };
- struct DirectionalLight
- {
- BaseLight Base;
- vec3 Direction;
- };
- struct Attenuation
- {
- float Constant;
- float Linear;
- float Exp;
- };
- struct PointLight
- {
- BaseLight Base;
- vec3 Position;
- Attenuation Atten;
- };
- uniform int gNumPointLights;
- uniform DirectionalLight gDirectionalLight;
- uniform PointLight gPointLights[MAX_POINT_LIGHTS];
- uniform sampler2D Tex1;
- uniform vec3 gEyeWorldPos;
- uniform float gMatSpecularIntensity;
- uniform float gSpecularPower;
- vec4 CalcLightInternal(BaseLight Light, vec3 LightDirection, vec3 Normal)
- {
- vec4 AmbientColor = vec4(Light.Color, 1.0f) * Light.AmbientIntensity;
- float DiffuseFactor = dot(Normal, -LightDirection);
- vec4 DiffuseColor = vec4(0, 0, 0, 0);
- vec4 SpecularColor = vec4(0, 0, 0, 0);
- if (DiffuseFactor > 0)
- {
- DiffuseColor = vec4(Light.Color, 1.0f) * Light.DiffuseIntensity * DiffuseFactor;
- vec3 VertexToEye = normalize(gEyeWorldPos - WorldPos0);
- vec3 LightReflect = normalize(reflect(LightDirection, Normal));
- float SpecularFactor = dot(VertexToEye, LightReflect);
- SpecularFactor = pow(SpecularFactor, gSpecularPower);
- if (SpecularFactor > 0)
- {
- SpecularColor = vec4(Light.Color, 1.0f) *
- gMatSpecularIntensity * SpecularFactor;
- }
- }
- return (AmbientColor + DiffuseColor + SpecularColor);
- }
- vec4 CalcDirectionalLight(vec3 Normal)
- {
- return CalcLightInternal(gDirectionalLight.Base, gDirectionalLight.Direction, Normal);
- }
- vec4 CalcPointLight(int Index, vec3 Normal)
- {
- vec3 LightDirection = WorldPos0 - gPointLights[Index].Position;
- float Distance = length(LightDirection);
- LightDirection = normalize(LightDirection);
- vec4 Color = CalcLightInternal(gPointLights[Index].Base, LightDirection, Normal);
- float Attenuation = gPointLights[Index].Atten.Constant +
- gPointLights[Index].Atten.Linear * Distance +
- gPointLights[Index].Atten.Exp * Distance * Distance;
- return Color / Attenuation;
- }
- void main()
- {
- vec3 Normal = normalize(Normal0);
- vec4 TotalLight = CalcDirectionalLight(Normal);
- for (int i = 0; i < gNumPointLights; i++)
- {
- TotalLight += CalcPointLight(i, Normal);
- }
- outputColor = texture2D(Tex1, TexCoord0.xy) * TotalLight;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement