]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.cpp
Remove unfinished light properties panel from the blender exporter
[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 to_texcoord = Matrix().translate(Vector3(0.5f, 0.5f, 0.5f-depth_bias/l.region.width)).scale(0.5f);
157                 Matrix shadow_matrix = to_texcoord*l.shadow_camera.get_projection_matrix()*l.shadow_camera.get_view_matrix();
158
159                 shdata.uniform(format("shadows[%d].shd_world_matrix", l.index), shadow_matrix);
160         }
161
162         for(ShadowedLight &l: lights)
163         {
164                 Renderer::Push push(renderer);
165                 renderer.set_framebuffer(&fbo);
166                 renderer.set_viewport(&l.region);
167                 renderer.set_scissor(&l.region);
168                 renderer.set_camera(l.shadow_camera);
169
170                 renderer.render(shadow_caster);
171         }
172 }
173
174 void ShadowMap::finish_frame()
175 {
176         if(rendered)
177         {
178                 rendered = false;
179                 renderable.finish_frame();
180         }
181 }
182
183 void ShadowMap::render(Renderer &renderer, Tag tag) const
184 {
185         if(!enabled_passes.count(tag))
186                 return renderer.render(renderable, tag);
187
188         Renderer::Push _push_rend(renderer);
189
190         renderer.set_texture("shadow_map", &depth_buf, &sampler);
191         renderer.add_shader_data(shdata);
192         renderer.render(renderable, tag);
193 }
194
195 void ShadowMap::set_debug_name(const string &name)
196 {
197 #ifdef DEBUG
198         fbo.set_debug_name(name+" [FBO]");
199         for(unsigned i=0; i<lights.size(); ++i)
200                 lights[i].shadow_camera.set_debug_name(format("%s/light%d.camera", name, i));
201         depth_buf.set_debug_name(name+"/depth.tex2d");
202         shdata.set_debug_name(name+" [UBO]");
203 #else
204         (void)name;
205 #endif
206 }
207
208 } // namespace GL
209 } // namespace Msp