2016-02-17 22:29:26 +00:00
#define MAX_LIGHTS 8
2018-10-15 22:12:15 +00:00
uniform int colorMode;
2017-09-21 00:59:02 +00:00
void perLight(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal, vec4 diffuse, vec3 ambient)
2016-02-17 22:29:26 +00:00
{
vec3 lightDir;
2018-06-22 00:02:01 +00:00
float lightDistance;
2016-02-17 22:29:26 +00:00
2017-09-21 00:59:02 +00:00
lightDir = gl_LightSource[lightIndex].position.xyz - (viewPos.xyz * gl_LightSource[lightIndex].position.w);
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
2017-09-21 01:03:53 +00:00
ambientOut = ambient * gl_LightSource[lightIndex].ambient.xyz * illumination;
diffuseOut = diffuse.xyz * gl_LightSource[lightIndex].diffuse.xyz * max(dot(viewNormal.xyz, lightDir), 0.0) * illumination;
2017-09-20 23:25:48 +00:00
}
2017-10-07 23:44:35 +00:00
#if PER_PIXEL_LIGHTING
2017-09-20 23:25:48 +00:00
vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor, float shadowing)
#else
2017-09-21 00:59:02 +00:00
vec4 doLighting(vec3 viewPos, vec3 viewNormal, vec4 vertexColor, out vec3 shadowDiffuse)
2017-09-20 23:25:48 +00:00
#endif
{
2018-10-15 22:12:15 +00:00
vec4 diffuse;
vec3 ambient;
if (colorMode == 3)
{
diffuse = gl_FrontMaterial.diffuse;
2018-10-22 16:35:01 +00:00
ambient = vertexColor.xyz;
2018-10-15 22:12:15 +00:00
}
else if (colorMode == 2)
{
diffuse = vertexColor;
ambient = vertexColor.xyz;
}
else
{
diffuse = gl_FrontMaterial.diffuse;
ambient = gl_FrontMaterial.ambient.xyz;
}
2016-02-17 22:29:26 +00:00
vec4 lightResult = vec4(0.0, 0.0, 0.0, diffuse.a);
2017-09-21 00:59:02 +00:00
vec3 diffuseLight, ambientLight;
perLight(ambientLight, diffuseLight, 0, viewPos, viewNormal, diffuse, ambient);
2017-10-08 00:34:54 +00:00
#if PER_PIXEL_LIGHTING
2018-08-14 20:35:23 +00:00
lightResult.xyz += diffuseLight * shadowing - diffuseLight; // This light gets added a second time in the loop to fix Mesa users' slowdown, so we need to negate its contribution here.
2017-09-20 23:25:48 +00:00
#else
2017-09-21 00:59:02 +00:00
shadowDiffuse = diffuseLight;
2018-08-14 20:35:23 +00:00
lightResult.xyz -= shadowDiffuse; // This light gets added a second time in the loop to fix Mesa users' slowdown, so we need to negate its contribution here.
2017-09-20 23:25:48 +00:00
#endif
2018-08-14 20:35:23 +00:00
for (int i=0; i<MAX_LIGHTS; ++i)
2016-02-17 22:29:26 +00:00
{
2017-09-21 00:59:02 +00:00
perLight(ambientLight, diffuseLight, i, viewPos, viewNormal, diffuse, ambient);
lightResult.xyz += ambientLight + diffuseLight;
2016-02-17 22:29:26 +00:00
}
lightResult.xyz += gl_LightModel.ambient.xyz * ambient;
2018-10-15 22:12:15 +00:00
if (colorMode == 1)
lightResult.xyz += vertexColor.xyz;
else
lightResult.xyz += gl_FrontMaterial.emission.xyz;
2016-02-17 22:29:26 +00:00
2016-02-18 16:08:18 +00:00
#if @clamp
2019-05-01 08:33:19 +00:00
lightResult = clamp(lightResult, vec4(0.0), vec4(1.0));
2016-02-18 16:08:18 +00:00
#else
lightResult = max(lightResult, 0.0);
#endif
2016-02-17 22:29:26 +00:00
return lightResult;
}
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)
2018-03-27 18:20:40 +00:00
return vec3(0.,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
}