]> git.tdb.fi Git - libs/gl.git/blob - source/effects/environmentmap.h
Support effects and subordinate sequences inside sequence templates
[libs/gl.git] / source / effects / environmentmap.h
1 #ifndef MSP_GL_ENVIRONMENTMAP_H_
2 #define MSP_GL_ENVIRONMENTMAP_H_
3
4 #include <vector>
5 #include "camera.h"
6 #include "effect.h"
7 #include "framebuffer.h"
8 #include "programdata.h"
9 #include "texture2d.h"
10 #include "texturecube.h"
11
12 namespace Msp {
13 namespace GL {
14
15 class Mesh;
16 class Sampler;
17
18 /**
19 Creates a cube map texture of the surroundings of the renderable.  This texture
20 can then be used to implement effects such as reflections or refractions.
21
22 If the EnvironmentMap is used in a Sequence, it's worth noting that the cube
23 map will be prepared outside of any rendering pass.  It's recommended to use
24 another Sequence to define which passes should be used to render the
25 environment.
26 */
27 class EnvironmentMap: public Effect
28 {
29 public:
30         struct Template: Effect::Template
31         {
32                 class Loader: public DataFile::DerivedObjectLoader<Template, Effect::Template::Loader>
33                 {
34                 private:
35                         static ActionMap shared_actions;
36
37                 public:
38                         Loader(Template &, Collection &);
39                 private:
40                         virtual void init_actions();
41
42                         void fixed_position(float, float, float);
43                 };
44
45                 unsigned size = 512;
46                 PixelFormat format = RGB16F;
47                 std::string environment_name;
48                 unsigned roughness_levels = 5;
49                 Vector3 fixed_position;
50                 bool use_fixed_position = false;
51                 float near_clip = 0.1f;
52                 float far_clip = 100.0f;
53
54                 virtual EnvironmentMap *create(const std::map<std::string, Renderable *> &) const;
55         };
56
57 protected:
58         struct Face
59         {
60                 Framebuffer fbo;
61                 Camera camera;
62         };
63
64         unsigned size;
65         Renderable &environment;
66         TextureCube env_tex;
67         Texture2D depth_buf;
68         Face faces[6];
69         Vector3 fixed_position;
70         bool use_fixed_pos = false;
71
72         TextureCube irradiance;
73         const Program &irradiance_shprog;
74         Framebuffer irradiance_fbo;
75         const Program &specular_shprog;
76         std::vector<Framebuffer> specular_fbos;
77         ProgramData prefilter_shdata;
78         const Mesh &fullscreen_mesh;
79
80         const Sampler &sampler;
81         const Sampler &mip_sampler;
82         ProgramData shdata;
83         bool rendered = false;
84         unsigned update_interval = 1;
85         unsigned update_delay = 0;
86
87 public:
88         EnvironmentMap(unsigned size, PixelFormat, Renderable &rend, Renderable &env);
89         EnvironmentMap(unsigned size, PixelFormat, unsigned, Renderable &rend, Renderable &env);
90
91         void set_fixed_position(const Vector3 &);
92
93         void set_depth_clip(float, float);
94
95         /** Sets the interval in frames between environment map updates.  A value of
96         0 means an update is only done when manually requested. */
97         void set_update_interval(unsigned);
98
99         /** Request that the environment map is updated on the next frame. */
100         void queue_update();
101
102         virtual void setup_frame(Renderer &);
103         virtual void finish_frame();
104
105         virtual void render(Renderer &, Tag = Tag()) const;
106
107         virtual void set_debug_name(const std::string &);
108 };
109
110 } // namespace GL
111 } // namespace Msp
112
113 #endif