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