]> git.tdb.fi Git - libs/gl.git/blob - source/shadowmap.cpp
Add shd_eye_matrix to ShadowMap's shdata
[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         set_texture_unit(3);
37 }
38
39 void ShadowMap::set_target(const Vector3 &t, float r)
40 {
41         target = t;
42         radius = r;
43 }
44
45 void ShadowMap::set_darkness(float d)
46 {
47         if(d<0.0f || d>1.0f)
48                 throw invalid_argument("ShadowMap::set_darkness");
49
50         shdata.uniform("shadow_darkness", d);
51 }
52
53 void ShadowMap::set_depth_bias(float b)
54 {
55         if(b<0.0f)
56                 throw invalid_argument("ShadowMap::set_depth_bias");
57
58         depth_bias = b;
59 }
60
61 void ShadowMap::set_texture_unit(unsigned u)
62 {
63         unit = u;
64
65         int i = unit;
66         shdata.uniform("shadow", i);
67         shdata.uniform("shadow_unit", i);
68 }
69
70 void ShadowMap::setup_frame() const
71 {
72         if(rendered)
73                 return;
74
75         renderable.setup_frame();
76         rendered = true;
77
78         const Vector4 &lpos = light.get_position();
79         Vector3 back;
80         if(lpos.w)
81         {
82                 /* XXX Not really proper way to support positional lights, but good
83                 enough when the light source is far away */
84                 back = Vector3(lpos)-target;
85         }
86         else
87                 back = Vector3(lpos);
88         back.normalize();
89
90         Vector3 up;
91         if(abs(back.z)<0.99)
92                 up = Vector3(0, 0, 1);
93         else
94                 up = Vector3(0, 1, 0);
95
96         Vector3 right = normalize(cross(up, back));
97
98         Vector4 columns[4];
99         columns[0] = Vector4(right, 0.0f);
100         columns[1] = Vector4(normalize(cross(back, right)), 0.0f);
101         columns[2] = Vector4(back, 0.0f);
102         columns[3] = Vector4(target, 1.0f);
103         light_matrix = Matrix::from_columns(columns);
104         view_matrix = invert(light_matrix);
105
106         MatrixStack::Push push_mv(MatrixStack::modelview());
107         MatrixStack::Push push_proj(MatrixStack::projection());
108
109         MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius);
110         MatrixStack::modelview() = view_matrix;
111
112         shadow_matrix = light_matrix;
113         shadow_matrix.scale(radius*2, radius*2, -radius*2);
114         shadow_matrix.translate(-0.5, -0.5, depth_bias/size-0.5);
115         shadow_matrix.invert();
116
117         Bind bind_fbo(fbo, true);
118         Bind bind_depth(DepthTest::lequal());
119         fbo.clear(DEPTH_BUFFER_BIT);
120         renderable.render("shadow");
121 }
122
123 void ShadowMap::finish_frame() const
124 {
125         renderable.finish_frame();
126         rendered = false;
127 }
128
129 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
130 {
131         if(!enabled_passes.count(tag))
132                 return renderer.render(renderable, tag);
133
134         depth_buf.bind_to(unit);
135         TexGen tg_s, tg_t, tg_r;
136         tg_s.set_plane(Vector4(shadow_matrix(0, 0), shadow_matrix(0, 1), shadow_matrix(0, 2), shadow_matrix(0, 3)));
137         tg_t.set_plane(Vector4(shadow_matrix(1, 0), shadow_matrix(1, 1), shadow_matrix(1, 2), shadow_matrix(1, 3)));
138         tg_r.set_plane(Vector4(shadow_matrix(2, 0), shadow_matrix(2, 1), shadow_matrix(2, 2), shadow_matrix(2, 3)));
139         tg_s.bind_to(unit, SCOORD);
140         tg_t.bind_to(unit, TCOORD);
141         tg_r.bind_to(unit, RCOORD);
142
143         if(const Camera *camera = renderer.get_camera())
144                 /* Multiply by camera's object matrix to form a matrix that transforms
145                 from eye space to shadow space. */
146                 shdata.uniform("shd_eye_matrix", shadow_matrix*camera->get_object_matrix());
147         else
148                 shdata.uniform("shd_eye_matrix", shadow_matrix);
149
150         Renderer::Push _push_rend(renderer);
151         renderer.add_shader_data(shdata);
152         renderer.render(renderable, tag);
153
154         Texture::unbind_from(unit);
155         TexGen::unbind_from(unit, SCOORD);
156         TexGen::unbind_from(unit, TCOORD);
157         TexGen::unbind_from(unit, RCOORD);
158 }
159
160 } // namespace GL
161 } // namespace Msp