]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.cpp
Check the flat qualifier from the correct member
[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(unsigned s, Renderable &r, const Light &l, Renderable &c):
17         Effect(r),
18         light(l),
19         shadow_caster(c),
20         sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
21 {
22         init(s);
23 }
24
25 ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l):
26         Effect(r),
27         light(l),
28         shadow_caster(r),
29         sampler(Resources::get_global().get<Sampler>("_linear_clamp_shadow.samp"))
30 {
31         init(s);
32 }
33
34 void ShadowMap::init(unsigned s)
35 {
36         size = s;
37         radius = 1;
38         depth_bias = 4;
39         rendered = false;
40
41         depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
42         fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
43         fbo.require_complete();
44
45         set_darkness(1.0f);
46         shdata.uniform("shd_world_matrix", Matrix());
47 }
48
49 void ShadowMap::set_target(const Vector3 &t, float r)
50 {
51         target = t;
52         radius = r;
53 }
54
55 void ShadowMap::set_darkness(float d)
56 {
57         if(d<0.0f || d>1.0f)
58                 throw invalid_argument("ShadowMap::set_darkness");
59
60         shdata.uniform("shadow_darkness", d);
61 }
62
63 void ShadowMap::set_depth_bias(float b)
64 {
65         if(b<0.0f)
66                 throw invalid_argument("ShadowMap::set_depth_bias");
67
68         depth_bias = b;
69 }
70
71 void ShadowMap::setup_frame(Renderer &renderer)
72 {
73         if(rendered)
74                 return;
75
76         rendered = true;
77         renderable.setup_frame(renderer);
78         shadow_caster.setup_frame(renderer);
79
80         shadow_camera.set_object_matrix(*light.get_matrix());
81         shadow_camera.set_position(target);
82         // TODO support point and spot lights with a frustum projection.
83         // Omnidirectional lights also need a cube shadow map.
84         shadow_camera.set_orthographic(radius*2, radius*2);
85         shadow_camera.set_depth_clip(-radius, radius);
86
87         shadow_matrix = shadow_camera.get_object_matrix();
88         shadow_matrix.scale(radius*2, radius*2, -radius*2);
89         shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
90         shadow_matrix.invert();
91
92         shdata.uniform("shd_world_matrix", shadow_matrix);
93
94         BindRestore bind_fbo(fbo);
95         Bind bind_depth(DepthTest::lequal());
96         fbo.clear(DEPTH_BUFFER_BIT);
97
98         Renderer::Push push(renderer);
99         renderer.set_camera(shadow_camera);
100
101         renderer.render(shadow_caster);
102 }
103
104 void ShadowMap::finish_frame()
105 {
106         if(rendered)
107         {
108                 rendered = false;
109                 renderable.finish_frame();
110         }
111 }
112
113 void ShadowMap::render(Renderer &renderer, Tag tag) const
114 {
115         if(!enabled_passes.count(tag))
116                 return renderer.render(renderable, tag);
117
118         Renderer::Push _push_rend(renderer);
119
120         renderer.set_texture("shadow_map", &depth_buf, &sampler);
121         renderer.add_shader_data(shdata);
122         renderer.render(renderable, tag);
123 }
124
125 void ShadowMap::set_debug_name(const std::string &name)
126 {
127 #ifdef DEBUG
128         fbo.set_debug_name(name+" [FBO]");
129         shadow_camera.set_debug_name(name+".camera");
130         depth_buf.set_debug_name(name+"/depth.tex2d");
131         shdata.set_debug_name(name+" [UBO]");
132 #else
133         (void)name;
134 #endif
135 }
136
137 } // namespace GL
138 } // namespace Msp