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