]> git.tdb.fi Git - libs/gl.git/blob - source/shadowmap.cpp
Disable blend and depth test while rendering Bloom
[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         radius(1)
28 {
29         depth_buf.set_min_filter(LINEAR);
30         depth_buf.storage(DEPTH_COMPONENT, size, size, 0);
31         depth_buf.image(0, DEPTH_COMPONENT, UNSIGNED_BYTE, 0);
32         depth_buf.parameter(GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
33         depth_buf.parameter(GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
34         depth_buf.parameter(GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
35         depth_buf.parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
36         depth_buf.parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
37         fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
38         glDrawBuffer(GL_NONE);
39         Framebuffer::unbind();
40         Texture::unbind();
41 }
42
43 void ShadowMap::set_target(const Vector3 &t, float r)
44 {
45         target=t;
46         radius=r;
47 }
48
49 void ShadowMap::prepare()
50 {
51         const Vector4 &lpos=light.get_position();
52         if(lpos.w)
53                 throw Exception("Non-directional lights not supported at the moment");
54
55         float matrix[16];
56         if(abs(lpos.z)>=abs(lpos.x) && abs(lpos.z)>=abs(lpos.y))
57         {
58                 float d=sqrt(lpos.x*lpos.x+lpos.z*lpos.z);
59                 matrix[0]=lpos.z/d;
60                 matrix[4]=0;
61                 matrix[8]=-lpos.x/d;
62                 matrix[1]=-lpos.x*lpos.y/d;
63                 matrix[5]=d;
64                 matrix[9]=-lpos.z*lpos.y/d;
65         }
66         else
67         {
68                 float d=sqrt(lpos.x*lpos.x+lpos.y*lpos.y);
69                 matrix[0]=-lpos.y/d;
70                 matrix[4]=lpos.x/d;
71                 matrix[8]=0;
72                 matrix[1]=-lpos.x*lpos.z/d;
73                 matrix[5]=-lpos.y*lpos.z/d;
74                 matrix[9]=d;
75         }
76
77         matrix[2]=lpos.x;
78         matrix[6]=lpos.y;
79         matrix[10]=lpos.z;
80
81         matrix[12]=-(target.x*matrix[0]+target.y*matrix[4]+target.z*matrix[8]);
82         matrix[13]=-(target.x*matrix[1]+target.y*matrix[5]+target.z*matrix[9]);
83         matrix[14]=-(target.x*matrix[2]+target.y*matrix[6]+target.z*matrix[10]);
84         matrix[3]=0;
85         matrix[7]=0;
86         matrix[11]=0;
87         matrix[15]=1;
88
89         {
90                 matrix_mode(PROJECTION);
91                 push_matrix();
92                 load_identity();
93                 ortho(-radius, radius, -radius, radius, -radius, radius);
94                 matrix_mode(MODELVIEW);
95                 push_matrix();
96                 load_matrix(matrix);
97
98                 const Framebuffer *old_fbo=Framebuffer::current();
99                 fbo.bind();
100                 glViewport(0, 0, size, size);
101                 glClear(GL_DEPTH_BUFFER_BIT);
102                 scene.render("shadow");
103
104                 matrix_mode(PROJECTION);
105                 pop_matrix();
106                 matrix_mode(MODELVIEW);
107                 pop_matrix();
108                 if(old_fbo)
109                         old_fbo->bind();
110                 else
111                         Framebuffer::unbind();
112         }
113
114         depth_buf.bind_to(3);
115         float diam=radius*2;
116         float s_eq[4]={ matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5 };
117         float t_eq[4]={ matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5 };
118         float r_eq[4]={ -matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5-matrix[14]/diam-4.0/size };
119         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
120         glTexGenfv(GL_S, GL_EYE_PLANE, s_eq);
121         enable(GL_TEXTURE_GEN_S);
122         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
123         glTexGenfv(GL_T, GL_EYE_PLANE, t_eq);
124         enable(GL_TEXTURE_GEN_T);
125         glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
126         glTexGenfv(GL_R, GL_EYE_PLANE, r_eq);
127         enable(GL_TEXTURE_GEN_R);
128
129         TexUnit::activate(0);
130 }
131
132 void ShadowMap::cleanup()
133 {
134         TexUnit::activate(3);
135         Texture::unbind();
136         disable(GL_TEXTURE_GEN_S);
137         disable(GL_TEXTURE_GEN_T);
138         disable(GL_TEXTURE_GEN_R);
139         TexUnit::activate(0);
140 }
141
142 } // namespace GL
143 } // namespace Msp