]> git.tdb.fi Git - libs/gl.git/blob - source/effects/shadowmap.h
Access builtin resources through a global instance
[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 "texture2d.h"
9 #include "vector.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class Light;
15 class Resources;
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         unsigned size;
27         const Light &light;
28         Renderable &shadow_caster;
29         Framebuffer fbo;
30         Camera shadow_camera;
31         Matrix shadow_matrix;
32         Texture2D depth_buf;
33         const Sampler &sampler;
34         Vector3 target;
35         float radius;
36         float depth_bias;
37         ProgramData shdata;
38         bool rendered;
39
40 public:
41         ShadowMap(unsigned, Renderable &, const Light &, Renderable &);
42         DEPRECATED ShadowMap(unsigned, Renderable &, const Light &);
43 private:
44         void init(unsigned);
45
46 public:
47         /** Sets the ShadowMap target point and radius.  The transformation matrix is
48         computed so that a sphere with the specified parameters will be completely
49         covered by the ShadowMap. */
50         void set_target(const Vector3 &, float);
51
52         /** Sets the darkness of shadows.  Must be in the range between 0.0 and 1.0,
53         inclusive.  Only usable with shaders, and provided through the
54         shadow_darkness uniform. */
55         void set_darkness(float);
56
57         /** Sets a distance beyond objects from which the shadow starts.  Expressed
58         in pixel-sized units.  Must be positive; values less than 1.0 are not
59         recommended.  Larger values produce less depth artifacts, but may prevent
60         thin objects from casting shadows on nearby sufraces. */
61         void set_depth_bias(float);
62
63         const Texture2D &get_depth_texture() const { return depth_buf; }
64         const Matrix &get_shadow_matrix() const { return shadow_matrix; }
65
66         virtual void setup_frame(Renderer &);
67         virtual void finish_frame();
68
69         virtual void render(Renderer &, Tag = Tag()) const;
70 };
71
72 } // namespace GL
73 } // namespace Msp
74
75 #endif