]> git.tdb.fi Git - libs/gl.git/blob - source/shadowmap.cpp
Rewrite Framebuffer to defer attach/detach operations until bound
[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.set_compare_enabled(true);
32         depth_buf.set_compare_func(LEQUAL);
33         depth_buf.set_wrap(CLAMP_TO_EDGE);
34         depth_buf.storage(DEPTH_COMPONENT, size, size, 0);
35         depth_buf.image(0, DEPTH_COMPONENT, UNSIGNED_BYTE, 0);
36         fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
37 }
38
39 void ShadowMap::set_target(const Vector3 &t, float r)
40 {
41         target = t;
42         radius = r;
43 }
44
45 void ShadowMap::set_texture_unit(unsigned u)
46 {
47         unit = u;
48 }
49
50 void ShadowMap::prepare()
51 {
52         Vector4 lpos = light.get_position();
53         if(lpos.w)
54         {
55                 /* XXX Not really proper way to support positional lights, but good
56                 enough when the light source is far away */
57                 lpos.x -= target.x;
58                 lpos.y -= target.y;
59                 lpos.z -= target.z;
60                 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y+lpos.z*lpos.z);
61                 lpos.x /= d;
62                 lpos.y /= d;
63                 lpos.z /= d;
64         }
65
66         float matrix[16];
67         if(abs(lpos.z)>=abs(lpos.x) && abs(lpos.z)>=abs(lpos.y))
68         {
69                 float d = sqrt(lpos.x*lpos.x+lpos.z*lpos.z);
70                 matrix[0] = lpos.z/d;
71                 matrix[4] = 0;
72                 matrix[8] = -lpos.x/d;
73                 matrix[1] = -lpos.x*lpos.y/d;
74                 matrix[5] = d;
75                 matrix[9] = -lpos.z*lpos.y/d;
76         }
77         else
78         {
79                 float d = sqrt(lpos.x*lpos.x+lpos.y*lpos.y);
80                 matrix[0] = -lpos.y/d;
81                 matrix[4] = lpos.x/d;
82                 matrix[8] = 0;
83                 matrix[1] = -lpos.x*lpos.z/d;
84                 matrix[5] = -lpos.y*lpos.z/d;
85                 matrix[9] = d;
86         }
87
88         matrix[2] = lpos.x;
89         matrix[6] = lpos.y;
90         matrix[10] = lpos.z;
91
92         matrix[12] = -(target.x*matrix[0]+target.y*matrix[4]+target.z*matrix[8]);
93         matrix[13] = -(target.x*matrix[1]+target.y*matrix[5]+target.z*matrix[9]);
94         matrix[14] = -(target.x*matrix[2]+target.y*matrix[6]+target.z*matrix[10]);
95         matrix[3] = 0;
96         matrix[7] = 0;
97         matrix[11] = 0;
98         matrix[15] = 1;
99
100         {
101                 matrix_mode(PROJECTION);
102                 push_matrix();
103                 load_identity();
104                 ortho(-radius, radius, -radius, radius, -radius, radius);
105                 matrix_mode(MODELVIEW);
106                 push_matrix();
107                 load_matrix(matrix);
108
109                 Bind bind_fbo(fbo, true);
110                 fbo.clear(DEPTH_BUFFER_BIT);
111                 scene.render("shadow");
112
113                 matrix_mode(PROJECTION);
114                 pop_matrix();
115                 matrix_mode(MODELVIEW);
116                 pop_matrix();
117         }
118
119         depth_buf.bind_to(unit);
120         float diam = radius*2;
121         float s_eq[4] = { matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5 };
122         float t_eq[4] = { matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5 };
123         float r_eq[4] = { -matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5-matrix[14]/diam-4.0/size };
124         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
125         glTexGenfv(GL_S, GL_EYE_PLANE, s_eq);
126         enable(GL_TEXTURE_GEN_S);
127         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
128         glTexGenfv(GL_T, GL_EYE_PLANE, t_eq);
129         enable(GL_TEXTURE_GEN_T);
130         glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
131         glTexGenfv(GL_R, GL_EYE_PLANE, r_eq);
132         enable(GL_TEXTURE_GEN_R);
133
134         TexUnit::activate(0);
135 }
136
137 void ShadowMap::cleanup()
138 {
139         Texture::unbind_from(unit);
140         disable(GL_TEXTURE_GEN_S);
141         disable(GL_TEXTURE_GEN_T);
142         disable(GL_TEXTURE_GEN_R);
143         TexUnit::activate(0);
144 }
145
146 } // namespace GL
147 } // namespace Msp