]> git.tdb.fi Git - libs/gl.git/blobdiff - source/effects/shadowmap.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / effects / shadowmap.cpp
index 49dfa36de0bb636d101900dc78e47ca42f03f723..bbf602bc60f2ed483215074c0727eba0e8ea2634 100644 (file)
@@ -1,7 +1,8 @@
 #include <msp/strings/format.h>
+#include "directionallight.h"
 #include "error.h"
-#include "light.h"
 #include "lighting.h"
+#include "pointlight.h"
 #include "renderer.h"
 #include "resources.h"
 #include "shadowmap.h"
@@ -11,44 +12,53 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l, Renderable &c):
-       Effect(r),
+ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &c, const Lighting *l):
+       Effect(c),
        width(w),
        height(h),
        lighting(l),
-       shadow_caster(c),
-       sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp")),
-       radius(1),
-       depth_bias(4),
-       darkness(1.0f),
-       rendered(false)
+       sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
 {
        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)
+       for(unsigned i=0; i<6; ++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 Light &l, Renderable &c):
-       ShadowMap(s, s, r, 0, c)
+ShadowMap::ShadowMap(unsigned s, Renderable &c, const DirectionalLight &l, Renderable &sc):
+       ShadowMap(s, s, c, 0)
 {
-       add_light(l, s);
+       add_light(l, s, sc);
 }
 
-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 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 Light &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; i<n_views; ++i)
+       {
+               ShadowView &view = views[sl.view_index+i];
+               view.light_index = lights.size()-1;
+               view.face = i;
+
+               if(type==TETRAHEDRON)
+               {
+                       if(i>0)
+                               view.face_matrix = Matrix().rotate(Geometry::Angle<float>::from_turns((i-0.5f)/3.0f), Vector3(0, 0, 1))
+                                       .rotate(Geometry::Angle<float>::straight()-Geometry::acos<float>(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<float>(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<float>::from_turns(i/4.0f));
+               }
+       }
 
        string base = format("shadows[%d]", index);
-       shdata.uniform(base+".enabled", 1);
+       shdata.uniform(base+".type", static_cast<int>(type));
        shdata.uniform(base+".darkness", darkness);
+       shdata.uniform(base+".matrix_index", static_cast<int>(sl.view_index));
 
        float xf = static_cast<float>(region.left)/width;
        float yf = static_cast<float>(region.bottom)/height;
@@ -107,7 +146,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; i<views.size(); ++i)
+                       views[i].camera.set_debug_name(format("%s/view%d.camera", debug_name, i));
+       }
 #endif
 }
 
@@ -141,35 +183,59 @@ void ShadowMap::setup_frame(Renderer &renderer)
                return;
 
        rendered = true;
-       renderable.setup_frame(renderer);
-       shadow_caster.setup_frame(renderer);
+       content.setup_frame(renderer);
+       for(const ShadowedLight &l: lights)
+               l.shadow_caster->setup_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", views[l.view_index].camera.get_projection_matrix()(2, 2), 1.0f-bias);
+               }
        }
 
-       for(ShadowedLight &l: lights)
+       vector<Matrix> 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.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());
+       }
+
+       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);
+               light.shadow_caster->render(renderer, (v.face>0 ? "noclear" : ""));
        }
 }
 
@@ -178,34 +244,120 @@ 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)
 {
 #ifdef DEBUG
        fbo.set_debug_name(name+" [FBO]");
-       for(unsigned i=0; i<lights.size(); ++i)
-               lights[i].shadow_camera.set_debug_name(format("%s/light%d.camera", name, i));
-       depth_buf.set_debug_name(name+"/depth.tex2d");
+       for(unsigned i=0; i<views.size(); ++i)
+               views[i].camera.set_debug_name(format("%s/view%d.camera", name, i));
+       depth_buf.set_debug_name(name+"/depth.tex");
        shdata.set_debug_name(name+" [UBO]");
 #else
        (void)name;
 #endif
 }
 
+
+ShadowMap *ShadowMap::Template::create(const map<string, Renderable *> &renderables) const
+{
+       Renderable *content = get_item(renderables, content_name);
+       if(!content || !lighting)
+               throw invalid_operation("ShadowMap::Template::create");
+
+       RefPtr<ShadowMap> 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<const DirectionalLight *>(l.light))
+                       shadow_map->add_light(*dir_light, l.size, *shadow_caster);
+               else if(const PointLight *point_light = dynamic_cast<const PointLight *>(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<Template, Effect::Template::Loader>(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<Light>(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<ShadowedLight>(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