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