2016-02-17 22:29:26 +00:00
|
|
|
#define MAX_LIGHTS 8
|
|
|
|
|
2020-12-19 17:17:42 +00:00
|
|
|
void perLight(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal)
|
2016-02-17 22:29:26 +00:00
|
|
|
{
|
2020-12-19 17:17:42 +00:00
|
|
|
vec3 lightDir = gl_LightSource[lightIndex].position.xyz - viewPos * gl_LightSource[lightIndex].position.w;
|
|
|
|
float lightDistance = length(lightDir);
|
2017-09-20 23:25:48 +00:00
|
|
|
lightDir = normalize(lightDir);
|
2018-06-22 00:02:01 +00:00
|
|
|
float illumination = clamp(1.0 / (gl_LightSource[lightIndex].constantAttenuation + gl_LightSource[lightIndex].linearAttenuation * lightDistance + gl_LightSource[lightIndex].quadraticAttenuation * lightDistance * lightDistance), 0.0, 1.0);
|
2017-09-20 23:25:48 +00:00
|
|
|
|
2020-12-19 17:17:42 +00:00
|
|
|
ambientOut = gl_LightSource[lightIndex].ambient.xyz * illumination;
|
|
|
|
diffuseOut = gl_LightSource[lightIndex].diffuse.xyz * max(dot(viewNormal, lightDir), 0.0) * illumination;
|
2017-09-20 23:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 23:44:35 +00:00
|
|
|
#if PER_PIXEL_LIGHTING
|
2020-12-19 17:17:42 +00:00
|
|
|
void doLighting(vec3 viewPos, vec3 viewNormal, float shadowing, out vec3 diffuseLight, out vec3 ambientLight)
|
2017-09-20 23:25:48 +00:00
|
|
|
#else
|
2020-12-19 17:17:42 +00:00
|
|
|
void doLighting(vec3 viewPos, vec3 viewNormal, out vec3 diffuseLight, out vec3 ambientLight, out vec3 shadowDiffuse)
|
2017-09-20 23:25:48 +00:00
|
|
|
#endif
|
|
|
|
{
|
2020-12-19 17:17:42 +00:00
|
|
|
vec3 ambientOut, diffuseOut;
|
|
|
|
// This light gets added a second time in the loop to fix Mesa users' slowdown, so we need to negate its contribution here.
|
|
|
|
perLight(ambientOut, diffuseOut, 0, viewPos, viewNormal);
|
2017-10-08 00:34:54 +00:00
|
|
|
#if PER_PIXEL_LIGHTING
|
2020-12-19 17:17:42 +00:00
|
|
|
diffuseLight = diffuseOut * shadowing - diffuseOut;
|
2017-09-20 23:25:48 +00:00
|
|
|
#else
|
2020-12-19 17:17:42 +00:00
|
|
|
shadowDiffuse = diffuseOut;
|
|
|
|
diffuseLight = -diffuseOut;
|
2017-09-20 23:25:48 +00:00
|
|
|
#endif
|
2020-12-19 17:17:42 +00:00
|
|
|
ambientLight = gl_LightModel.ambient.xyz;
|
2018-08-14 20:35:23 +00:00
|
|
|
for (int i=0; i<MAX_LIGHTS; ++i)
|
2016-02-17 22:29:26 +00:00
|
|
|
{
|
2020-12-19 17:17:42 +00:00
|
|
|
perLight(ambientOut, diffuseOut, i, viewPos, viewNormal);
|
|
|
|
ambientLight += ambientOut;
|
|
|
|
diffuseLight += diffuseOut;
|
2016-02-17 22:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-20 18:02:11 +00:00
|
|
|
|
|
|
|
|
2016-02-21 00:28:42 +00:00
|
|
|
vec3 getSpecular(vec3 viewNormal, vec3 viewDirection, float shininess, vec3 matSpec)
|
2016-02-20 18:02:11 +00:00
|
|
|
{
|
|
|
|
vec3 lightDir = normalize(gl_LightSource[0].position.xyz);
|
2019-03-18 14:07:27 +00:00
|
|
|
float NdotL = dot(viewNormal, lightDir);
|
|
|
|
if (NdotL <= 0.0)
|
2020-12-19 17:17:42 +00:00
|
|
|
return vec3(0.0);
|
2016-02-21 00:28:42 +00:00
|
|
|
vec3 halfVec = normalize(lightDir - viewDirection);
|
2019-03-18 14:07:27 +00:00
|
|
|
float NdotH = dot(viewNormal, halfVec);
|
|
|
|
return pow(max(NdotH, 0.0), max(1e-4, shininess)) * gl_LightSource[0].specular.xyz * matSpec;
|
2016-02-20 18:02:11 +00:00
|
|
|
}
|