]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.cpp
Refactor resolution of declarations for MemberAccess
[libs/gl.git] / source / effects / shadowmap.cpp
1 #include <cmath>
2 #include <cstdlib>
3 #include "camera.h"
4 #include "light.h"
5 #include "renderer.h"
6 #include "resources.h"
7 #include "scene.h"
8 #include "shadowmap.h"
9 #include "tests.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l):
17         Effect(r),
18         size(s),
19         light(l),
20         sampler(resources.get<Sampler>("_linear_clamp_shadow.samp")),
21         radius(1),
22         depth_bias(4),
23         rendered(false)
24 {
25         depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
26         fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
27         fbo.require_complete();
28
29         set_darkness(0.7);
30 }
31
32 void ShadowMap::set_target(const Vector3 &t, float r)
33 {
34         target = t;
35         radius = r;
36 }
37
38 void ShadowMap::set_darkness(float d)
39 {
40         if(d<0.0f || d>1.0f)
41                 throw invalid_argument("ShadowMap::set_darkness");
42
43         shdata.uniform("shadow_darkness", d);
44 }
45
46 void ShadowMap::set_depth_bias(float b)
47 {
48         if(b<0.0f)
49                 throw invalid_argument("ShadowMap::set_depth_bias");
50
51         depth_bias = b;
52 }
53
54 void ShadowMap::setup_frame(Renderer &renderer)
55 {
56         if(rendered)
57                 return;
58
59         rendered = true;
60         renderable.setup_frame(renderer);
61
62         Camera camera;
63         camera.set_object_matrix(*light.get_matrix());
64         camera.set_position(target);
65         // TODO support point and spot lights with a frustum projection.
66         // Omnidirectional lights also need a cube shadow map.
67         camera.set_orthographic(radius*2, radius*2);
68         camera.set_depth_clip(-radius, radius);
69
70         shadow_matrix = camera.get_object_matrix();
71         shadow_matrix.scale(radius*2, radius*2, -radius*2);
72         shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
73         shadow_matrix.invert();
74
75         BindRestore bind_fbo(fbo);
76         Bind bind_depth(DepthTest::lequal());
77         fbo.clear(DEPTH_BUFFER_BIT);
78
79         Renderer::Push push(renderer);
80         renderer.set_camera(camera);
81
82         renderer.render(renderable, "shadow");
83 }
84
85 void ShadowMap::finish_frame()
86 {
87         renderable.finish_frame();
88         rendered = false;
89 }
90
91 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
92 {
93         if(!enabled_passes.count(tag))
94                 return renderer.render(renderable, tag);
95
96         Renderer::Push _push_rend(renderer);
97
98         unsigned unit = renderer.allocate_effect_texunit();
99         int iunit = unit;
100         shdata.uniform("shadow_map", iunit);
101
102         Bind _bind_sampler(sampler, unit);
103         Bind _bind_depth(depth_buf, unit);
104
105         if(const Camera *camera = renderer.get_camera())
106                 /* Multiply by camera's object matrix to form a matrix that transforms
107                 from eye space to shadow space. */
108                 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
109         else
110                 shdata.uniform("shd_eye_matrix", shadow_matrix);
111
112         renderer.add_shader_data(shdata);
113         renderer.render(renderable, tag);
114 }
115
116 } // namespace GL
117 } // namespace Msp