1 #ifndef MSP_GL_SHADOWMAP_H_
2 #define MSP_GL_SHADOWMAP_H_
6 #include "framebuffer.h"
7 #include "programdata.h"
15 class DirectionalLight;
20 Creates shadows on a renderable through a shadow map texture. In the setup
21 phase, the scene is rendered to a depth texture from the point of view of the
22 lightsource. This texture is then used in the rendering phase together with
23 texture coordinate generation to determine whether each fragment is lit.
25 class ShadowMap: public Effect
28 struct Template: Effect::Template
30 class Loader: public DataFile::DerivedObjectLoader<Template, Effect::Template::Loader>
33 static ActionMap shared_actions;
36 Loader(Template &, Collection &);
38 virtual void init_actions();
40 void light(const std::string &);
41 void size_square(unsigned);
42 void target(float, float, float);
47 class Loader: public DataFile::ObjectLoader<ShadowedLight>
50 static ActionMap shared_actions;
53 Loader(ShadowedLight &);
55 virtual void init_actions();
58 const Light *light = 0;
60 std::string shadow_caster_name;
63 unsigned width = 2048;
64 unsigned height = 2048;
65 const Lighting *lighting = 0;
66 std::vector<ShadowedLight> lights;
69 float depth_bias = 4.0f;
70 float darkness = 1.0f;
72 virtual ShadowMap *create(const std::map<std::string, Renderable *> &) const;
90 Renderable *shadow_caster;
103 const Lighting *lighting;
104 std::vector<ShadowedLight> lights;
105 std::vector<ShadowView> views;
108 const Sampler &sampler;
111 float depth_bias = 4.0f;
112 float darkness = 1.0f;
114 bool rendered = false;
115 std::string debug_name;
117 ShadowMap(unsigned, unsigned, Renderable &, const Lighting *);
119 ShadowMap(unsigned, Renderable &, const DirectionalLight &, Renderable &);
120 ShadowMap(unsigned, unsigned, Renderable &, const Lighting &);
122 void add_light(const DirectionalLight &, unsigned, Renderable &);
123 void add_light(const PointLight &, unsigned, Renderable &);
125 void add_light(const Light &, unsigned, ShadowType, Renderable &);
128 /** Sets the ShadowMap target point and radius. The transformation matrix is
129 computed so that a sphere with the specified parameters will be completely
130 covered by the ShadowMap. */
131 void set_target(const Vector3 &, float);
133 /** Sets the darkness of shadows. Must be in the range between 0.0 and 1.0,
134 inclusive. Only usable with shaders, and provided through the
135 shadow_darkness uniform. */
136 void set_darkness(float);
138 /** Sets a distance beyond objects from which the shadow starts. Expressed
139 in pixel-sized units. Must be positive; values less than 1.0 are not
140 recommended. Larger values produce less depth artifacts, but may prevent
141 thin objects from casting shadows on nearby sufraces. */
142 void set_depth_bias(float);
144 const Texture2D &get_depth_texture() const { return depth_buf; }
146 virtual void setup_frame(Renderer &);
147 virtual void finish_frame();
149 virtual void render(Renderer &, Tag = Tag()) const;
151 virtual void set_debug_name(const std::string &);