]> git.tdb.fi Git - libs/gl.git/blob - source/shadowmap.cpp
Use the frame notifications in ShadowMap
[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 "tests.h"
10 #include "texgen.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
33         set_darkness(0.7);
34         set_texture_unit(3);
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::set_texture_unit(unsigned u)
60 {
61         unit = u;
62
63         int i = unit;
64         shdata.uniform("shadow", i);
65         shdata.uniform("shadow_unit", i);
66 }
67
68 void ShadowMap::setup_frame() const
69 {
70         if(rendered)
71                 return;
72
73         renderable.setup_frame();
74         rendered = true;
75
76         Vector4 lpos = light.get_position();
77         if(lpos.w)
78         {
79                 /* XXX Not really proper way to support positional lights, but good
80                 enough when the light source is far away */
81                 lpos.x -= target.x;
82                 lpos.y -= target.y;
83                 lpos.z -= target.z;
84                 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y+lpos.z*lpos.z);
85                 lpos.x /= d;
86                 lpos.y /= d;
87                 lpos.z /= d;
88         }
89
90         float matrix[16];
91         if(abs(lpos.z)>=abs(lpos.x) && abs(lpos.z)>=abs(lpos.y))
92         {
93                 float d = sqrt(lpos.x*lpos.x+lpos.z*lpos.z);
94                 matrix[0] = lpos.z/d;
95                 matrix[4] = 0;
96                 matrix[8] = -lpos.x/d;
97                 matrix[1] = -lpos.x*lpos.y/d;
98                 matrix[5] = d;
99                 matrix[9] = -lpos.z*lpos.y/d;
100         }
101         else
102         {
103                 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y);
104                 matrix[0] = -lpos.y/d;
105                 matrix[4] = lpos.x/d;
106                 matrix[8] = 0;
107                 matrix[1] = -lpos.x*lpos.z/d;
108                 matrix[5] = -lpos.y*lpos.z/d;
109                 matrix[9] = d;
110         }
111
112         matrix[2] = lpos.x;
113         matrix[6] = lpos.y;
114         matrix[10] = lpos.z;
115
116         matrix[12] = -(target.x*matrix[0]+target.y*matrix[4]+target.z*matrix[8]);
117         matrix[13] = -(target.x*matrix[1]+target.y*matrix[5]+target.z*matrix[9]);
118         matrix[14] = -(target.x*matrix[2]+target.y*matrix[6]+target.z*matrix[10]);
119         matrix[3] = 0;
120         matrix[7] = 0;
121         matrix[11] = 0;
122         matrix[15] = 1;
123
124         light_matrix = matrix;
125
126         MatrixStack::Push push_mv(MatrixStack::modelview());
127         MatrixStack::Push push_proj(MatrixStack::projection());
128
129         MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius);
130         MatrixStack::modelview() = light_matrix;
131
132         Bind bind_fbo(fbo, true);
133         Bind bind_depth(DepthTest::lequal());
134         fbo.clear(DEPTH_BUFFER_BIT);
135         renderable.render("shadow");
136 }
137
138 void ShadowMap::finish_frame() const
139 {
140         renderable.finish_frame();
141         rendered = false;
142 }
143
144 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
145 {
146         if(!enabled_passes.count(tag))
147                 return renderer.render(renderable, tag);
148
149         const double *matrix = light_matrix.data();
150
151         // Has side effect of changing the current unit
152         depth_buf.bind_to(unit);
153         float diam = radius*2;
154         TexGen tg_s, tg_t, tg_r;
155         tg_s.set_plane(Vector4(matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5f));
156         tg_t.set_plane(Vector4(matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5f));
157         tg_r.set_plane(Vector4(-matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5f-matrix[14]/diam-depth_bias/size));
158         tg_s.bind_to(SCOORD);
159         tg_t.bind_to(TCOORD);
160         tg_r.bind_to(RCOORD);
161         TexUnit::activate(0);
162
163         Renderer::Push _push_rend(renderer);
164         renderer.add_shader_data(shdata);
165         renderer.render(renderable, tag);
166
167         Texture::unbind_from(unit);
168         TexGen::unbind_from(SCOORD);
169         TexGen::unbind_from(TCOORD);
170         TexGen::unbind_from(RCOORD);
171         TexUnit::activate(0);
172 }
173
174 } // namespace GL
175 } // namespace Msp