summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
Diffstat (limited to 'gfx')
-rw-r--r--gfx/gl/sceneRenderer.cpp11
-rw-r--r--gfx/gl/sceneRenderer.h5
-rw-r--r--gfx/gl/shaders/directionalLight.fs38
-rw-r--r--gfx/gl/shadowMapper.cpp4
-rw-r--r--gfx/gl/shadowMapper.h2
5 files changed, 45 insertions, 15 deletions
diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp
index 1d7507a..0fcf471 100644
--- a/gfx/gl/sceneRenderer.cpp
+++ b/gfx/gl/sceneRenderer.cpp
@@ -129,16 +129,21 @@ SceneRenderer::setDirectionalLight(
SceneRenderer::DirectionalLightProgram::DirectionalLightProgram() :
Program {lightingShader_vs, directionalLight_fs}, directionLoc {*this, "lightDirection"},
- colourLoc {*this, "lightColour"}, lightViewProjectionLoc {*this, "lightViewProjection"}
+ colourLoc {*this, "lightColour"}, lightViewProjectionLoc {*this, "lightViewProjection"},
+ lightViewProjectionCountLoc {*this, "lightViewProjectionCount"}, lightViewShadowMapRegionLoc {
+ *this, "shadowMapRegion"}
{
}
void
SceneRenderer::DirectionalLightProgram::setDirectionalLight(
- const glm::vec3 & c, const glm::vec3 & d, const glm::mat4x4 & lvp) const
+ const glm::vec3 & c, const glm::vec3 & d, const std::span<const glm::mat4x4> lvp) const
{
+ constexpr glm::vec4 shadowMapRegions {0.5F, 0.5F, 0.5F, 0.5F};
glUniform3fv(colourLoc, 1, glm::value_ptr(c));
const auto nd = glm::normalize(d);
glUniform3fv(directionLoc, 1, glm::value_ptr(nd));
- glUniformMatrix4fv(lightViewProjectionLoc, 1, GL_FALSE, glm::value_ptr(lvp));
+ glUniform1ui(lightViewProjectionCountLoc, static_cast<GLuint>(lvp.size()));
+ glUniformMatrix4fv(lightViewProjectionLoc, static_cast<GLsizei>(lvp.size()), GL_FALSE, glm::value_ptr(lvp.front()));
+ glUniform4fv(lightViewShadowMapRegionLoc, 1, glm::value_ptr(shadowMapRegions));
}
diff --git a/gfx/gl/sceneRenderer.h b/gfx/gl/sceneRenderer.h
index d7a92b7..a345b4b 100644
--- a/gfx/gl/sceneRenderer.h
+++ b/gfx/gl/sceneRenderer.h
@@ -35,10 +35,11 @@ private:
DirectionalLightProgram();
using Program::use;
- void setDirectionalLight(const glm::vec3 &, const glm::vec3 &, const glm::mat4x4 &) const;
+ void setDirectionalLight(const glm::vec3 &, const glm::vec3 &, const std::span<const glm::mat4x4>) const;
private:
- RequiredUniformLocation directionLoc, colourLoc, lightViewProjectionLoc;
+ RequiredUniformLocation directionLoc, colourLoc, lightViewProjectionLoc, lightViewProjectionCountLoc,
+ lightViewShadowMapRegionLoc;
};
DeferredLightProgram lighting;
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);
}
diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp
index 5691de9..76d4390 100644
--- a/gfx/gl/shadowMapper.cpp
+++ b/gfx/gl/shadowMapper.cpp
@@ -33,7 +33,7 @@ ShadowMapper::ShadowMapper(const glm::ivec2 & s) : size {s}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
-glm::mat4x4
+std::array<glm::mat4x4, 1>
ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const Camera & camera) const
{
auto viewExtents = camera.extentsAtDist(1) + camera.extentsAtDist(1000);
@@ -67,7 +67,7 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const C
scene.shadows(*this);
glCullFace(GL_BACK);
- return lightViewProjection;
+ return {lightViewProjection};
}
ShadowMapper::FixedPoint::FixedPoint() : Program {shadowFixedPoint_vs}, viewProjectionLoc {*this, "viewProjection"} { }
diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h
index 45a9536..2b9bc85 100644
--- a/gfx/gl/shadowMapper.h
+++ b/gfx/gl/shadowMapper.h
@@ -11,7 +11,7 @@ class ShadowMapper {
public:
ShadowMapper(const glm::ivec2 & size);
- glm::mat4x4 update(const SceneProvider &, const glm::vec3 & direction, const Camera &) const;
+ std::array<glm::mat4x4, 1> update(const SceneProvider &, const glm::vec3 & direction, const Camera &) const;
class FixedPoint : public Program {
public: