X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Feffects%2Fshadowmap.cpp;h=27b695443341c860337242d5c2908d0479b502d7;hp=4cc0475d3e6ad79fae9d207755161ffba3826503;hb=083a8227715fa32c841fc2b8126e4ab8d2840ba0;hpb=bace0b24414abc7b3ba46df3a9fd7408d7479a5e diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index 4cc0475d..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" @@ -30,6 +31,7 @@ ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l): 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)); + shdata.uniform(base+".bias", 0.0f, 1.0f); } Matrix dummy_matrix; @@ -51,6 +53,11 @@ void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable 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()) @@ -92,6 +99,8 @@ void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Rende region.left = next_left; } + unsigned n_views = (type==TETRAHEDRON ? 4 : 1); + lights.emplace_back(); ShadowedLight &sl = lights.back(); sl.light = &light; @@ -101,10 +110,28 @@ void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Rende 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; + 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+".type", static_cast(type)); @@ -160,6 +187,19 @@ void ShadowMap::setup_frame(Renderer &renderer) for(const ShadowedLight &l: lights) l.shadow_caster->setup_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, 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); + } + } + vector shadow_matrices; shadow_matrices.reserve(views.size()); for(ShadowView &v: views) @@ -173,8 +213,13 @@ void ShadowMap::setup_frame(Renderer &renderer) 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-depth_bias/light.region.width)).scale(0.5f); + 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()); } @@ -205,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); @@ -228,5 +273,89 @@ void ShadowMap::set_debug_name(const string &name) #endif } + +ShadowMap *ShadowMap::Template::create(const map &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