]> git.tdb.fi Git - libs/gl.git/blob - source/shadowmap.cpp
Add Vector3 and Vector4 classes
[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                 Bind _bind_fbo(fbo);
99                 glViewport(0, 0, size, size);
100                 glClear(GL_DEPTH_BUFFER_BIT);
101                 scene.render();
102
103                 matrix_mode(PROJECTION);
104                 pop_matrix();
105                 matrix_mode(MODELVIEW);
106                 pop_matrix();
107         }
108
109         depth_buf.bind_to(3);
110         float diam=radius*2;
111         float s_eq[4]={ matrix[0]/diam, matrix[4]/diam, matrix[8]/diam, matrix[12]/diam+0.5 };
112         float t_eq[4]={ matrix[1]/diam, matrix[5]/diam, matrix[9]/diam, matrix[13]/diam+0.5 };
113         float r_eq[4]={ -matrix[2]/diam, -matrix[6]/diam, -matrix[10]/diam, 0.5-matrix[14]/diam-4.0/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
127 void ShadowMap::cleanup()
128 {
129         TexUnit::activate(3);
130         Texture::unbind();
131         disable(GL_TEXTURE_GEN_S);
132         disable(GL_TEXTURE_GEN_T);
133         disable(GL_TEXTURE_GEN_R);
134         TexUnit::activate(0);
135 }
136
137 } // namespace GL
138 } // namespace Msp