@ -20,21 +20,39 @@ float calcLambert(vec3 viewNormal, vec3 lightDir, vec3 viewDir)
return lambert;
}
float calcSpecIntensity(vec3 viewNormal, vec3 viewDir, float shininess, vec3 lightDir)
{
if (dot(viewNormal, lightDir) > 0.0)
{
vec3 halfVec = normalize(lightDir - viewDir);
float NdotH = max(dot(viewNormal, halfVec), 0.0);
return pow(NdotH, shininess);
}
return 0.0;
}
#if PER_PIXEL_LIGHTING
void doLighting(vec3 viewPos, vec3 viewNormal, float shadowing, out vec3 diffuseLight, out vec3 ambientLight)
void doLighting(vec3 viewPos, vec3 viewNormal, float shininess, float sh adowing, out vec3 diffuseLight, out vec3 ambientLight, out vec3 specular Light)
#else
void doLighting(vec3 viewPos, vec3 viewNormal, out vec3 diffuseLight, out vec3 ambientLight, out vec3 shadowDiffuse)
void doLighting(vec3 viewPos, vec3 viewNormal, float shininess, out vec3 diffuseLight, out vec3 ambientLight, out vec3 specularLight, out vec3 s hadowDiffuse, out vec3 shadowSpecular )
#endif
{
vec3 viewDir = normalize(viewPos);
shininess = max(shininess, 1e-4);
diffuseLight = lcalcDiffuse(0).xyz * calcLambert(viewNormal, normalize(lcalcPosition(0)), viewDir);
vec3 sunDir = normalize(lcalcPosition(0));
diffuseLight = lcalcDiffuse(0) * calcLambert(viewNormal, sunDir, viewDir);
ambientLight = gl_LightModel.ambient.xyz;
specularLight = lcalcSpecular(0).xyz * calcSpecIntensity(viewNormal, viewDir, shininess, sunDir);
#if PER_PIXEL_LIGHTING
diffuseLight *= shadowing;
specularLight *= shadowing;
#else
shadowDiffuse = diffuseLight;
shadowSpecular = specularLight;
diffuseLight = vec3(0.0);
specularLight = vec3(0.0);
#endif
for (int i = @startLight; i < @endLight; ++i)
@ -56,52 +74,10 @@ void doLighting(vec3 viewPos, vec3 viewNormal, out vec3 diffuseLight, out vec3 a
vec3 lightDir = lightPos / lightDistance;
float illumination = lcalcIllumination(lightIndex, lightDistance);
ambientLight += lcalcAmbient(lightIndex) * illumination;
diffuseLight += lcalcDiffuse(lightIndex) * calcLambert(viewNormal, lightDir, viewDir) * illumination;
ambientLight += lcalcAmbient(lightIndex) * illumination;
specularLight += lcalcSpecular(lightIndex).xyz * calcSpecIntensity(viewNormal, viewDir, shininess, lightDir) * illumination;
}
}
float calcSpecIntensity(vec3 viewNormal, vec3 viewDir, float shininess, vec3 lightDir)
{
if (dot(viewNormal, lightDir) > 0.0)
{
vec3 halfVec = normalize(lightDir - viewDir);
float NdotH = max(dot(viewNormal, halfVec), 0.0);
return pow(NdotH, shininess);
}
return 0.0;
}
vec3 getSpecular(vec3 viewNormal, vec3 viewPos, float shininess, float shadowing)
{
shininess = max(shininess, 1e-4);
vec3 viewDir = normalize(viewPos);
vec3 specularLight = lcalcSpecular(0).xyz * calcSpecIntensity(viewNormal, viewDir, shininess, normalize(lcalcPosition(0)));
specularLight *= shadowing;
for (int i = @startLight; i < @endLight; ++i)
{
#if @lightingMethodUBO
int lightIndex = PointLightIndex[i];
#else
int lightIndex = i;
#endif
vec3 lightPos = lcalcPosition(lightIndex) - viewPos;
float lightDistance = length(lightPos);
#if !@lightingMethodFFP
if (lightDistance > lcalcRadius(lightIndex) * 2.0)
continue;
#endif
float illumination = lcalcIllumination(lightIndex, lightDistance);
float intensity = calcSpecIntensity(viewNormal, viewDir, shininess, normalize(lightPos));
specularLight += lcalcSpecular(lightIndex).xyz * intensity * illumination;
}
return specularLight;
}
#endif