X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Feffects%2Fshadowmap.cpp;h=4cc0475d3e6ad79fae9d207755161ffba3826503;hb=bace0b24414abc7b3ba46df3a9fd7408d7479a5e;hp=bcdb0c9f39ef6ec24ac5ac3121f8a8b3bd0be64b;hpb=6353307898cd397e2bcde13e2448a8a678a60004;p=libs%2Fgl.git diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index bcdb0c9f..4cc0475d 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -1,10 +1,9 @@ -#include -#include -#include "camera.h" -#include "light.h" +#include +#include "directionallight.h" +#include "error.h" +#include "lighting.h" #include "renderer.h" #include "resources.h" -#include "scene.h" #include "shadowmap.h" using namespace std; @@ -12,40 +11,119 @@ using namespace std; namespace Msp { namespace GL { -ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l, Renderable &c): +ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l): Effect(r), - light(l), - shadow_caster(c), + width(w), + height(h), + lighting(l), sampler(Resources::get_global().get("_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+".type", 0); + shdata.uniform(base+".darkness", 1.0f); + shdata.uniform(base+".matrix_index", 0); + shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f)); + } + + Matrix dummy_matrix; + shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix); } -ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l): - Effect(r), - light(l), - shadow_caster(r), - sampler(Resources::get_global().get("_linear_clamp_shadow.samp")) +ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c): + ShadowMap(s, s, r, 0) { - init(s); + add_light(l, s, c); } -void ShadowMap::init(unsigned s) +ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l): + ShadowMap(w, h, r, &l) +{ } + +void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c) { - size = s; - radius = 1; - depth_bias = 4; - rendered = false; + add_light(light, s, DIRECTIONAL, c); +} - depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1); - fbo.set_format((DEPTH_ATTACHMENT,DEPTH_COMPONENT32F)); - fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0); +void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c) +{ + if(!lighting && !lights.empty()) + throw invalid_operation("ShadowMap::add_light"); - depth_test.enabled = true; - depth_test.compare = LEQUAL; + int index = (lighting ? lighting->find_light_index(light) : 0); + if(index<0) + throw invalid_argument("ShadowMap::add_light"); - set_darkness(1.0f); - shdata.uniform("shd_world_matrix", Matrix()); + 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.bottoml.region.left && region.leftwidth) + { + if(next_bottom+region.height>height) + throw invalid_operation("ShadowMap::add_light"); + region.bottom = next_bottom; + region.left = 0; + } + else + region.left = next_left; + } + + lights.emplace_back(); + ShadowedLight &sl = lights.back(); + sl.light = &light; + sl.index = index; + sl.region = region; + sl.type = type; + sl.view_index = views.size(); + sl.shadow_caster = &c; + + views.emplace_back(); + ShadowView &view = views[sl.view_index]; + view.light_index = lights.size()-1; + view.face = 0; + + string base = format("shadows[%d]", index); + shdata.uniform(base+".type", static_cast(type)); + shdata.uniform(base+".darkness", darkness); + shdata.uniform(base+".matrix_index", static_cast(sl.view_index)); + + float xf = static_cast(region.left)/width; + float yf = static_cast(region.bottom)/height; + float wf = static_cast(region.width)/width; + float hf = static_cast(region.height)/height; + shdata.uniform(base+".region", Vector4(xf, yf, wf, hf)); + +#ifdef DEBUG + if(!debug_name.empty()) + { + for(unsigned i=sl.view_index; i1.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,29 +157,41 @@ void ShadowMap::setup_frame(Renderer &renderer) rendered = true; renderable.setup_frame(renderer); - shadow_caster.setup_frame(renderer); + for(const ShadowedLight &l: lights) + l.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); + vector shadow_matrices; + shadow_matrices.reserve(views.size()); + for(ShadowView &v: views) + { + const ShadowedLight &light = lights[v.light_index]; + + if(light.type==DIRECTIONAL) + { + v.camera.set_object_matrix(*light.light->get_matrix()); + v.camera.set_position(target); + v.camera.set_orthographic(radius*2, radius*2); + v.camera.set_depth_clip(-radius, radius); + } + + Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/light.region.width)).scale(0.5f); + shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix()); + } - 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(); + shdata.uniform_array("shd_world_matrix", shadow_matrices.size(), shadow_matrices.data()); - shdata.uniform("shd_world_matrix", shadow_matrix); + for(const ShadowView &v: views) + { + const ShadowedLight &light = lights[v.light_index]; - Renderer::Push push(renderer); - renderer.set_framebuffer(&fbo); - renderer.clear(DEPTH_BUFFER_BIT); - renderer.set_camera(shadow_camera); - renderer.set_depth_test(&depth_test); + Renderer::Push push(renderer); + renderer.set_framebuffer(&fbo); + renderer.set_viewport(&light.region); + renderer.set_scissor(&light.region); + renderer.set_camera(v.camera); - renderer.render(shadow_caster); + renderer.render(*light.shadow_caster, (v.face>0 ? "noclear" : "")); + } } void ShadowMap::finish_frame() @@ -123,12 +215,13 @@ void ShadowMap::render(Renderer &renderer, Tag tag) const renderer.render(renderable, tag); } -void ShadowMap::set_debug_name(const std::string &name) +void ShadowMap::set_debug_name(const string &name) { #ifdef DEBUG fbo.set_debug_name(name+" [FBO]"); - shadow_camera.set_debug_name(name+".camera"); - depth_buf.set_debug_name(name+"/depth.tex2d"); + for(unsigned i=0; i