]> git.tdb.fi Git - libs/gl.git/blob - source/shadowmap.cpp
Automatically determine texture units for effects
[libs/gl.git] / source / shadowmap.cpp
1 #include <cmath>
2 #include <cstdlib>
3 #include "camera.h"
4 #include "light.h"
5 #include "matrix.h"
6 #include "misc.h"
7 #include "renderer.h"
8 #include "scene.h"
9 #include "shadowmap.h"
10 #include "tests.h"
11 #include "texgen.h"
12 #include "texunit.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 ShadowMap::ShadowMap(unsigned s, const Renderable &r, const Light &l):
20         Effect(r),
21         size(s),
22         light(l),
23         radius(1),
24         depth_bias(4),
25         rendered(false)
26 {
27         depth_buf.set_min_filter(LINEAR);
28         depth_buf.set_compare_enabled(true);
29         depth_buf.set_compare_func(LEQUAL);
30         depth_buf.set_wrap(CLAMP_TO_EDGE);
31         depth_buf.storage(DEPTH_COMPONENT, size, size);
32         fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
33         fbo.require_complete();
34
35         set_darkness(0.7);
36 }
37
38 void ShadowMap::set_target(const Vector3 &t, float r)
39 {
40         target = t;
41         radius = r;
42 }
43
44 void ShadowMap::set_darkness(float d)
45 {
46         if(d<0.0f || d>1.0f)
47                 throw invalid_argument("ShadowMap::set_darkness");
48
49         shdata.uniform("shadow_darkness", d);
50 }
51
52 void ShadowMap::set_depth_bias(float b)
53 {
54         if(b<0.0f)
55                 throw invalid_argument("ShadowMap::set_depth_bias");
56
57         depth_bias = b;
58 }
59
60 void ShadowMap::setup_frame() const
61 {
62         if(rendered)
63                 return;
64
65         renderable.setup_frame();
66         rendered = true;
67
68         const Vector4 &lpos = light.get_position();
69         Vector3 back;
70         if(lpos.w)
71         {
72                 /* XXX Not really proper way to support positional lights, but good
73                 enough when the light source is far away */
74                 back = Vector3(lpos)-target;
75         }
76         else
77                 back = Vector3(lpos);
78         back.normalize();
79
80         Vector3 up;
81         if(abs(back.z)<0.99)
82                 up = Vector3(0, 0, 1);
83         else
84                 up = Vector3(0, 1, 0);
85
86         Vector3 right = normalize(cross(up, back));
87
88         Vector4 columns[4];
89         columns[0] = Vector4(right, 0.0f);
90         columns[1] = Vector4(normalize(cross(back, right)), 0.0f);
91         columns[2] = Vector4(back, 0.0f);
92         columns[3] = Vector4(target, 1.0f);
93         light_matrix = Matrix::from_columns(columns);
94         view_matrix = invert(light_matrix);
95
96         MatrixStack::Push push_mv(MatrixStack::modelview());
97         MatrixStack::Push push_proj(MatrixStack::projection());
98
99         MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius);
100         MatrixStack::modelview() = view_matrix;
101
102         shadow_matrix = light_matrix;
103         shadow_matrix.scale(radius*2, radius*2, -radius*2);
104         shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
105         shadow_matrix.invert();
106
107         Bind bind_fbo(fbo, true);
108         Bind bind_depth(DepthTest::lequal());
109         fbo.clear(DEPTH_BUFFER_BIT);
110         renderable.render("shadow");
111 }
112
113 void ShadowMap::finish_frame() const
114 {
115         renderable.finish_frame();
116         rendered = false;
117 }
118
119 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
120 {
121         if(!enabled_passes.count(tag))
122                 return renderer.render(renderable, tag);
123
124         Renderer::Push _push_rend(renderer);
125
126         unsigned unit = renderer.allocate_effect_texunit();
127         int iunit = unit;
128         shdata.uniform("shadow", iunit);
129         shdata.uniform("shadow_unit", iunit);
130
131         depth_buf.bind_to(unit);
132         TexGen tg_s, tg_t, tg_r;
133         tg_s.set_plane(Vector4(shadow_matrix(0, 0), shadow_matrix(0, 1), shadow_matrix(0, 2), shadow_matrix(0, 3)));
134         tg_t.set_plane(Vector4(shadow_matrix(1, 0), shadow_matrix(1, 1), shadow_matrix(1, 2), shadow_matrix(1, 3)));
135         tg_r.set_plane(Vector4(shadow_matrix(2, 0), shadow_matrix(2, 1), shadow_matrix(2, 2), shadow_matrix(2, 3)));
136         tg_s.bind_to(unit, SCOORD);
137         tg_t.bind_to(unit, TCOORD);
138         tg_r.bind_to(unit, RCOORD);
139
140         if(const Camera *camera = renderer.get_camera())
141                 /* Multiply by camera's object matrix to form a matrix that transforms
142                 from eye space to shadow space. */
143                 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
144         else
145                 shdata.uniform("shd_eye_matrix", shadow_matrix);
146
147         renderer.add_shader_data(shdata);
148         renderer.render(renderable, tag);
149
150         Texture::unbind_from(unit);
151         TexGen::unbind_from(unit, SCOORD);
152         TexGen::unbind_from(unit, TCOORD);
153         TexGen::unbind_from(unit, RCOORD);
154 }
155
156 } // namespace GL
157 } // namespace Msp