mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-15 15:19:55 +00:00
70 lines
2.5 KiB
GLSL
70 lines
2.5 KiB
GLSL
#define MAX_LIGHTS 8
|
|
|
|
void perLight(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal)
|
|
{
|
|
vec3 lightDir = gl_LightSource[lightIndex].position.xyz - viewPos * gl_LightSource[lightIndex].position.w;
|
|
float lightDistance = length(lightDir);
|
|
lightDir = normalize(lightDir);
|
|
float illumination = clamp(1.0 / (gl_LightSource[lightIndex].constantAttenuation + gl_LightSource[lightIndex].linearAttenuation * lightDistance + gl_LightSource[lightIndex].quadraticAttenuation * lightDistance * lightDistance), 0.0, 1.0);
|
|
|
|
ambientOut = gl_LightSource[lightIndex].ambient.xyz * illumination;
|
|
|
|
float lambert = dot(viewNormal.xyz, lightDir) * illumination;
|
|
#ifndef GROUNDCOVER
|
|
lambert = max(lambert, 0.0);
|
|
#else
|
|
{
|
|
float cosine = dot(normalize(viewPos), normalize(viewNormal.xyz));
|
|
if (lambert >= 0.0)
|
|
cosine = -cosine;
|
|
|
|
float mult = 1.0;
|
|
float divisor = 8.0;
|
|
|
|
if (cosine < 0.0 && cosine >= -1.0/divisor)
|
|
mult = mix(1.0, 0.3, -cosine*divisor);
|
|
else if (cosine < -1.0/divisor)
|
|
mult = 0.3;
|
|
|
|
lambert *= mult;
|
|
lambert = abs(lambert);
|
|
}
|
|
#endif
|
|
diffuseOut = gl_LightSource[lightIndex].diffuse.xyz * lambert;
|
|
}
|
|
|
|
#if PER_PIXEL_LIGHTING
|
|
void doLighting(vec3 viewPos, vec3 viewNormal, float shadowing, out vec3 diffuseLight, out vec3 ambientLight)
|
|
#else
|
|
void doLighting(vec3 viewPos, vec3 viewNormal, out vec3 diffuseLight, out vec3 ambientLight, out vec3 shadowDiffuse)
|
|
#endif
|
|
{
|
|
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);
|
|
#if PER_PIXEL_LIGHTING
|
|
diffuseLight = diffuseOut * shadowing - diffuseOut;
|
|
#else
|
|
shadowDiffuse = diffuseOut;
|
|
diffuseLight = -diffuseOut;
|
|
#endif
|
|
ambientLight = gl_LightModel.ambient.xyz;
|
|
for (int i=0; i<MAX_LIGHTS; ++i)
|
|
{
|
|
perLight(ambientOut, diffuseOut, i, viewPos, viewNormal);
|
|
ambientLight += ambientOut;
|
|
diffuseLight += diffuseOut;
|
|
}
|
|
}
|
|
|
|
|
|
vec3 getSpecular(vec3 viewNormal, vec3 viewDirection, float shininess, vec3 matSpec)
|
|
{
|
|
vec3 lightDir = normalize(gl_LightSource[0].position.xyz);
|
|
float NdotL = dot(viewNormal, lightDir);
|
|
if (NdotL <= 0.0)
|
|
return vec3(0.0);
|
|
vec3 halfVec = normalize(lightDir - viewDirection);
|
|
float NdotH = dot(viewNormal, halfVec);
|
|
return pow(max(NdotH, 0.0), max(1e-4, shininess)) * gl_LightSource[0].specular.xyz * matSpec;
|
|
}
|