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