]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.h
Add tetrahedron shadow maps for point lights
[libs/gl.git] / source / effects / shadowmap.h
1 #ifndef MSP_GL_SHADOWMAP_H_
2 #define MSP_GL_SHADOWMAP_H_
3
4 #include "camera.h"
5 #include "effect.h"
6 #include "framebuffer.h"
7 #include "programdata.h"
8 #include "rect.h"
9 #include "texture2d.h"
10 #include "vector.h"
11
12 namespace Msp {
13 namespace GL {
14
15 class DirectionalLight;
16 class Light;
17 class PointLight;
18
19 /**
20 Creates shadows on a renderable through a shadow map texture.  In the setup
21 phase, the scene is rendered to a depth texture from the point of view of the
22 lightsource.  This texture is then used in the rendering phase together with
23 texture coordinate generation to determine whether each fragment is lit.
24 */
25 class ShadowMap: public Effect
26 {
27 private:
28         enum ShadowType
29         {
30                 NONE,
31                 DIRECTIONAL,
32                 TETRAHEDRON
33         };
34
35         struct ShadowedLight
36         {
37                 const Light *light;
38                 unsigned index;
39                 Rect region;
40                 ShadowType type;
41                 unsigned view_index;
42                 Renderable *shadow_caster;
43         };
44
45         struct ShadowView
46         {
47                 unsigned light_index;
48                 unsigned face;
49                 Camera camera;
50                 Matrix face_matrix;
51         };
52
53         unsigned width;
54         unsigned height;
55         const Lighting *lighting;
56         std::vector<ShadowedLight> lights;
57         std::vector<ShadowView> views;
58         Framebuffer fbo;
59         Texture2D depth_buf;
60         const Sampler &sampler;
61         Vector3 target;
62         float radius = 1.0f;
63         float depth_bias = 4.0f;
64         float darkness = 1.0f;
65         ProgramData shdata;
66         bool rendered = false;
67         std::string debug_name;
68
69         ShadowMap(unsigned, unsigned, Renderable &, const Lighting *);
70 public:
71         ShadowMap(unsigned, Renderable &, const DirectionalLight &, Renderable &);
72         ShadowMap(unsigned, unsigned, Renderable &, const Lighting &);
73
74         void add_light(const DirectionalLight &, unsigned, Renderable &);
75         void add_light(const PointLight &, unsigned, Renderable &);
76 private:
77         void add_light(const Light &, unsigned, ShadowType, Renderable &);
78
79 public:
80         /** Sets the ShadowMap target point and radius.  The transformation matrix is
81         computed so that a sphere with the specified parameters will be completely
82         covered by the ShadowMap. */
83         void set_target(const Vector3 &, float);
84
85         /** Sets the darkness of shadows.  Must be in the range between 0.0 and 1.0,
86         inclusive.  Only usable with shaders, and provided through the
87         shadow_darkness uniform. */
88         void set_darkness(float);
89
90         /** Sets a distance beyond objects from which the shadow starts.  Expressed
91         in pixel-sized units.  Must be positive; values less than 1.0 are not
92         recommended.  Larger values produce less depth artifacts, but may prevent
93         thin objects from casting shadows on nearby sufraces. */
94         void set_depth_bias(float);
95
96         const Texture2D &get_depth_texture() const { return depth_buf; }
97
98         virtual void setup_frame(Renderer &);
99         virtual void finish_frame();
100
101         virtual void render(Renderer &, Tag = Tag()) const;
102
103         virtual void set_debug_name(const std::string &);
104 };
105
106 } // namespace GL
107 } // namespace Msp
108
109 #endif