]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.cpp
Refactor ShadowMap to allow different types of lights
[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+".type", 0);
30                 shdata.uniform(base+".darkness", 1.0f);
31                 shdata.uniform(base+".matrix_index", 0);
32                 shdata.uniform(base+".region", Vector4(0.0f, 0.0f, 1.0f, 1.0f));
33         }
34
35         Matrix dummy_matrix;
36         shdata.uniform_array("shd_world_matrix", 1, &dummy_matrix);
37 }
38
39 ShadowMap::ShadowMap(unsigned s, Renderable &r, const DirectionalLight &l, Renderable &c):
40         ShadowMap(s, s, r, 0)
41 {
42         add_light(l, s, c);
43 }
44
45 ShadowMap::ShadowMap(unsigned w, unsigned h, Renderable &r, const Lighting &l):
46         ShadowMap(w, h, r, &l)
47 { }
48
49 void ShadowMap::add_light(const DirectionalLight &light, unsigned s, Renderable &c)
50 {
51         add_light(light, s, DIRECTIONAL, c);
52 }
53
54 void ShadowMap::add_light(const Light &light, unsigned s, ShadowType type, Renderable &c)
55 {
56         if(!lighting && !lights.empty())
57                 throw invalid_operation("ShadowMap::add_light");
58
59         int index = (lighting ? lighting->find_light_index(light) : 0);
60         if(index<0)
61                 throw invalid_argument("ShadowMap::add_light");
62
63         Rect region(0, 0, s, s);
64         while(1)
65         {
66                 int next_bottom = height;
67                 int next_left = region.left;
68
69                 int top = region.bottom+region.height;
70                 int right = region.left+region.width;
71                 for(const ShadowedLight &l: lights)
72                 {
73                         int l_top = l.region.bottom+l.region.height;
74                         int l_right = l.region.left+l.region.width;
75                         if(l_top>region.bottom)
76                                 next_bottom = min(next_bottom, l_top);
77
78                         if(top>l.region.bottom && region.bottom<l_top && right>l.region.left && region.left<l_right)
79                                 next_left = max(next_left, l_right);
80                 }
81
82                 if(next_left==region.left)
83                         break;
84                 else if(next_left+region.width>width)
85                 {
86                         if(next_bottom+region.height>height)
87                                 throw invalid_operation("ShadowMap::add_light");
88                         region.bottom = next_bottom;
89                         region.left = 0;
90                 }
91                 else
92                         region.left = next_left;
93         }
94
95         lights.emplace_back();
96         ShadowedLight &sl = lights.back();
97         sl.light = &light;
98         sl.index = index;
99         sl.region = region;
100         sl.type = type;
101         sl.view_index = views.size();
102         sl.shadow_caster = &c;
103
104         views.emplace_back();
105         ShadowView &view = views[sl.view_index];
106         view.light_index = lights.size()-1;
107         view.face = 0;
108
109         string base = format("shadows[%d]", index);
110         shdata.uniform(base+".type", static_cast<int>(type));
111         shdata.uniform(base+".darkness", darkness);
112         shdata.uniform(base+".matrix_index", static_cast<int>(sl.view_index));
113
114         float xf = static_cast<float>(region.left)/width;
115         float yf = static_cast<float>(region.bottom)/height;
116         float wf = static_cast<float>(region.width)/width;
117         float hf = static_cast<float>(region.height)/height;
118         shdata.uniform(base+".region", Vector4(xf, yf, wf, hf));
119
120 #ifdef DEBUG
121         if(!debug_name.empty())
122         {
123                 for(unsigned i=sl.view_index; i<views.size(); ++i)
124                         views[i].camera.set_debug_name(format("%s/view%d.camera", debug_name, i));
125         }
126 #endif
127 }
128
129 void ShadowMap::set_target(const Vector3 &t, float r)
130 {
131         target = t;
132         radius = r;
133 }
134
135 void ShadowMap::set_darkness(float d)
136 {
137         if(d<0.0f || d>1.0f)
138                 throw invalid_argument("ShadowMap::set_darkness");
139
140         darkness = d;
141         for(const ShadowedLight &l: lights)
142                 shdata.uniform(format("shadows[%d].darkness", l.index), d);
143 }
144
145 void ShadowMap::set_depth_bias(float b)
146 {
147         if(b<0.0f)
148                 throw invalid_argument("ShadowMap::set_depth_bias");
149
150         depth_bias = b;
151 }
152
153 void ShadowMap::setup_frame(Renderer &renderer)
154 {
155         if(rendered)
156                 return;
157
158         rendered = true;
159         renderable.setup_frame(renderer);
160         for(const ShadowedLight &l: lights)
161                 l.shadow_caster->setup_frame(renderer);
162
163         vector<Matrix> shadow_matrices;
164         shadow_matrices.reserve(views.size());
165         for(ShadowView &v: views)
166         {
167                 const ShadowedLight &light = lights[v.light_index];
168
169                 if(light.type==DIRECTIONAL)
170                 {
171                         v.camera.set_object_matrix(*light.light->get_matrix());
172                         v.camera.set_position(target);
173                         v.camera.set_orthographic(radius*2, radius*2);
174                         v.camera.set_depth_clip(-radius, radius);
175                 }
176
177                 Matrix to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/light.region.width)).scale(0.5f);
178                 shadow_matrices.push_back(to_texcoord*v.camera.get_projection_matrix()*v.camera.get_view_matrix());
179         }
180
181         shdata.uniform_array("shd_world_matrix", shadow_matrices.size(), shadow_matrices.data());
182
183         for(const ShadowView &v: views)
184         {
185                 const ShadowedLight &light = lights[v.light_index];
186
187                 Renderer::Push push(renderer);
188                 renderer.set_framebuffer(&fbo);
189                 renderer.set_viewport(&light.region);
190                 renderer.set_scissor(&light.region);
191                 renderer.set_camera(v.camera);
192
193                 renderer.render(*light.shadow_caster, (v.face>0 ? "noclear" : ""));
194         }
195 }
196
197 void ShadowMap::finish_frame()
198 {
199         if(rendered)
200         {
201                 rendered = false;
202                 renderable.finish_frame();
203         }
204 }
205
206 void ShadowMap::render(Renderer &renderer, Tag tag) const
207 {
208         if(!enabled_passes.count(tag))
209                 return renderer.render(renderable, tag);
210
211         Renderer::Push _push_rend(renderer);
212
213         renderer.set_texture("shadow_map", &depth_buf, &sampler);
214         renderer.add_shader_data(shdata);
215         renderer.render(renderable, tag);
216 }
217
218 void ShadowMap::set_debug_name(const string &name)
219 {
220 #ifdef DEBUG
221         fbo.set_debug_name(name+" [FBO]");
222         for(unsigned i=0; i<views.size(); ++i)
223                 views[i].camera.set_debug_name(format("%s/view%d.camera", name, i));
224         depth_buf.set_debug_name(name+"/depth.tex");
225         shdata.set_debug_name(name+" [UBO]");
226 #else
227         (void)name;
228 #endif
229 }
230
231 } // namespace GL
232 } // namespace Msp