1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 21:59:55 +00:00
openmw/files/shaders/lighting.glsl

139 lines
4.1 KiB
Text
Raw Normal View History

2021-02-21 18:38:15 +00:00
#if !@ffpLighting
2016-02-17 22:29:26 +00:00
2021-02-21 18:38:15 +00:00
#include "sun.glsl"
#define getLight PointLights
struct PointLight
{
2021-03-02 07:30:54 +00:00
vec4 position;
2021-02-21 18:38:15 +00:00
vec4 diffuse;
vec4 ambient;
2021-03-02 07:30:54 +00:00
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
float radius;
2021-02-21 18:38:15 +00:00
};
uniform int PointLightCount;
uniform int PointLightIndex[@maxLights];
layout(std140) uniform PointLightBuffer
{
2021-03-02 07:30:54 +00:00
PointLight PointLights[@maxLightsInScene];
2021-02-21 18:38:15 +00:00
};
#else
#define getLight gl_LightSource
#endif
void perLightSun(out vec3 ambientOut, out vec3 diffuseOut, vec3 viewPos, vec3 viewNormal)
{
vec3 lightDir = @sunDirection.xyz;
lightDir = normalize(lightDir);
ambientOut = @sunAmbient.xyz;
float lambert = dot(viewNormal.xyz, lightDir);
#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
diffuseOut = @sunDiffuse.xyz * lambert;
}
2021-03-02 07:30:54 +00:00
uniform float osg_SimulationTime;
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
{
2021-03-02 07:30:54 +00:00
vec4 pos = getLight[lightIndex].position;
vec3 lightDir = pos.xyz - viewPos;
2021-02-21 18:38:15 +00:00
2020-12-19 17:17:42 +00:00
float lightDistance = length(lightDir);
2017-09-20 23:25:48 +00:00
lightDir = normalize(lightDir);
2021-03-02 07:30:54 +00:00
float illumination = clamp(1.0 / (getLight[lightIndex].constantAttenuation + getLight[lightIndex].linearAttenuation * lightDistance + getLight[lightIndex].quadraticAttenuation * lightDistance * lightDistance), 0.0, 1.0);
// Add an artificial cutoff, otherwise effected objects will be brightly lit and adjacent objects not effected by this light will be dark by contrast
// This causes nasty artifacts, especially with active grid so it is necassary for now.
#if !@ffpLighting
float cutoff = getLight[lightIndex].radius * 0.5;
illumination *= 1.0 - smoothstep(0.0, 1.0, ((lightDistance / cutoff) - 1.0) * 0.887);
illumination = max(0.0, illumination);
#endif
2021-02-21 18:38:15 +00:00
ambientOut = getLight[lightIndex].ambient.xyz * illumination;
2020-01-12 07:42:47 +00:00
float lambert = dot(viewNormal.xyz, lightDir) * illumination;
#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
#if @ffpLighting
2021-02-21 18:38:15 +00:00
diffuseOut = getLight[lightIndex].diffuse.xyz * lambert;
2021-03-02 07:30:54 +00:00
#else
diffuseOut = (getLight[lightIndex].diffuse.xyz * pos.w) * lambert;
#endif
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;
// This light gets added a second time in the loop to fix Mesa users' slowdown, so we need to negate its contribution here.
2021-02-21 18:38:15 +00:00
perLightSun(ambientOut, diffuseOut, viewPos, viewNormal);
#if PER_PIXEL_LIGHTING
2020-12-19 17:17:42 +00:00
diffuseLight = diffuseOut * shadowing - diffuseOut;
2017-09-20 23:25:48 +00:00
#else
2020-12-19 17:17:42 +00:00
shadowDiffuse = diffuseOut;
diffuseLight = -diffuseOut;
2017-09-20 23:25:48 +00:00
#endif
2020-12-19 17:17:42 +00:00
ambientLight = gl_LightModel.ambient.xyz;
2021-02-21 18:38:15 +00:00
#if !@ffpLighting
perLightSun(ambientOut, diffuseOut, viewPos, viewNormal);
ambientLight += ambientOut;
diffuseLight += diffuseOut;
for (int i=0; i<PointLightCount; ++i)
2016-02-17 22:29:26 +00:00
{
2021-03-02 07:30:54 +00:00
perLightPoint(ambientOut, diffuseOut, PointLightIndex[i], viewPos, viewNormal);
2021-02-21 18:38:15 +00:00
#else
for (int i=0; i<@maxLights; ++i)
{
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
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
{
2021-02-21 18:38:15 +00:00
vec3 lightDir = normalize(@sunDirection.xyz);
2019-03-18 14:07:27 +00:00
float NdotL = dot(viewNormal, lightDir);
if (NdotL <= 0.0)
2020-12-19 17:17:42 +00:00
return vec3(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);
2021-02-21 18:38:15 +00:00
return pow(max(NdotH, 0.0), max(1e-4, shininess)) * @sunSpecular.xyz * matSpec;
2016-02-20 18:02:11 +00:00
}