]> git.tdb.fi Git - libs/gl.git/blobdiff - source/effects/shadowmap.cpp
Change various generated texture names to use the unified extension
[libs/gl.git] / source / effects / shadowmap.cpp
index 11bb706e6afa3265562e277e3ac899783e888095..bfb053421440d98bdd3164a6e16d092e1479aa47 100644 (file)
-#include <cmath>
-#include <cstdlib>
-#include "camera.h"
-#include "light.h"
+#include <msp/strings/format.h>
+#include "directionallight.h"
+#include "error.h"
+#include "lighting.h"
 #include "renderer.h"
 #include "resources.h"
-#include "scene.h"
 #include "shadowmap.h"
-#include "tests.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GL {
 
-ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l, Renderable &c):
+ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l, Renderable &c):
        Effect(r),
-       light(l),
+       width(w),
+       height(h),
+       lighting(l),
        shadow_caster(c),
-       sampler(resources.get<Sampler>("_linear_clamp_shadow.samp"))
+       sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
 {
-       init(s);
+       depth_buf.storage(DEPTH_COMPONENT32F, width, height, 1);
+       fbo.set_format((DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
+       fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
+
+       set_darkness(1.0f);
+       for(unsigned i=0; i<4; ++i)
+       {
+               string base = format("shadows[%d]", i);
+               shdata.uniform(base+".enabled", 0);
+               shdata.uniform(base+".darkness", 1.0f);
+               shdata.uniform(base+".shd_world_matrix", Matrix());
+               shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
+       }
 }
 
-ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l):
-       Effect(r),
-       light(l),
-       shadow_caster(r),
-       sampler(resources.get<Sampler>("_linear_clamp_shadow.samp"))
+ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
+       ShadowMap(s, s, r, 0, c)
 {
-       init(s);
+       add_light(l, s);
 }
 
-void ShadowMap::init(unsigned s)
+ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l, Renderable &c):
+       ShadowMap(w, h, r, &l, c)
+{ }
+
+void ShadowMap::add_light(const DirectionalLight &light, unsigned s)
 {
-       size = s;
-       radius = 1;
-       depth_bias = 4;
-       rendered = false;
+       if(!lighting && !lights.empty())
+               throw invalid_operation("ShadowMap::add_light");
 
-       depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
-       fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
-       fbo.require_complete();
+       int index = (lighting ? lighting->find_light_index(light) : 0);
+       if(index<0)
+               throw invalid_argument("ShadowMap::add_light");
+
+       Rect region(0, 0, s, s);
+       while(1)
+       {
+               int next_bottom = height;
+               int next_left = region.left;
+
+               int top = region.bottom+region.height;
+               int right = region.left+region.width;
+               for(const ShadowedLight &l: lights)
+               {
+                       int l_top = l.region.bottom+l.region.height;
+                       int l_right = l.region.left+l.region.width;
+                       if(l_top>region.bottom)
+                               next_bottom = min(next_bottom, l_top);
+
+                       if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
+                               next_left = max(next_left, l_right);
+               }
+
+               if(next_left==region.left)
+                       break;
+               else if(next_left+region.width>width)
+               {
+                       if(next_bottom+region.height>height)
+                               throw invalid_operation("ShadowMap::add_light");
+                       region.bottom = next_bottom;
+                       region.left = 0;
+               }
+               else
+                       region.left = next_left;
+       }
 
-       set_darkness(0.7);
-       shdata.uniform("shd_world_matrix", Matrix());
+       lights.emplace_back();
+       ShadowedLight &sl = lights.back();
+       sl.light = &light;
+       sl.index = index;
+       sl.region = region;
+
+       string base = format("shadows[%d]", index);
+       shdata.uniform(base+".enabled", 1);
+       shdata.uniform(base+".darkness", darkness);
+
+       float xf = static_cast<float>(region.left)/width;
+       float yf = static_cast<float>(region.bottom)/height;
+       float wf = static_cast<float>(region.width)/width;
+       float hf = static_cast<float>(region.height)/height;
+       shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
+
+#ifdef DEBUG
+       if(!debug_name.empty())
+               sl.shadow_camera.set_debug_name(format("%s/light%d.camera", debug_name, lights.size()-1));
+#endif
 }
 
 void ShadowMap::set_target(const Vector3 &t, float r)
@@ -57,7 +118,9 @@ void ShadowMap::set_darkness(float d)
        if(d<0.0f || d>1.0f)
                throw invalid_argument("ShadowMap::set_darkness");
 
-       shdata.uniform("shadow_darkness", d);
+       darkness = d;
+       for(const ShadowedLight &l: lights)
+               shdata.uniform(format("shadows[%d].darkness", l.index), d);
 }
 
 void ShadowMap::set_depth_bias(float b)
@@ -77,28 +140,31 @@ void ShadowMap::setup_frame(Renderer &renderer)
        renderable.setup_frame(renderer);
        shadow_caster.setup_frame(renderer);
 
-       shadow_camera.set_object_matrix(*light.get_matrix());
-       shadow_camera.set_position(target);
-       // TODO support point and spot lights with a frustum projection.
-       // Omnidirectional lights also need a cube shadow map.
-       shadow_camera.set_orthographic(radius*2, radius*2);
-       shadow_camera.set_depth_clip(-radius, radius);
-
-       shadow_matrix = shadow_camera.get_object_matrix();
-       shadow_matrix.scale(radius*2, radius*2, -radius*2);
-       shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
-       shadow_matrix.invert();
+       for(ShadowedLight &l: lights)
+       {
+               l.shadow_camera.set_object_matrix(*l.light->get_matrix());
+               l.shadow_camera.set_position(target);
+               // TODO support point and spot lights with a frustum projection.
+               // Omnidirectional lights also need a cube shadow map.
+               l.shadow_camera.set_orthographic(radius*2, radius*2);
+               l.shadow_camera.set_depth_clip(-radius, radius);
 
-       shdata.uniform("shd_world_matrix", shadow_matrix);
+               Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/l.region.width)).scale(0.5f);
+               Matrix shadow_matrix = to_texcoord*l.shadow_camera.get_projection_matrix()*l.shadow_camera.get_view_matrix();
 
-       BindRestore bind_fbo(fbo);
-       Bind bind_depth(DepthTest::lequal());
-       fbo.clear(DEPTH_BUFFER_BIT);
+               shdata.uniform(format("shadows[%d].shd_world_matrix", l.index), shadow_matrix);
+       }
 
-       Renderer::Push push(renderer);
-       renderer.set_camera(shadow_camera);
+       for(ShadowedLight &l: lights)
+       {
+               Renderer::Push push(renderer);
+               renderer.set_framebuffer(&fbo);
+               renderer.set_viewport(&l.region);
+               renderer.set_scissor(&l.region);
+               renderer.set_camera(l.shadow_camera);
 
-       renderer.render(shadow_caster);
+               renderer.render(shadow_caster);
+       }
 }
 
 void ShadowMap::finish_frame()
@@ -122,5 +188,18 @@ void ShadowMap::render(Renderer &renderer, Tag tag) const
        renderer.render(renderable, tag);
 }
 
+void ShadowMap::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       fbo.set_debug_name(name+" [FBO]");
+       for(unsigned i=0; i<lights.size(); ++i)
+               lights[i].shadow_camera.set_debug_name(format("%s/light%d.camera", name, i));
+       depth_buf.set_debug_name(name+"/depth.tex");
+       shdata.set_debug_name(name+" [UBO]");
+#else
+       (void)name;
+#endif
+}
+
 } // namespace GL
 } // namespace Msp