1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-25 06:35:30 +00:00
OpenMW/files/shaders/lighting.glsl

100 lines
2.9 KiB
Plaintext
Raw Normal View History

#include "lighting_util.glsl"
2016-02-17 23:29:26 +01:00
void perLightSun(out vec3 diffuseOut, vec3 viewPos, vec3 viewNormal)
2021-02-21 10:38:15 -08:00
{
vec3 lightDir = normalize(lcalcPosition(0));
2021-02-21 10:38:15 -08:00
float lambert = dot(viewNormal.xyz, lightDir);
2021-04-07 18:39:19 -07:00
2021-02-21 10:38:15 -08: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-07 18:39:19 -07:00
diffuseOut = lcalcDiffuse(0).xyz * lambert;
}
2021-03-01 23:30:54 -08:00
2021-02-21 10:38:15 -08:00
void perLightPoint(out vec3 ambientOut, out vec3 diffuseOut, int lightIndex, vec3 viewPos, vec3 viewNormal)
2016-02-17 23:29:26 +01:00
{
vec3 lightPos = lcalcPosition(lightIndex) - viewPos;
float lightDistance = length(lightPos);
2021-03-27 23:41:22 -07:00
2021-04-07 18:39:19 -07: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-03 22:25:13 -07:00
if (lightDistance > radius * 2.0)
2021-03-27 23:41:22 -07:00
{
ambientOut = vec3(0.0);
diffuseOut = vec3(0.0);
return;
}
#endif
lightPos = normalize(lightPos);
2017-09-21 00:25:48 +01:00
float illumination = lcalcIllumination(lightIndex, lightDistance);
ambientOut = lcalcAmbient(lightIndex) * illumination;
float lambert = dot(viewNormal.xyz, lightPos) * illumination;
2020-01-12 11:42:47 +04: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 11:42:47 +04:00
{
2021-02-09 21:56:21 +00:00
lambert = -lambert;
eyeCosine = -eyeCosine;
2020-01-12 11:42:47 +04: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 11:42:47 +04:00
#endif
2021-03-01 23:30:54 -08:00
diffuseOut = lcalcDiffuse(lightIndex) * lambert;
2017-09-21 00:25:48 +01:00
}
#if PER_PIXEL_LIGHTING
2020-12-19 20:17:42 +03:00
void doLighting(vec3 viewPos, vec3 viewNormal, float shadowing, out vec3 diffuseLight, out vec3 ambientLight)
2017-09-21 00:25:48 +01:00
#else
2020-12-19 20:17:42 +03:00
void doLighting(vec3 viewPos, vec3 viewNormal, out vec3 diffuseLight, out vec3 ambientLight, out vec3 shadowDiffuse)
2017-09-21 00:25:48 +01:00
#endif
{
2020-12-19 20:17:42 +03:00
vec3 ambientOut, diffuseOut;
2021-02-21 10:38:15 -08:00
perLightSun(diffuseOut, viewPos, viewNormal);
ambientLight = gl_LightModel.ambient.xyz;
#if PER_PIXEL_LIGHTING
2021-04-07 18:39:19 -07:00
diffuseLight = diffuseOut * shadowing;
2017-09-21 00:25:48 +01:00
#else
2020-12-19 20:17:42 +03:00
shadowDiffuse = diffuseOut;
diffuseLight = vec3(0.0);
2017-09-21 00:25:48 +01: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 20:17:42 +03:00
ambientLight += ambientOut;
diffuseLight += diffuseOut;
2016-02-17 23:29:26 +01:00
}
}
2016-02-20 19:02:11 +01:00
2016-02-21 01:28:42 +01:00
vec3 getSpecular(vec3 viewNormal, vec3 viewDirection, float shininess, vec3 matSpec)
2016-02-20 19:02:11 +01:00
{
vec3 lightDir = normalize(lcalcPosition(0));
2019-03-18 17:07:27 +03:00
float NdotL = dot(viewNormal, lightDir);
if (NdotL <= 0.0)
2020-12-19 20:17:42 +03:00
return vec3(0.0);
2016-02-21 01:28:42 +01:00
vec3 halfVec = normalize(lightDir - viewDirection);
2019-03-18 17:07:27 +03:00
float NdotH = dot(viewNormal, halfVec);
return pow(max(NdotH, 0.0), max(1e-4, shininess)) * lcalcSpecular(0).xyz * matSpec;
2016-02-20 19:02:11 +01:00
}