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