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