]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.h
Refactor ShadowMap to support multiple 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 Light;
16
17 /**
18 Creates shadows on a renderable through a shadow map texture.  In the setup
19 phase, the scene is rendered to a depth texture from the point of view of the
20 lightsource.  This texture is then used in the rendering phase together with
21 texture coordinate generation to determine whether each fragment is lit.
22 */
23 class ShadowMap: public Effect
24 {
25 private:
26         struct ShadowedLight
27         {
28                 const Light *light;
29                 unsigned index;
30                 Rect region;
31                 Camera shadow_camera;
32         };
33
34         unsigned width;
35         unsigned height;
36         const Lighting *lighting;
37         std::vector<ShadowedLight> lights;
38         Renderable &shadow_caster;
39         Framebuffer fbo;
40         Texture2D depth_buf;
41         const Sampler &sampler;
42         Vector3 target;
43         float radius;
44         float depth_bias;
45         float darkness;
46         ProgramData shdata;
47         bool rendered;
48         std::string debug_name;
49
50         ShadowMap(unsigned, unsigned, Renderable &, const Lighting *, Renderable &);
51 public:
52         ShadowMap(unsigned, Renderable &, const Light &, Renderable &);
53         ShadowMap(unsigned, unsigned, Renderable &, const Lighting &, Renderable &);
54
55         void add_light(const Light &, unsigned);
56
57         /** Sets the ShadowMap target point and radius.  The transformation matrix is
58         computed so that a sphere with the specified parameters will be completely
59         covered by the ShadowMap. */
60         void set_target(const Vector3 &, float);
61
62         /** Sets the darkness of shadows.  Must be in the range between 0.0 and 1.0,
63         inclusive.  Only usable with shaders, and provided through the
64         shadow_darkness uniform. */
65         void set_darkness(float);
66
67         /** Sets a distance beyond objects from which the shadow starts.  Expressed
68         in pixel-sized units.  Must be positive; values less than 1.0 are not
69         recommended.  Larger values produce less depth artifacts, but may prevent
70         thin objects from casting shadows on nearby sufraces. */
71         void set_depth_bias(float);
72
73         const Texture2D &get_depth_texture() const { return depth_buf; }
74
75         virtual void setup_frame(Renderer &);
76         virtual void finish_frame();
77
78         virtual void render(Renderer &, Tag = Tag()) const;
79
80         virtual void set_debug_name(const std::string &);
81 };
82
83 } // namespace GL
84 } // namespace Msp
85
86 #endif