1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #version 140
#define NUMBER_OF_POINT_LIGHTS 4
#define NUMBER_OF_SPOT_LIGHTS 4
struct SMaterial
{
sampler2D Diffuse;
sampler2D Specular;
float Shininess;
};
struct SDirectionalLight
{
vec3 Direction;
vec3 Ambient;
vec3 Diffuse;
vec3 Specular;
};
struct SPointLight
{
vec3 Position;
vec3 Ambient;
vec3 Diffuse;
vec3 Specular;
float Constant;
float Linear;
float Quadratic;
};
struct SSpotLight
{
vec3 Position;
vec3 Direction;
vec3 Ambient;
vec3 Diffuse;
vec3 Specular;
float CutOff;
float OuterCutOff;
float Constant;
float Linear;
float Quadratic;
};
in vec2 STMap;
in vec3 normal0;
in vec3 FragPos;
uniform vec3 ViewPos;
uniform SDirectionalLight DirectionalLight;
uniform SPointLight PointLights[NUMBER_OF_POINT_LIGHTS];
uniform SSpotLight SpotLights[NUMBER_OF_SPOT_LIGHTS];
uniform SMaterial Material;
vec3 CalcDirectionalLight(SDirectionalLight Light, vec3 Normal, vec3 ViewDir);
vec3 CalcPointLight(SPointLight Light, vec3 Normal, vec3 FragPos, vec3 ViewDir);
vec3 CalcSpotLight(SSpotLight Light, vec3 Normal, vec3 FragPos, vec3 ViewDir);
void main()
{
vec3 norm = normalize(normal0);
vec3 ViewDirection = normalize(ViewPos - FragPos);
vec3 Result = CalcDirectionalLight(DirectionalLight, norm, ViewDirection);
for (int i = 0; i < NUMBER_OF_POINT_LIGHTS; i++)
{
Result += CalcPointLight(PointLights[i], norm, FragPos, ViewDirection);
}
for (int i = 0; i < NUMBER_OF_SPOT_LIGHTS; i++)
{
Result += CalcSpotLight(SpotLights[i], norm, FragPos, ViewDirection);
}
gl_FragColor = vec4(Result, 1.0);
}
// Calculates the color when using a directional light.
vec3 CalcDirectionalLight(SDirectionalLight Light, vec3 Normal, vec3 ViewDir)
{
vec3 LightDir = normalize(-Light.Direction);
// Diffuse shading
float Diff = max(dot(Normal, LightDir), 0.0);
// Specular shading
vec3 ReflectDir = reflect( -LightDir, Normal);
float Spec = pow( max( dot( ViewDir, ReflectDir ), 0.0 ), Material.Shininess);
// Combine results
vec3 Ambient = Light.Ambient * vec3( texture(Material.Diffuse, STMap));
vec3 Diffuse = Light.Diffuse * Diff * vec3( texture(Material.Diffuse, STMap));
vec3 Specular = Light.Specular * Spec * vec3( texture(Material.Specular, STMap));
return (Ambient + Diffuse + Specular);
}
// Calculates the color when using a point light.
vec3 CalcPointLight(SPointLight Light, vec3 Normal, vec3 FragPosition, vec3 ViewDir)
{
vec3 LightDir = normalize(Light.Position - FragPosition);
// Diffuse shading
float Diff = max(dot(Normal, LightDir), 0.0);
// Specular shading
vec3 ReflectDir = reflect( -LightDir, Normal);
float Spec = pow( max( dot( ViewDir, ReflectDir ), 0.0 ), Material.Shininess);
// Attenuation
float Distance = length(Light.Position - FragPosition);
float Attenuation = 1.0f / (Light.Constant + Light.Linear * Distance + Light.Quadratic * Distance * Distance);
// Combine results
vec3 Ambient = Light.Ambient * vec3( texture(Material.Diffuse, STMap));
vec3 Diffuse = Light.Diffuse * Diff * vec3( texture(Material.Diffuse, STMap));
vec3 Specular = Light.Specular * Spec * vec3( texture(Material.Specular, STMap));
Ambient *= Attenuation;
Diffuse *= Attenuation;
Specular *= Attenuation;
return (Ambient + Diffuse + Specular);
}
// Calculates the color when using a spot light.
vec3 CalcSpotLight(SSpotLight Light, vec3 Normal, vec3 FragPosition, vec3 ViewDir )
{
vec3 LightDir = normalize(Light.Position - FragPosition );
// Diffuse shading
float Diff = max( dot(Normal, LightDir ), 0.0 );
// Specular shading
vec3 ReflectDir = reflect(-LightDir, Normal );
float Spec = pow( max( dot( ViewDir, ReflectDir ), 0.0 ), Material.Shininess );
// Attenuation
float Distance = length(Light.Position - FragPosition );
float Attenuation = 1.0f / (Light.Constant + Light.Linear * Distance + Light.Quadratic * Distance * Distance);
// Spotlight intensity
float Theta = dot(LightDir, normalize(-Light.Direction));
float Epsilon = Light.CutOff - Light.OuterCutOff;
float Intensity = clamp((Theta - Light.OuterCutOff ) / Epsilon, 0.0, 1.0);
// Combine results
vec3 Ambient = Light.Ambient * vec3( texture(Material.Diffuse, STMap));
vec3 Diffuse = Light.Diffuse * Diff * vec3( texture(Material.Diffuse, STMap));
vec3 Specular = Light.Specular * Spec * vec3( texture(Material.Specular, STMap));
Ambient *= Attenuation * Intensity;
Diffuse *= Attenuation * Intensity;
Specular *= Attenuation * Intensity;
return (Ambient + Diffuse + Specular);
} |
Partager