]> git.tdb.fi Git - libs/gl.git/blob - source/shadowmap.cpp
Turn Effect into a Renderable
[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 "texunit.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 ShadowMap::ShadowMap(unsigned s, const Renderable &r, const Light &l):
17         Effect(r),
18         size(s),
19         light(l),
20         unit(3),
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
31 void ShadowMap::set_target(const Vector3 &t, float r)
32 {
33         target = t;
34         radius = r;
35 }
36
37 void ShadowMap::set_texture_unit(unsigned u)
38 {
39         unit = u;
40 }
41
42 void ShadowMap::render(Renderer &renderer, const Tag &tag) const
43 {
44         if(!enabled_passes.count(tag))
45                 return renderable.render(renderer, tag);
46
47         Vector4 lpos = light.get_position();
48         if(lpos.w)
49         {
50                 /* XXX Not really proper way to support positional lights, but good
51                 enough when the light source is far away */
52                 lpos.x -= target.x;
53                 lpos.y -= target.y;
54                 lpos.z -= target.z;
55                 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y+lpos.z*lpos.z);
56                 lpos.x /= d;
57                 lpos.y /= d;
58                 lpos.z /= d;
59         }
60
61         float matrix[16];
62         if(abs(lpos.z)>=abs(lpos.x) && abs(lpos.z)>=abs(lpos.y))
63         {
64                 float d = sqrt(lpos.x*lpos.x+lpos.z*lpos.z);
65                 matrix[0] = lpos.z/d;
66                 matrix[4] = 0;
67                 matrix[8] = -lpos.x/d;
68                 matrix[1] = -lpos.x*lpos.y/d;
69                 matrix[5] = d;
70                 matrix[9] = -lpos.z*lpos.y/d;
71         }
72         else
73         {
74                 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y);
75                 matrix[0] = -lpos.y/d;
76                 matrix[4] = lpos.x/d;
77                 matrix[8] = 0;
78                 matrix[1] = -lpos.x*lpos.z/d;
79                 matrix[5] = -lpos.y*lpos.z/d;
80                 matrix[9] = d;
81         }
82
83         matrix[2] = lpos.x;
84         matrix[6] = lpos.y;
85         matrix[10] = lpos.z;
86
87         matrix[12] = -(target.x*matrix[0]+target.y*matrix[4]+target.z*matrix[8]);
88         matrix[13] = -(target.x*matrix[1]+target.y*matrix[5]+target.z*matrix[9]);
89         matrix[14] = -(target.x*matrix[2]+target.y*matrix[6]+target.z*matrix[10]);
90         matrix[3] = 0;
91         matrix[7] = 0;
92         matrix[11] = 0;
93         matrix[15] = 1;
94
95         renderer.escape();
96
97         {
98                 MatrixStack::Push push_mv(MatrixStack::modelview());
99                 MatrixStack::Push push_proj(MatrixStack::projection());
100
101                 MatrixStack::projection() = Matrix::ortho(-radius, radius, -radius, radius, -radius, radius);
102                 MatrixStack::modelview() = matrix;
103
104                 Bind bind_fbo(fbo, true);
105                 fbo.clear(DEPTH_BUFFER_BIT);
106                 renderable.render("shadow");
107         }
108
109         depth_buf.bind_to(unit);
110         float diam = radius*2;
111         float s_eq[4] = { matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5f };
112         float t_eq[4] = { matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5f };
113         float r_eq[4] = { -matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5f-matrix[14]/diam-4.0f/size };
114         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
115         glTexGenfv(GL_S, GL_EYE_PLANE, s_eq);
116         enable(GL_TEXTURE_GEN_S);
117         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
118         glTexGenfv(GL_T, GL_EYE_PLANE, t_eq);
119         enable(GL_TEXTURE_GEN_T);
120         glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
121         glTexGenfv(GL_R, GL_EYE_PLANE, r_eq);
122         enable(GL_TEXTURE_GEN_R);
123
124         TexUnit::activate(0);
125
126         renderable.render(renderer, tag);
127
128         Texture::unbind_from(unit);
129         disable(GL_TEXTURE_GEN_S);
130         disable(GL_TEXTURE_GEN_T);
131         disable(GL_TEXTURE_GEN_R);
132         TexUnit::activate(0);
133 }
134
135 } // namespace GL
136 } // namespace Msp