1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-07 12:54:00 +00:00
OpenMW/files/shaders/lighting.glsl

199 lines
5.7 KiB
Plaintext
Raw Normal View History

#define LIGHTING_MODEL_FFP 0
#define LIGHTING_MODEL_SINGLE_UBO 1
#define LIGHTING_MODEL_PER_OBJECT_UNIFORM 2
2016-02-17 22:29:26 +00:00
2021-02-21 18:38:15 +00:00
#if !@ffpLighting
#define getLight LightBuffer
float quickstep(float x)
{
x = clamp(x, 0.0, 1.0);
x = 1.0 - x*x;
x = 1.0 - x*x;
return x;
}
2016-02-17 22:29:26 +00:00
#if @useUBO
const uint mask = uint(0xff);
2021-03-27 19:47:41 +00:00
const uvec4 shift = uvec4(uint(0), uint(8), uint(16), uint(24));
2021-03-27 19:47:41 +00:00
vec3 unpackRGB(uint data)
{
2021-03-27 19:47:41 +00:00
return vec3( (float(((data >> shift.x) & mask)) / 255.0)
,(float(((data >> shift.y) & mask)) / 255.0)
,(float(((data >> shift.z) & mask)) / 255.0));
}
2021-02-21 18:38:15 +00:00
2021-03-27 19:47:41 +00:00
vec4 unpackRGBA(uint data)
{
2021-03-27 19:47:41 +00:00
return vec4( (float(((data >> shift.x) & mask)) / 255.0)
,(float(((data >> shift.y) & mask)) / 255.0)
,(float(((data >> shift.z) & mask)) / 255.0)
,(float(((data >> shift.w) & mask)) / 255.0));
}
2021-02-21 18:38:15 +00:00
struct LightData
2021-02-21 18:38:15 +00:00
{
uvec4 packedColors; // diffuse, ambient, specular
2021-03-02 07:30:54 +00:00
vec4 position;
vec4 attenuation; // constant, linear, quadratic, radius
2021-02-21 18:38:15 +00:00
};
uniform int PointLightIndex[@maxLights];
uniform int PointLightCount;
2021-02-21 18:38:15 +00:00
layout(std140) uniform LightBufferBinding
2021-02-21 18:38:15 +00:00
{
LightData LightBuffer[@maxLightsInScene];
2021-02-21 18:38:15 +00:00
};
#else
struct LightData
{
vec4 position;
vec4 diffuse;
vec4 ambient;
vec4 specular;
vec4 attenuation; // constant, linear, quadratic, radius
};
uniform LightData LightBuffer[@maxLights];
uniform int PointLightCount;
#endif
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 = normalize(getLight[0].position.xyz);
2021-02-21 18:38:15 +00:00
#if @lightingModel == LIGHTING_MODEL_SINGLE_UBO
2021-03-27 19:47:41 +00:00
uvec4 data = getLight[0].packedColors;
ambientOut = unpackRGB(data.y);
vec3 sunDiffuse = unpackRGB(data.x);
#else
ambientOut = getLight[0].ambient.xyz;
vec3 sunDiffuse = getLight[0].diffuse.xyz;
#endif
2021-02-21 18:38:15 +00:00
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 * 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 lightDir = getLight[lightIndex].position.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);
#if @ffpLighting
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);
#else
float illumination = clamp(1.0 / (getLight[lightIndex].attenuation.x + getLight[lightIndex].attenuation.y * lightDistance + getLight[lightIndex].attenuation.z * lightDistance * lightDistance), 0.0, 1.0);
2021-03-27 19:47:41 +00:00
illumination *= 1.0 - quickstep((lightDistance / (getLight[lightIndex].attenuation.w)) - 1.0);
2021-03-02 07:30:54 +00:00
#endif
#if @useUBO
2021-03-27 19:47:41 +00:00
uvec4 data = getLight[lightIndex].packedColors;
ambientOut = unpackRGB(data.y) * illumination;
#else
2021-02-21 18:38:15 +00:00
ambientOut = getLight[lightIndex].ambient.xyz * illumination;
#endif
2020-01-12 07:42:47 +00:00
float lambert = dot(viewNormal.xyz, lightDir) * 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
#if @useUBO
2021-03-27 19:47:41 +00:00
diffuseOut = unpackRGB(data.x) * lambert * float(int(data.w));
2021-03-02 07:30:54 +00:00
#else
diffuseOut = getLight[lightIndex].diffuse.xyz * lambert;
2021-03-02 07:30:54 +00:00
#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
perLightSun(ambientOut, diffuseOut, viewPos, viewNormal);
ambientLight += ambientOut;
diffuseLight += diffuseOut;
#if @lightingModel == LIGHTING_MODEL_FFP
for (int i=1; i < @maxLights; ++i)
2016-02-17 22:29:26 +00:00
{
perLightPoint(ambientOut, diffuseOut, i, viewPos, viewNormal);
#elif @lightingModel == LIGHTING_MODEL_PER_OBJECT_UNIFORM
for (int i=1; i <= PointLightCount; ++i)
2021-02-21 18:38:15 +00:00
{
perLightPoint(ambientOut, diffuseOut, i, viewPos, viewNormal);
#else
for (int i=0; i < PointLightCount; ++i)
{
perLightPoint(ambientOut, diffuseOut, PointLightIndex[i], viewPos, viewNormal);
2021-02-21 18:38:15 +00:00
#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
{
vec3 sunDir = getLight[0].position.xyz;
#if @lightingModel == LIGHTING_MODEL_SINGLE_UBO
vec3 sunSpec = unpackRGB(getLight[0].packedColors.z);
#else
vec3 sunSpec = getLight[0].specular.xyz;
#endif
vec3 lightDir = normalize(sunDir);
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);
return pow(max(NdotH, 0.0), max(1e-4, shininess)) * sunSpec * matSpec;
2016-02-20 18:02:11 +00:00
}