]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor ShadowMap to allow different types of lights
authorMikko Rasa <tdb@tdb.fi>
Mon, 11 Oct 2021 12:57:50 +0000 (15:57 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 11 Oct 2021 13:14:31 +0000 (16:14 +0300)
Omnidirectional lights will need more than one view per light.

shaderlib/shadow.glsl
source/effects/shadowmap.cpp
source/effects/shadowmap.h

index 52cd7c13ab767a4cb07b24e11b4be7bb1c898df4..2fa1fd81686c832f3ce78463467500966c092a8e 100644 (file)
@@ -2,15 +2,16 @@ import msp_interface;
 
 struct ShadowParameters
 {
-       int enabled;
+       int type;
        float darkness;
-       mat4 shd_world_matrix;
+       int matrix_index;
        vec4 region;
 };
 
 uniform ShadowMap
 {
        ShadowParameters shadows[max_lights];
+       mat4 shd_world_matrix[max_lights];
 };
 
 uniform sampler2DShadow shadow_map;
@@ -22,10 +23,13 @@ virtual float get_shadow_factor(int index, vec4 world_pos)
 {
        if(use_shadow_map)
        {
-               if(shadows[index].enabled==0)
+               int type = shadows[index].type;
+               vec3 shadow_coord;
+               if(type==1)
+                       shadow_coord = (shd_world_matrix[shadows[index].matrix_index]*world_pos).xyz;
+               else
                        return 1.0;
 
-               vec3 shadow_coord = (shadows[index].shd_world_matrix*world_pos).xyz;
                if(shadow_coord.x<0 || shadow_coord.x>1 || shadow_coord.y<0 || shadow_coord.y>1)
                        return 1.0;
 
index 74dad5e78b74fb6d32505008ca2ecf15d262600b..4cc0475d3e6ad79fae9d207755161ffba3826503 100644 (file)
@@ -26,11 +26,14 @@ ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l):
        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));
        }
+
+       Matrix dummy_matrix;
+       shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix);
 }
 
 ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
@@ -44,6 +47,11 @@ ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l):
 { }
 
 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");
@@ -89,11 +97,19 @@ void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable
        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<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;
@@ -103,7 +119,10 @@ void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable
 
 #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,30 +160,37 @@ void ShadowMap::setup_frame(Renderer &renderer)
        for(const ShadowedLight &l: lights)
                l.shadow_caster->setup_frame(renderer);
 
-       for(ShadowedLight &l: lights)
+       vector<Matrix> 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);
+               const ShadowedLight &light = lights[v.light_index];
 
-               Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/l.region.width)).scale(0.5f);
-               Matrix shadow_matrix = to_texcoord*l.shadow_camera.get_projection_matrix()*l.shadow_camera.get_view_matrix();
+               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);
+               }
 
-               shdata.uniform(format("shadows[%d].shd_world_matrix", l.index), shadow_matrix);
+               Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/light.region.width)).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(*light.shadow_caster);
+               renderer.render(*light.shadow_caster, (v.face>0 ? "noclear" : ""));
        }
 }
 
@@ -193,8 +219,8 @@ 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));
+       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
index 6c4e1e501713d84aa393652b8eefb32912a3a0ec..cf2dec01bc69e90afda1255dd63918fd5b8d5ebc 100644 (file)
@@ -24,19 +24,34 @@ texture coordinate generation to determine whether each fragment is lit.
 class ShadowMap: public Effect
 {
 private:
+       enum ShadowType
+       {
+               NONE,
+               DIRECTIONAL
+       };
+
        struct ShadowedLight
        {
                const Light *light;
                unsigned index;
                Rect region;
-               Camera shadow_camera;
+               ShadowType type;
+               unsigned view_index;
                Renderable *shadow_caster;
        };
 
+       struct ShadowView
+       {
+               unsigned light_index;
+               unsigned face;
+               Camera camera;
+       };
+
        unsigned width;
        unsigned height;
        const Lighting *lighting;
        std::vector<ShadowedLight> lights;
+       std::vector<ShadowView> views;
        Framebuffer fbo;
        Texture2D depth_buf;
        const Sampler &sampler;
@@ -54,7 +69,10 @@ public:
        ShadowMap(unsigned, unsigned, Renderable &, const Lighting &);
 
        void add_light(const DirectionalLight &, unsigned, Renderable &);
+private:
+       void add_light(const Light &, unsigned, ShadowType, Renderable &);
 
+public:
        /** Sets the ShadowMap target point and radius.  The transformation matrix is
        computed so that a sphere with the specified parameters will be completely
        covered by the ShadowMap. */