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