X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Feffects%2Fshadowmap.cpp;h=27b695443341c860337242d5c2908d0479b502d7;hp=1b5f531b913c2d339f8e1aee3830775d576aed58;hb=083a8227715fa32c841fc2b8126e4ab8d2840ba0;hpb=adc26a2e141a2853b6c5025130c46a46cece4b84 diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index 1b5f531b..27b69544 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -2,6 +2,7 @@ #include "directionallight.h" #include "error.h" #include "lighting.h" +#include "pointlight.h" #include "renderer.h" #include "resources.h" #include "shadowmap.h" @@ -11,17 +12,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 +27,38 @@ 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, 1.0f); } + + Matrix dummy_matrix; + shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix); } ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c): - ShadowMap(s, s, r, 0, 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 DirectionalLight &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 PointLight &light, unsigned s, Renderable &c) +{ + add_light(light, s, TETRAHEDRON, c); +} + +void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c) { if(!lighting && !lights.empty()) throw invalid_operation("ShadowMap::add_light"); @@ -89,15 +99,44 @@ void ShadowMap::add_light(const DirectionalLight &light, unsigned s) region.left = next_left; } + unsigned n_views = (type==TETRAHEDRON ? 4 : 1); + 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.resize(views.size()+n_views); + for(unsigned i=0; i0) + view.face_matrix = Matrix().rotate(Geometry::Angle::from_turns((i-0.5f)/3.0f), Vector3(0, 0, 1)) + .rotate(Geometry::Angle::straight()-Geometry::acos(1.0f/3.0f), Vector3(1, 0, 0)); + + float triangle_h = sqrt(0.75f)*2.0f; + float bottom = 0.5f/sqrt(0.75f); + float distance = (sqrt(2.0f/3.0f)-sqrt(3.0f/8.0f))*2.0f; + view.camera.set_field_of_view(Geometry::atan(triangle_h/distance)*2.0f); + view.camera.set_aspect_ratio(1.0f/triangle_h); + view.camera.set_frustum_axis(0.0f, 1.0f-bottom/triangle_h); + view.camera.set_frustum_rotation(-Geometry::Angle::from_turns(i/4.0f)); + } + } 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 +146,10 @@ void ShadowMap::add_light(const DirectionalLight &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(ShadowedLight &l: lights) + for(const 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); - - 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); + string base = format("shadows[%d]", l.index); + if(l.type==DIRECTIONAL) + shdata.uniform(base+".bias", depth_bias/l.region.width, 0.0f); + else if(l.type==TETRAHEDRON) + { + float distance = (sqrt(2.0f/3.0f)-sqrt(3.0f/8.0f))*2.0f; + float bias = depth_bias*2.0f/(distance*l.region.width); + shdata.uniform(base+".bias", -1001.0f/999.0f, 1.0f-bias); + } } - for(ShadowedLight &l: lights) + 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); + } + else if(light.type==TETRAHEDRON) + { + v.camera.set_object_matrix((*light.light->get_matrix())*v.face_matrix); + v.camera.set_depth_clip(radius/1000.0f, 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()); + } + + 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" : "")); } } @@ -184,7 +250,7 @@ void ShadowMap::finish_frame() void ShadowMap::render(Renderer &renderer, Tag tag) const { - if(!enabled_passes.count(tag)) + if(!enabled_methods.count(tag)) return renderer.render(renderable, tag); Renderer::Push _push_rend(renderer); @@ -198,14 +264,98 @@ void ShadowMap::set_debug_name(const string &name) { #ifdef DEBUG fbo.set_debug_name(name+" [FBO]"); - for(unsigned i=0; i &renderables) const +{ + Renderable *content = get_item(renderables, content_name); + if(!content || !lighting) + throw invalid_operation("ShadowMap::Template::create"); + + RefPtr shadow_map = new ShadowMap(width, height, *content, *lighting); + shadow_map->set_target(target, radius); + shadow_map->set_depth_bias(depth_bias); + shadow_map->set_darkness(darkness); + + for(const ShadowedLight &l: lights) + { + Renderable *shadow_caster = get_item(renderables, l.shadow_caster_name); + if(!l.light || !shadow_caster) + throw invalid_operation("ShadowMap::Template::create"); + if(const DirectionalLight *dir_light = dynamic_cast(l.light)) + shadow_map->add_light(*dir_light, l.size, *shadow_caster); + else if(const PointLight *point_light = dynamic_cast(l.light)) + shadow_map->add_light(*point_light, l.size, *shadow_caster); + else + throw invalid_operation("ShadowMap::Template::create"); + } + + return shadow_map.release(); +} + + +DataFile::Loader::ActionMap ShadowMap::Template::Loader::shared_actions; + +ShadowMap::Template::Loader::Loader(Template &t, Collection &c): + DerivedObjectLoader(t, c) +{ + set_actions(shared_actions); +} + +void ShadowMap::Template::Loader::init_actions() +{ + Effect::Template::Loader::init_actions(); + add("darkness", &Template::darkness); + add("depth_bias", &Template::depth_bias); + add("light", &Loader::light); + add("lighting", &Template::lighting); + add("radius", &Template::radius); + add("size", &Loader::size_square); + add("size", &Template::width, &Template::height); + add("target", &Loader::target); +} + +void ShadowMap::Template::Loader::light(const string &name) +{ + ShadowedLight light; + light.light = &get_collection().get(name); + load_sub(light); + obj.lights.push_back(light); +} + +void ShadowMap::Template::Loader::size_square(unsigned s) +{ + obj.width = s; + obj.height = s; +} + +void ShadowMap::Template::Loader::target(float x, float y, float z) +{ + obj.target = Vector3(x, y, z); +} + + +DataFile::Loader::ActionMap ShadowMap::Template::ShadowedLight::Loader::shared_actions; + +ShadowMap::Template::ShadowedLight::Loader::Loader(ShadowedLight &l): + ObjectLoader(l) +{ + set_actions(shared_actions); +} + +void ShadowMap::Template::ShadowedLight::Loader::init_actions() +{ + add("size", &ShadowedLight::size); + add("shadow_caster", &ShadowedLight::shadow_caster_name); +} + } // namespace GL } // namespace Msp