diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-07-20 10:57:17 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-08-10 18:11:55 +0100 |
commit | 900d19a41bc1886e7a809d99d6119b12235a4f0a (patch) | |
tree | 2e72221ae1a470d448497664a95d4e64acc2e8f1 /gfx/gl/shadowStenciller.cpp | |
parent | Fix texture usage via materials in shadows (diff) | |
download | ilt-900d19a41bc1886e7a809d99d6119b12235a4f0a.tar.bz2 ilt-900d19a41bc1886e7a809d99d6119b12235a4f0a.tar.xz ilt-900d19a41bc1886e7a809d99d6119b12235a4f0a.zip |
Initial commit of basic shadow depth map creation
Diffstat (limited to 'gfx/gl/shadowStenciller.cpp')
-rw-r--r-- | gfx/gl/shadowStenciller.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
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 <stdexcept> + +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(); +} |