summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--game/scenary/foliage.cpp3
-rw-r--r--game/scenary/foliage.h2
-rw-r--r--gfx/gl/shaders/shadowStencil.fs0
-rw-r--r--gfx/gl/shaders/shadowStencil.vs11
-rw-r--r--gfx/gl/shadowStenciller.cpp51
-rw-r--r--gfx/gl/shadowStenciller.h21
6 files changed, 88 insertions, 0 deletions
diff --git a/game/scenary/foliage.cpp b/game/scenary/foliage.cpp
index 73d285f..870adb0 100644
--- a/game/scenary/foliage.cpp
+++ b/game/scenary/foliage.cpp
@@ -17,6 +17,9 @@ Foliage::postLoad()
texture = getTexture();
bodyMesh->configureVAO(instanceVAO)
.addAttribs<LocationVertex, &LocationVertex::first, &LocationVertex::second>(instances.bufferName(), 1);
+ ShadowStenciller ss;
+ ss.renderStencil(shadowStencil, *bodyMesh, {-4000, -4000, 0}, {4000, 4000, 8000});
+ Texture::saveDepth(shadowStencil, std::format("/tmp/stencil-{}.tga", id).c_str());
}
void
diff --git a/game/scenary/foliage.h b/game/scenary/foliage.h
index 0a4261c..5367d44 100644
--- a/game/scenary/foliage.h
+++ b/game/scenary/foliage.h
@@ -2,6 +2,7 @@
#include "assetFactory/asset.h"
#include "gfx/gl/instanceVertices.h"
+#include "gfx/gl/shadowStenciller.h"
#include "gfx/models/texture.h"
#include "gfx/renderable.h"
@@ -19,6 +20,7 @@ public:
mutable InstanceVertices<LocationVertex> instances;
void render(const SceneShader &) const override;
void shadows(const ShadowMapper &) const override;
+ glTexture shadowStencil = ShadowStenciller::createStencilTexture(256, 256);
protected:
friend Persistence::SelectionPtrBase<std::shared_ptr<Foliage>>;
diff --git a/gfx/gl/shaders/shadowStencil.fs b/gfx/gl/shaders/shadowStencil.fs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gfx/gl/shaders/shadowStencil.fs
diff --git a/gfx/gl/shaders/shadowStencil.vs b/gfx/gl/shaders/shadowStencil.vs
new file mode 100644
index 0000000..4f4c250
--- /dev/null
+++ b/gfx/gl/shaders/shadowStencil.vs
@@ -0,0 +1,11 @@
+#version 330 core
+#extension GL_ARB_shading_language_420pack : enable
+
+include(`meshIn.glsl')
+uniform mat4 viewProjection;
+
+void
+main()
+{
+ gl_Position = viewProjection * vec4(position, 1);
+}
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();
+}
diff --git a/gfx/gl/shadowStenciller.h b/gfx/gl/shadowStenciller.h
new file mode 100644
index 0000000..460fa68
--- /dev/null
+++ b/gfx/gl/shadowStenciller.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include "config/types.h"
+#include "gfx/gl/program.h"
+#include "gfx/models/mesh.h"
+#include "glArrays.h"
+
+class ShadowStenciller {
+public:
+ ShadowStenciller();
+
+ [[nodiscard]]
+ static glTexture createStencilTexture(GLsizei width, GLsizei height);
+ void renderStencil(const glTexture &, const MeshBase &, const RelativePosition3D & mins,
+ const RelativePosition3D & maxs) const;
+
+private:
+ glFrameBuffer fbo;
+ Program shadowCaster;
+ Program::UniformLocation viewProjectionLoc {shadowCaster, "viewProjection"};
+};