]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.cpp
74dad5e78b74fb6d32505008ca2ecf15d262600b
[libs/gl.git] / source / effects / shadowmap.cpp
1 #include <msp/strings/format.h>
2 #include "directionallight.h"
3 #include "error.h"
4 #include "lighting.h"
5 #include "renderer.h"
6 #include "resources.h"
7 #include "shadowmap.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting *l):
15         Effect(r),
16         width(w),
17         height(h),
18         lighting(l),
19         sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
20 {
21         depth_buf.storage(DEPTH_COMPONENT32F, width, height, 1);
22         fbo.set_format((DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
23         fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
24
25         set_darkness(1.0f);
26         for(unsigned i=0; i<4; ++i)
27         {
28                 string base = format("shadows[%d]", i);
29                 shdata.uniform(base+".enabled", 0);
30                 shdata.uniform(base+".darkness", 1.0f);
31                 shdata.uniform(base+".shd_world_matrix", Matrix());
32                 shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
33         }
34 }
35
36 ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
37         ShadowMap(s, s, r, 0)
38 {
39         add_light(l, s, c);
40 }
41
42 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l):
43         ShadowMap(w, h, r, &l)
44 { }
45
46 void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c)
47 {
48         if(!lighting && !lights.empty())
49                 throw invalid_operation("ShadowMap::add_light");
50
51         int index = (lighting ? lighting->find_light_index(light) : 0);
52         if(index<0)
53                 throw invalid_argument("ShadowMap::add_light");
54
55         Rect region(0, 0, s, s);
56         while(1)
57         {
58                 int next_bottom = height;
59                 int next_left = region.left;
60
61                 int top = region.bottom+region.height;
62                 int right = region.left+region.width;
63                 for(const ShadowedLight &l: lights)
64                 {
65                         int l_top = l.region.bottom+l.region.height;
66                         int l_right = l.region.left+l.region.width;
67                         if(l_top>region.bottom)
68                                 next_bottom = min(next_bottom, l_top);
69
70                         if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
71                                 next_left = max(next_left, l_right);
72                 }
73
74                 if(next_left==region.left)
75                         break;
76                 else if(next_left+region.width>width)
77                 {
78                         if(next_bottom+region.height>height)
79                                 throw invalid_operation("ShadowMap::add_light");
80                         region.bottom = next_bottom;
81                         region.left = 0;
82                 }
83                 else
84                         region.left = next_left;
85         }
86
87         lights.emplace_back();
88         ShadowedLight &sl = lights.back();
89         sl.light = &light;
90         sl.index = index;
91         sl.region = region;
92         sl.shadow_caster = &c;
93
94         string base = format("shadows[%d]", index);
95         shdata.uniform(base+".enabled", 1);
96         shdata.uniform(base+".darkness", darkness);
97
98         float xf = static_cast<float>(region.left)/width;
99         float yf = static_cast<float>(region.bottom)/height;
100         float wf = static_cast<float>(region.width)/width;
101         float hf = static_cast<float>(region.height)/height;
102         shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
103
104 #ifdef DEBUG
105         if(!debug_name.empty())
106                 sl.shadow_camera.set_debug_name(format("%s/light%d.camera", debug_name, lights.size()-1));
107 #endif
108 }
109
110 void ShadowMap::set_target(const Vector3 &t, float r)
111 {
112         target = t;
113         radius = r;
114 }
115
116 void ShadowMap::set_darkness(float d)
117 {
118         if(d<0.0f || d>1.0f)
119                 throw invalid_argument("ShadowMap::set_darkness");
120
121         darkness = d;
122         for(const ShadowedLight &l: lights)
123                 shdata.uniform(format("shadows[%d].darkness", l.index), d);
124 }
125
126 void ShadowMap::set_depth_bias(float b)
127 {
128         if(b<0.0f)
129                 throw invalid_argument("ShadowMap::set_depth_bias");
130
131         depth_bias = b;
132 }
133
134 void ShadowMap::setup_frame(Renderer &renderer)
135 {
136         if(rendered)
137                 return;
138
139         rendered = true;
140         renderable.setup_frame(renderer);
141         for(const ShadowedLight &l: lights)
142                 l.shadow_caster->setup_frame(renderer);
143
144         for(ShadowedLight &l: lights)
145         {
146                 l.shadow_camera.set_object_matrix(*l.light->get_matrix());
147                 l.shadow_camera.set_position(target);
148                 // TODO support point and spot lights with a frustum projection.
149                 // Omnidirectional lights also need a cube shadow map.
150                 l.shadow_camera.set_orthographic(radius*2, radius*2);
151                 l.shadow_camera.set_depth_clip(-radius, radius);
152
153                 Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/l.region.width)).scale(0.5f);
154                 Matrix shadow_matrix = to_texcoord*l.shadow_camera.get_projection_matrix()*l.shadow_camera.get_view_matrix();
155
156                 shdata.uniform(format("shadows[%d].shd_world_matrix", l.index), shadow_matrix);
157         }
158
159         for(ShadowedLight &l: lights)
160         {
161                 Renderer::Push push(renderer);
162                 renderer.set_framebuffer(&fbo);
163                 renderer.set_viewport(&l.region);
164                 renderer.set_scissor(&l.region);
165                 renderer.set_camera(l.shadow_camera);
166
167                 renderer.render(*light.shadow_caster);
168         }
169 }
170
171 void ShadowMap::finish_frame()
172 {
173         if(rendered)
174         {
175                 rendered = false;
176                 renderable.finish_frame();
177         }
178 }
179
180 void ShadowMap::render(Renderer &renderer, Tag tag) const
181 {
182         if(!enabled_passes.count(tag))
183                 return renderer.render(renderable, tag);
184
185         Renderer::Push _push_rend(renderer);
186
187         renderer.set_texture("shadow_map", &depth_buf, &sampler);
188         renderer.add_shader_data(shdata);
189         renderer.render(renderable, tag);
190 }
191
192 void ShadowMap::set_debug_name(const string &name)
193 {
194 #ifdef DEBUG
195         fbo.set_debug_name(name+" [FBO]");
196         for(unsigned i=0; i<lights.size(); ++i)
197                 lights[i].shadow_camera.set_debug_name(format("%s/light%d.camera", name, i));
198         depth_buf.set_debug_name(name+"/depth.tex");
199         shdata.set_debug_name(name+" [UBO]");
200 #else
201         (void)name;
202 #endif
203 }
204
205 } // namespace GL
206 } // namespace Msp