X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Feffects%2Fshadowmap.cpp;h=bbf602bc60f2ed483215074c0727eba0e8ea2634;hp=465243973f3c87a702b23727c5bdea3a5a3266d5;hb=HEAD;hpb=b733c793381e78637f52c7f77cc4f69e914918e1 diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index 46524397..bbf602bc 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,8 +12,8 @@ using namespace std; namespace Msp { namespace GL { -ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l): - Effect(r), +ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &c, const Lighting *l): + Effect(c), width(w), height(h), lighting(l), @@ -23,24 +24,24 @@ ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l): fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0); set_darkness(1.0f); - for(unsigned i=0; i<4; ++i) + for(unsigned i=0; i<6; ++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)); - shdata.uniform(base+".bias", 0.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) +ShadowMap::ShadowMap(unsigned s, Renderable &c, const DirectionalLight &l, Renderable &sc): + ShadowMap(s, s, c, 0) { - add_light(l, s, c); + add_light(l, s, sc); } ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l): @@ -52,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()) @@ -93,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; @@ -102,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)); @@ -157,7 +183,7 @@ void ShadowMap::setup_frame(Renderer &renderer) return; rendered = true; - renderable.setup_frame(renderer); + content.setup_frame(renderer); for(const ShadowedLight &l: lights) l.shadow_caster->setup_frame(renderer); @@ -165,7 +191,13 @@ void ShadowMap::setup_frame(Renderer &renderer) { string base = format("shadows[%d]", l.index); if(l.type==DIRECTIONAL) - shdata.uniform(base+".bias", depth_bias/l.region.width); + 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", views[l.view_index].camera.get_projection_matrix()(2, 2), 1.0f-bias); + } } vector shadow_matrices; @@ -181,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)).scale(0.5f); + Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.0f)).scale(Vector3(0.5f, 0.5f, 1.0f)); shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix()); } @@ -198,7 +235,7 @@ void ShadowMap::setup_frame(Renderer &renderer) renderer.set_scissor(&light.region); renderer.set_camera(v.camera); - renderer.render(*light.shadow_caster, (v.face>0 ? "noclear" : "")); + light.shadow_caster->render(renderer, (v.face>0 ? "noclear" : "")); } } @@ -207,20 +244,20 @@ void ShadowMap::finish_frame() if(rendered) { rendered = false; - renderable.finish_frame(); + content.finish_frame(); } } void ShadowMap::render(Renderer &renderer, Tag tag) const { - if(!enabled_passes.count(tag)) - return renderer.render(renderable, tag); + if(!is_enabled_for_method(tag)) + return content.render(renderer, tag); Renderer::Push _push_rend(renderer); renderer.set_texture("shadow_map", &depth_buf, &sampler); renderer.add_shader_data(shdata); - renderer.render(renderable, tag); + content.render(renderer, tag); } void ShadowMap::set_debug_name(const string &name) @@ -236,5 +273,91 @@ 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"); + } + + create_base(*shadow_map); + + 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