X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Feffects%2Fshadowmap.cpp;h=465243973f3c87a702b23727c5bdea3a5a3266d5;hb=b733c793381e78637f52c7f77cc4f69e914918e1;hp=49dfa36de0bb636d101900dc78e47ca42f03f723;hpb=a2b0d155023ca23afe7848ae5d17e0f7bc328525;p=libs%2Fgl.git diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index 49dfa36d..46524397 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -1,6 +1,6 @@ #include +#include "directionallight.h" #include "error.h" -#include "light.h" #include "lighting.h" #include "renderer.h" #include "resources.h" @@ -11,17 +11,12 @@ using namespace std; namespace Msp { namespace GL { -ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l, Renderable &c): +ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l): Effect(r), width(w), height(h), lighting(l), - shadow_caster(c), - sampler(Resources::get_global().get("_linear_clamp_shadow.samp")), - radius(1), - depth_bias(4), - darkness(1.0f), - rendered(false) + sampler(Resources::get_global().get("_linear_clamp_shadow.samp")) { depth_buf.storage(DEPTH_COMPONENT32F, width, height, 1); fbo.set_format((DEPTH_ATTACHMENT,DEPTH_COMPONENT32F)); @@ -31,24 +26,33 @@ ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l, R for(unsigned i=0; i<4; ++i) { string base = format("shadows[%d]", i); - shdata.uniform(base+".enabled", 0); + shdata.uniform(base+".type", 0); shdata.uniform(base+".darkness", 1.0f); - shdata.uniform(base+".shd_world_matrix", Matrix()); + shdata.uniform(base+".matrix_index", 0); shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f)); + shdata.uniform(base+".bias", 0.0f); } + + Matrix dummy_matrix; + shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix); } -ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l, Renderable &c): - ShadowMap(s, s, r, 0, c) +ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c): + ShadowMap(s, s, r, 0) { - add_light(l, s); + add_light(l, s, c); } -ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l, Renderable &c): - ShadowMap(w, h, r, &l, c) +ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l): + ShadowMap(w, h, r, &l) { } -void ShadowMap::add_light(const Light &light, unsigned s) +void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c) +{ + add_light(light, s, DIRECTIONAL, c); +} + +void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c) { if(!lighting && !lights.empty()) throw invalid_operation("ShadowMap::add_light"); @@ -94,10 +98,19 @@ void ShadowMap::add_light(const Light &light, unsigned s) 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+".enabled", 1); + 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; @@ -107,7 +120,10 @@ void ShadowMap::add_light(const Light &light, unsigned s) #ifdef DEBUG if(!debug_name.empty()) - sl.shadow_camera.set_debug_name(format("%s/light%d.camera", debug_name, lights.size()-1)); + { + for(unsigned i=sl.view_index; isetup_frame(renderer); + + for(const ShadowedLight &l: lights) + { + string base = format("shadows[%d]", l.index); + if(l.type==DIRECTIONAL) + shdata.uniform(base+".bias", depth_bias/l.region.width); + } - for(ShadowedLight &l: lights) + vector shadow_matrices; + shadow_matrices.reserve(views.size()); + for(ShadowView &v: views) { - 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); - - Matrix shadow_matrix = l.shadow_camera.get_object_matrix(); - shadow_matrix.scale(radius*2, radius*2, -radius*2); - shadow_matrix.translate(-0.5, -0.5, depth_bias/l.region.width-0.5); - shadow_matrix.invert(); - - shdata.uniform(format("shadows[%d].shd_world_matrix", l.index), shadow_matrix); + 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)).scale(0.5f); + shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix()); } - for(ShadowedLight &l: lights) + shdata.uniform_array("shd_world_matrix", shadow_matrices.size(), shadow_matrices.data()); + + for(const ShadowView &v: views) { + const ShadowedLight &light = lights[v.light_index]; + 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.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" : "")); } } @@ -198,9 +227,9 @@ void ShadowMap::set_debug_name(const string &name) { #ifdef DEBUG fbo.set_debug_name(name+" [FBO]"); - for(unsigned i=0; i