]> git.tdb.fi Git - libs/gl.git/blob - source/shadowmap.cpp
Style update: add spaces around assignment operators
[libs/gl.git] / source / shadowmap.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cmath>
9 #include <cstdlib>
10 #include "light.h"
11 #include "matrix.h"
12 #include "misc.h"
13 #include "projection.h"
14 #include "scene.h"
15 #include "shadowmap.h"
16 #include "texunit.h"
17
18 using namespace std;
19
20 namespace Msp {
21 namespace GL {
22
23 ShadowMap::ShadowMap(unsigned s, const Scene &c, const Light &l):
24         size(s),
25         scene(c),
26         light(l),
27         unit(3),
28         radius(1)
29 {
30         depth_buf.set_min_filter(LINEAR);
31         depth_buf.storage(DEPTH_COMPONENT, size, size, 0);
32         depth_buf.image(0, DEPTH_COMPONENT, UNSIGNED_BYTE, 0);
33         depth_buf.parameter(GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
34         depth_buf.parameter(GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
35         depth_buf.parameter(GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
36         depth_buf.parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
37         depth_buf.parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
38         fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
39         draw_buffer(NO_BUFFER);
40         Framebuffer::unbind();
41         Texture::unbind();
42 }
43
44 void ShadowMap::set_target(const Vector3 &t, float r)
45 {
46         target = t;
47         radius = r;
48 }
49
50 void ShadowMap::set_texture_unit(unsigned u)
51 {
52         unit = u;
53 }
54
55 void ShadowMap::prepare()
56 {
57         Vector4 lpos = light.get_position();
58         if(lpos.w)
59         {
60                 /* XXX Not really proper way to support positional lights, but good
61                 enough when the light source is far away */
62                 lpos.x -= target.x;
63                 lpos.y -= target.y;
64                 lpos.z -= target.z;
65                 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y+lpos.z*lpos.z);
66                 lpos.x /= d;
67                 lpos.y /= d;
68                 lpos.z /= d;
69         }
70
71         float matrix[16];
72         if(abs(lpos.z)>=abs(lpos.x) && abs(lpos.z)>=abs(lpos.y))
73         {
74                 float d = sqrt(lpos.x*lpos.x+lpos.z*lpos.z);
75                 matrix[0] = lpos.z/d;
76                 matrix[4] = 0;
77                 matrix[8] = -lpos.x/d;
78                 matrix[1] = -lpos.x*lpos.y/d;
79                 matrix[5] = d;
80                 matrix[9] = -lpos.z*lpos.y/d;
81         }
82         else
83         {
84                 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y);
85                 matrix[0] = -lpos.y/d;
86                 matrix[4] = lpos.x/d;
87                 matrix[8] = 0;
88                 matrix[1] = -lpos.x*lpos.z/d;
89                 matrix[5] = -lpos.y*lpos.z/d;
90                 matrix[9] = d;
91         }
92
93         matrix[2] = lpos.x;
94         matrix[6] = lpos.y;
95         matrix[10] = lpos.z;
96
97         matrix[12] = -(target.x*matrix[0]+target.y*matrix[4]+target.z*matrix[8]);
98         matrix[13] = -(target.x*matrix[1]+target.y*matrix[5]+target.z*matrix[9]);
99         matrix[14] = -(target.x*matrix[2]+target.y*matrix[6]+target.z*matrix[10]);
100         matrix[3] = 0;
101         matrix[7] = 0;
102         matrix[11] = 0;
103         matrix[15] = 1;
104
105         {
106                 matrix_mode(PROJECTION);
107                 push_matrix();
108                 load_identity();
109                 ortho(-radius, radius, -radius, radius, -radius, radius);
110                 matrix_mode(MODELVIEW);
111                 push_matrix();
112                 load_matrix(matrix);
113
114                 const Framebuffer *old_fbo = Framebuffer::current();
115                 fbo.bind();
116                 clear(DEPTH_BUFFER_BIT);
117                 scene.render("shadow");
118
119                 matrix_mode(PROJECTION);
120                 pop_matrix();
121                 matrix_mode(MODELVIEW);
122                 pop_matrix();
123                 if(old_fbo)
124                         old_fbo->bind();
125                 else
126                         Framebuffer::unbind();
127         }
128
129         depth_buf.bind_to(unit);
130         float diam = radius*2;
131         float s_eq[4] = { matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5 };
132         float t_eq[4] = { matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5 };
133         float r_eq[4] = { -matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5-matrix[14]/diam-4.0/size };
134         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
135         glTexGenfv(GL_S, GL_EYE_PLANE, s_eq);
136         enable(GL_TEXTURE_GEN_S);
137         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
138         glTexGenfv(GL_T, GL_EYE_PLANE, t_eq);
139         enable(GL_TEXTURE_GEN_T);
140         glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
141         glTexGenfv(GL_R, GL_EYE_PLANE, r_eq);
142         enable(GL_TEXTURE_GEN_R);
143
144         TexUnit::activate(0);
145 }
146
147 void ShadowMap::cleanup()
148 {
149         TexUnit::activate(unit);
150         Texture::unbind();
151         disable(GL_TEXTURE_GEN_S);
152         disable(GL_TEXTURE_GEN_T);
153         disable(GL_TEXTURE_GEN_R);
154         TexUnit::activate(0);
155 }
156
157 } // namespace GL
158 } // namespace Msp