From 900d19a41bc1886e7a809d99d6119b12235a4f0a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jul 2024 10:57:17 +0100 Subject: Initial commit of basic shadow depth map creation --- gfx/gl/shadowStenciller.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 gfx/gl/shadowStenciller.cpp (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp new file mode 100644 index 0000000..6d5fa40 --- /dev/null +++ b/gfx/gl/shadowStenciller.cpp @@ -0,0 +1,51 @@ +#include "shadowStenciller.h" +#include "config/types.h" +#include "gfx/gl/program.h" +#include "gfx/gl/shaders/vs-shadowStencil.h" +#include "gfx/models/mesh.h" +#include "glArrays.h" +#include "gl_traits.h" +#include "maths.h" +#include + +ShadowStenciller::ShadowStenciller() : shadowCaster {shadowStencil_vs} +{ + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +glTexture +ShadowStenciller::createStencilTexture(GLsizei width, GLsizei height) +{ + glTexture stencil; + glBindTexture(GL_TEXTURE_2D, stencil); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr); + + return stencil; +} + +void +ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh, const RelativePosition3D & mins, + const RelativePosition3D & maxs) const +{ + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, stencil, 0); + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + throw std::runtime_error("Stencil framebuffer not complete!"); + } + glUseProgram(shadowCaster); + glClear(GL_DEPTH_BUFFER_BIT); + glViewport(0, 0, 256, 256); + glEnable(GL_DEPTH_TEST); + const auto extents = glm::ortho(mins.x, maxs.x, mins.z, maxs.z, mins.y, maxs.y); + const auto lightDir = glm::lookAt({}, north, up); + glUniform(viewProjectionLoc, extents * lightDir); + mesh.Draw(); +} -- cgit v1.2.3 From 4ba67f34eac848e43bad2ccc4b5c09ac65dd7952 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 20 Jul 2024 12:55:15 +0100 Subject: Use mesh extents for shadow stencil extents --- gfx/gl/shadowStenciller.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index 6d5fa40..e20eda1 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -32,8 +32,7 @@ ShadowStenciller::createStencilTexture(GLsizei width, GLsizei height) } void -ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh, const RelativePosition3D & mins, - const RelativePosition3D & maxs) const +ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh) const { glBindFramebuffer(GL_FRAMEBUFFER, fbo); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, stencil, 0); @@ -44,6 +43,8 @@ ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, 256, 256); glEnable(GL_DEPTH_TEST); + const auto & mins = mesh.minExtent(); + const auto & maxs = mesh.maxExtent(); const auto extents = glm::ortho(mins.x, maxs.x, mins.z, maxs.z, mins.y, maxs.y); const auto lightDir = glm::lookAt({}, north, up); glUniform(viewProjectionLoc, extents * lightDir); -- cgit v1.2.3 From f737aada2b7164683303beb3bb490c30a4949fb0 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 21 Jul 2024 02:33:24 +0100 Subject: Use texture alpha in shadow stencil --- gfx/gl/shadowStenciller.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index e20eda1..2ba519f 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -1,6 +1,7 @@ #include "shadowStenciller.h" #include "config/types.h" #include "gfx/gl/program.h" +#include "gfx/gl/shaders/fs-shadowStencil.h" #include "gfx/gl/shaders/vs-shadowStencil.h" #include "gfx/models/mesh.h" #include "glArrays.h" @@ -8,7 +9,7 @@ #include "maths.h" #include -ShadowStenciller::ShadowStenciller() : shadowCaster {shadowStencil_vs} +ShadowStenciller::ShadowStenciller() : shadowCaster {shadowStencil_vs, shadowStencil_fs} { glBindFramebuffer(GL_FRAMEBUFFER, fbo); glDrawBuffer(GL_NONE); @@ -32,13 +33,16 @@ ShadowStenciller::createStencilTexture(GLsizei width, GLsizei height) } void -ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh) const +ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh, const Texture::AnyPtr texture) const { glBindFramebuffer(GL_FRAMEBUFFER, fbo); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, stencil, 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { throw std::runtime_error("Stencil framebuffer not complete!"); } + if (texture) { + texture->bind(); + } glUseProgram(shadowCaster); glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, 256, 256); -- cgit v1.2.3 From 064864eac61e936af909470fcccc5c67ef6d3169 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 11 Aug 2024 19:32:23 +0100 Subject: Calculate centre and size of mesh, wrap it all in a Dimensions object --- gfx/gl/shadowStenciller.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index 2ba519f..dc87d76 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -47,8 +47,8 @@ ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, 256, 256); glEnable(GL_DEPTH_TEST); - const auto & mins = mesh.minExtent(); - const auto & maxs = mesh.maxExtent(); + const auto & mins = mesh.getDimensions().minExtent; + const auto & maxs = mesh.getDimensions().maxExtent; const auto extents = glm::ortho(mins.x, maxs.x, mins.z, maxs.z, mins.y, maxs.y); const auto lightDir = glm::lookAt({}, north, up); glUniform(viewProjectionLoc, extents * lightDir); -- cgit v1.2.3 From 172beac34e82c86f8c16b8a1be5fca9d7ccfc0d1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 18 Aug 2024 16:14:29 +0100 Subject: Update asset stencils from shadow mapper --- gfx/gl/shadowStenciller.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index dc87d76..9f13808 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -1,15 +1,14 @@ #include "shadowStenciller.h" -#include "config/types.h" #include "gfx/gl/program.h" #include "gfx/gl/shaders/fs-shadowStencil.h" #include "gfx/gl/shaders/vs-shadowStencil.h" #include "gfx/models/mesh.h" #include "glArrays.h" #include "gl_traits.h" -#include "maths.h" #include -ShadowStenciller::ShadowStenciller() : shadowCaster {shadowStencil_vs, shadowStencil_fs} +ShadowStenciller::ShadowStenciller(const Direction3D & lightDir, const Direction3D & lightDirUp) : + shadowCaster {shadowStencil_vs, shadowStencil_fs}, lightDirMat {glm::lookAt(-lightDir, {}, lightDirUp)} { glBindFramebuffer(GL_FRAMEBUFFER, fbo); glDrawBuffer(GL_NONE); @@ -47,10 +46,10 @@ ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, 256, 256); glEnable(GL_DEPTH_TEST); - const auto & mins = mesh.getDimensions().minExtent; - const auto & maxs = mesh.getDimensions().maxExtent; - const auto extents = glm::ortho(mins.x, maxs.x, mins.z, maxs.z, mins.y, maxs.y); - const auto lightDir = glm::lookAt({}, north, up); - glUniform(viewProjectionLoc, extents * lightDir); + const auto & centre = mesh.getDimensions().centre; + const auto & size = mesh.getDimensions().size; + const auto extentsMat + = glm::translate(glm::ortho(-size, size, -size, size, -size, size), {-centre.x, -centre.z, -centre.y}); + glUniform(viewProjectionLoc, extentsMat * lightDirMat); mesh.Draw(); } -- cgit v1.2.3 From 08c9a8ea438f25ae8012d80d2c2c74c799f5543c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 19 Aug 2024 23:07:12 +0100 Subject: Shuffle some GL state setting Ensures the right things are set at the right times, it's more calls but its less prone to randomly getting screwed over. Also updates the comments about which phases do what. --- gfx/gl/shadowStenciller.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index 9f13808..fc3d77c 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -45,7 +45,6 @@ ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh glUseProgram(shadowCaster); glClear(GL_DEPTH_BUFFER_BIT); glViewport(0, 0, 256, 256); - glEnable(GL_DEPTH_TEST); const auto & centre = mesh.getDimensions().centre; const auto & size = mesh.getDimensions().size; const auto extentsMat -- cgit v1.2.3 From dd452f3e9238be954c3ecd325ed11e97a50ec1c3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 19 Aug 2024 23:26:32 +0100 Subject: Persist a single ShadowStenciller within ShadowMapper --- gfx/gl/shadowStenciller.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index fc3d77c..ac63b4e 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -7,8 +7,7 @@ #include "gl_traits.h" #include -ShadowStenciller::ShadowStenciller(const Direction3D & lightDir, const Direction3D & lightDirUp) : - shadowCaster {shadowStencil_vs, shadowStencil_fs}, lightDirMat {glm::lookAt(-lightDir, {}, lightDirUp)} +ShadowStenciller::ShadowStenciller() : shadowCaster {shadowStencil_vs, shadowStencil_fs} { glBindFramebuffer(GL_FRAMEBUFFER, fbo); glDrawBuffer(GL_NONE); @@ -16,6 +15,12 @@ ShadowStenciller::ShadowStenciller(const Direction3D & lightDir, const Direction glBindFramebuffer(GL_FRAMEBUFFER, 0); } +void +ShadowStenciller::setLightDirection(const Direction3D & lightDir, const Direction3D & lightDirUp) +{ + lightDirMat = glm::lookAt(-lightDir, {}, lightDirUp); +} + glTexture ShadowStenciller::createStencilTexture(GLsizei width, GLsizei height) { -- cgit v1.2.3 From ce6659f5581a7d1f6b0f7b42e29c831e93315be8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 26 Aug 2024 19:07:37 +0100 Subject: Set stencil texture min/mag filters --- gfx/gl/shadowStenciller.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index ac63b4e..0c34da5 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -28,6 +28,8 @@ ShadowStenciller::createStencilTexture(GLsizei width, GLsizei height) glBindTexture(GL_TEXTURE_2D, stencil); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); -- cgit v1.2.3 From 052b2b49d9bfdaaa7f7c5603fa7c47b881df93c2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 2 Sep 2024 21:00:41 +0100 Subject: Update stencil texture to 2d array --- gfx/gl/shadowStenciller.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index 0c34da5..01248bd 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -25,15 +25,16 @@ glTexture ShadowStenciller::createStencilTexture(GLsizei width, GLsizei height) { glTexture stencil; - glBindTexture(GL_TEXTURE_2D, stencil); + glBindTexture(GL_TEXTURE_2D_ARRAY, stencil); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr); + glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT, width, height, 8, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, + nullptr); return stencil; } -- cgit v1.2.3 From f22f79330f9b8851cda76d1e11555877cbb72d80 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 3 Sep 2024 01:37:03 +0100 Subject: Populate all layers of the stencil texture Albeit with the same projection --- gfx/gl/shadowStenciller.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index 01248bd..1063b71 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -1,13 +1,14 @@ #include "shadowStenciller.h" #include "gfx/gl/program.h" #include "gfx/gl/shaders/fs-shadowStencil.h" +#include "gfx/gl/shaders/gs-shadowStencil.h" #include "gfx/gl/shaders/vs-shadowStencil.h" #include "gfx/models/mesh.h" #include "glArrays.h" #include "gl_traits.h" #include -ShadowStenciller::ShadowStenciller() : shadowCaster {shadowStencil_vs, shadowStencil_fs} +ShadowStenciller::ShadowStenciller() : shadowCaster {shadowStencil_vs, shadowStencil_gs, shadowStencil_fs} { glBindFramebuffer(GL_FRAMEBUFFER, fbo); glDrawBuffer(GL_NONE); -- cgit v1.2.3 From 5ec74c35fb1f2524add7fd03ace78667f93edb2e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 7 Sep 2024 12:35:03 +0100 Subject: Populate all layers of shadow stencil with view from all around --- gfx/gl/shadowStenciller.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'gfx/gl/shadowStenciller.cpp') diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index 1063b71..da2b3a0 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -6,9 +6,18 @@ #include "gfx/models/mesh.h" #include "glArrays.h" #include "gl_traits.h" +#include "maths.h" #include -ShadowStenciller::ShadowStenciller() : shadowCaster {shadowStencil_vs, shadowStencil_gs, shadowStencil_fs} +namespace { + static constexpr std::array anglesEigthPi {-3, -2, -1, 0, 1, 2, 3, 4}; + static const auto angles = anglesEigthPi * [](auto ep) { + return rotate_yaw(ep * quarter_pi); + }; +} + +ShadowStenciller::ShadowStenciller() : + shadowCaster {shadowStencil_vs, shadowStencil_gs, shadowStencil_fs}, viewProjections {} { glBindFramebuffer(GL_FRAMEBUFFER, fbo); glDrawBuffer(GL_NONE); @@ -20,6 +29,9 @@ void ShadowStenciller::setLightDirection(const Direction3D & lightDir, const Direction3D & lightDirUp) { lightDirMat = glm::lookAt(-lightDir, {}, lightDirUp); + viewProjections = angles * [this](const auto & a) { + return lightDirMat * a; + }; } glTexture @@ -58,6 +70,8 @@ ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh const auto & size = mesh.getDimensions().size; const auto extentsMat = glm::translate(glm::ortho(-size, size, -size, size, -size, size), {-centre.x, -centre.z, -centre.y}); - glUniform(viewProjectionLoc, extentsMat * lightDirMat); + glUniform(viewProjectionLoc, std::span {viewProjections * [&](const auto & vp) { + return extentsMat * vp; + }}); mesh.Draw(); } -- cgit v1.2.3