1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-06 00:55:50 +00:00
OpenMW/files/shaders/lib/light/lighting.glsl

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
3.8 KiB
Plaintext
Raw Normal View History

2023-02-25 19:03:39 +00:00
#ifndef LIB_LIGHT_LIGHTING
#define LIB_LIGHT_LIGHTING
#include "lighting_util.glsl"
2016-02-17 22:29:26 +00:00
void perLightSun(out vec3 diffuseOut, vec3 viewPos, vec3 viewNormal)
2021-02-21 18:38:15 +00:00
{
vec3 lightDir = normalize(lcalcPosition(0));
2021-02-21 18:38:15 +00:00
float lambert = dot(viewNormal.xyz, lightDir);
2021-04-08 01:39:19 +00:00
2021-02-21 18:38:15 +00:00
#ifndef GROUNDCOVER
lambert = max(lambert, 0.0);
#else
float eyeCosine = dot(normalize(viewPos), viewNormal.xyz);
if (lambert < 0.0)
{
lambert = -lambert;
eyeCosine = -eyeCosine;
}
lambert *= clamp(-8.0 * (1.0 - 0.3) * eyeCosine + 1.0, 0.3, 1.0);
#endif
2021-04-08 01:39:19 +00:00
diffuseOut = lcalcDiffuse(0).xyz * lambert;
}
2021-03-02 07:30:54 +00:00
2021-02-21 18:38:15 +00:00
void perLightPoint(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal)
2016-02-17 22:29:26 +00:00
{
vec3 lightPos = lcalcPosition(lightIndex) - viewPos;
float lightDistance = length(lightPos);
2021-03-28 06:41:22 +00:00
2021-04-08 01:39:19 +00:00
// cull non-FFP point lighting by radius, light is guaranteed to not fall outside this bound with our cutoff
#if !@lightingMethodFFP
float radius = lcalcRadius(lightIndex);
2021-04-04 05:25:13 +00:00
if (lightDistance > radius * 2.0)
2021-03-28 06:41:22 +00:00
{
ambientOut = vec3(0.0);
diffuseOut = vec3(0.0);
return;
}
#endif
lightPos = normalize(lightPos);
2017-09-20 23:25:48 +00:00
float illumination = lcalcIllumination(lightIndex, lightDistance);
ambientOut = lcalcAmbient(lightIndex) * illumination;
float lambert = dot(viewNormal.xyz, lightPos) * illumination;
2020-01-12 07:42:47 +00:00
#ifndef GROUNDCOVER
lambert = max(lambert, 0.0);
#else
2021-02-09 21:56:21 +00:00
float eyeCosine = dot(normalize(viewPos), viewNormal.xyz);
if (lambert < 0.0)
2020-01-12 07:42:47 +00:00
{
2021-02-09 21:56:21 +00:00
lambert = -lambert;
eyeCosine = -eyeCosine;
2020-01-12 07:42:47 +00:00
}
2021-02-09 21:56:21 +00:00
lambert *= clamp(-8.0 * (1.0 - 0.3) * eyeCosine + 1.0, 0.3, 1.0);
2020-01-12 07:42:47 +00:00
#endif
2021-03-02 07:30:54 +00:00
diffuseOut = lcalcDiffuse(lightIndex) * lambert;
2017-09-20 23:25:48 +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;
2021-02-21 18:38:15 +00:00
perLightSun(diffuseOut, viewPos, viewNormal);
ambientLight = gl_LightModel.ambient.xyz;
#if PER_PIXEL_LIGHTING
2021-04-08 01:39:19 +00:00
diffuseLight = diffuseOut * shadowing;
2017-09-20 23:25:48 +00:00
#else
2020-12-19 17:17:42 +00:00
shadowDiffuse = diffuseOut;
diffuseLight = vec3(0.0);
2017-09-20 23:25:48 +00:00
#endif
for (int i = @startLight; i < @endLight; ++i)
{
#if @lightingMethodUBO
perLightPoint(ambientOut, diffuseOut, PointLightIndex[i], viewPos, viewNormal);
#else
perLightPoint(ambientOut, diffuseOut, i, viewPos, viewNormal);
#endif
2020-12-19 17:17:42 +00:00
ambientLight += ambientOut;
diffuseLight += diffuseOut;
2016-02-17 22:29:26 +00:00
}
}
2016-02-20 18:02:11 +00:00
float calcSpecIntensity(vec3 viewNormal, vec3 viewDir, float shininess, vec3 lightDir)
2016-02-20 18:02:11 +00:00
{
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;
2016-02-20 18:02:11 +00:00
}
2023-02-25 19:03:39 +00:00
#endif