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