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