From 0d765be40336decc966932e0143fe9496f3eab4b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 18 Apr 2021 20:21:54 +0300 Subject: [PATCH] Use a separate shadow caster renderable for ShadowMap This makes the configuration more flexible. For example it's possible to use a Sequence to define which tag to use for the shadow pass. --- source/effects/shadowmap.cpp | 29 +++++++++++++++++++++++------ source/effects/shadowmap.h | 7 ++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index 0af93271..639a3525 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -13,15 +13,31 @@ using namespace std; namespace Msp { namespace GL { +ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l, Renderable &c): + Effect(r), + light(l), + shadow_caster(c), + sampler(resources.get("_linear_clamp_shadow.samp")) +{ + init(s); +} + ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l): Effect(r), - size(s), light(l), - sampler(resources.get("_linear_clamp_shadow.samp")), - radius(1), - depth_bias(4), - rendered(false) + shadow_caster(r), + sampler(resources.get("_linear_clamp_shadow.samp")) { + init(s); +} + +void ShadowMap::init(unsigned s) +{ + size = s; + radius = 1; + depth_bias = 4; + rendered = false; + depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1); fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0); fbo.require_complete(); @@ -58,6 +74,7 @@ void ShadowMap::setup_frame(Renderer &renderer) rendered = true; renderable.setup_frame(renderer); + shadow_caster.setup_frame(renderer); shadow_camera.set_object_matrix(*light.get_matrix()); shadow_camera.set_position(target); @@ -78,7 +95,7 @@ void ShadowMap::setup_frame(Renderer &renderer) Renderer::Push push(renderer); renderer.set_camera(shadow_camera); - renderer.render(renderable, "shadow"); + renderer.render(shadow_caster); } void ShadowMap::finish_frame() diff --git a/source/effects/shadowmap.h b/source/effects/shadowmap.h index d7b4eef9..5d8bf509 100644 --- a/source/effects/shadowmap.h +++ b/source/effects/shadowmap.h @@ -25,6 +25,7 @@ class ShadowMap: public Effect private: unsigned size; const Light &light; + Renderable &shadow_caster; Framebuffer fbo; Camera shadow_camera; Matrix shadow_matrix; @@ -37,8 +38,12 @@ private: bool rendered; public: - ShadowMap(Resources &, unsigned, Renderable &, const Light &); + ShadowMap(Resources &, unsigned, Renderable &, const Light &, Renderable &); + DEPRECATED ShadowMap(Resources &, unsigned, Renderable &, const Light &); +private: + void init(unsigned); +public: /** Sets the ShadowMap target point and radius. The transformation matrix is computed so that a sphere with the specified parameters will be completely covered by the ShadowMap. */ -- 2.45.2