summaryrefslogtreecommitdiff
path: root/gfx/gl/shaders/directionalLight.fs
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-12-28 15:13:01 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-12-28 15:13:05 +0000
commit80cfd4e0d5e928c2ffed9c4510441fb950908665 (patch)
tree480f819d4bb7eaf7ad6f86c9a2f9bce3c91b09c2 /gfx/gl/shaders/directionalLight.fs
parentRender some train bodies to test shadowing (diff)
downloadilt-80cfd4e0d5e928c2ffed9c4510441fb950908665.tar.bz2
ilt-80cfd4e0d5e928c2ffed9c4510441fb950908665.tar.xz
ilt-80cfd4e0d5e928c2ffed9c4510441fb950908665.zip
Initial support for multiple shadow maps in the same texture
Diffstat (limited to 'gfx/gl/shaders/directionalLight.fs')
-rw-r--r--gfx/gl/shaders/directionalLight.fs38
1 files changed, 31 insertions, 7 deletions
diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs
index d1e60ee..fa1e38d 100644
--- a/gfx/gl/shaders/directionalLight.fs
+++ b/gfx/gl/shaders/directionalLight.fs
@@ -1,6 +1,8 @@
#version 330 core
#extension GL_ARB_shading_language_420pack : enable
+const int MAX_MAPS = 3;
+
out vec3 FragColor;
in vec2 TexCoords;
@@ -11,17 +13,39 @@ layout(binding = 2) uniform sampler2D shadowMap;
uniform vec3 lightDirection;
uniform vec3 lightColour;
-uniform mat4 lightViewProjection;
+uniform mat4 lightViewProjection[MAX_MAPS];
+uniform vec4 shadowMapRegion[MAX_MAPS];
+uniform uint lightViewProjectionCount;
+
+const vec3 e1 = vec3(-1, -1, -1), e2 = vec3(1, 1, 1);
+
+float
+insideShadowCube(vec3 v)
+{
+ const vec3 s = step(e1, v) - step(e2, v);
+ return s.x * s.y * s.z;
+}
+
+float
+isShaded(vec3 Position)
+{
+ for (uint m = 0u; m < lightViewProjectionCount; m++) {
+ vec3 PositionInLightSpace = (lightViewProjection[m] * vec4(Position, 1.0f)).xyz;
+ const float inside = insideShadowCube(PositionInLightSpace);
+ if (inside > 0) {
+ const float lightSpaceDepth
+ = texture(shadowMap, PositionInLightSpace.xy * shadowMapRegion[m].xy + shadowMapRegion[m].zw).r;
+ return step(lightSpaceDepth, PositionInLightSpace.z * .5 + .5);
+ }
+ }
+ return 0;
+}
void
main()
{
const vec3 Position = texture(gPosition, TexCoords).xyz;
- const vec3 PositionInLightSpace = ((lightViewProjection * vec4(Position, 1.0f)) * 0.5 + 0.5).xyz;
- const float lightSpaceDepth = texture(shadowMap, PositionInLightSpace.xy).r;
- if (lightSpaceDepth < PositionInLightSpace.z) {
- discard;
- }
const vec3 Normal = texture(gNormal, TexCoords).rgb;
- FragColor = max(dot(-lightDirection, Normal) * lightColour, 0);
+ const float shaded = isShaded(Position);
+ FragColor = (1 - shaded) * max(dot(-lightDirection, Normal) * lightColour, 0);
}