]> git.tdb.fi Git - libs/gl.git/blob - source/effects/environmentmap.h
Check the flat qualifier from the correct member
[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 content renderable, for
20 use in image-based lighting.  Also called a light probe.
21
22 The cube map can optionally be prefiltered for varying amounts of roughness.
23 An irradiance map for diffuse lighting is also created.
24
25 The EnvironmentMap won't be rendered inside its own environment.  This avoids
26 artifacts if one is used to create reflections on a complex object.
27
28 The shader fragment common.glsl provides interfaces to access the environment
29 data.
30 */
31 class EnvironmentMap: public Effect
32 {
33 public:
34         struct Template: Effect::Template
35         {
36                 class Loader: public DataFile::DerivedObjectLoader<Template, Effect::Template::Loader>
37                 {
38                 private:
39                         static ActionMap shared_actions;
40
41                 public:
42                         Loader(Template &, Collection &);
43                 private:
44                         virtual void init_actions();
45
46                         void fixed_position(float, float, float);
47                 };
48
49                 unsigned size = 512;
50                 PixelFormat format = RGBA16F;
51                 std::string environment_name;
52                 unsigned roughness_levels = 5;
53                 Vector3 fixed_position;
54                 bool use_fixed_position = false;
55                 float near_clip = 0.1f;
56                 float far_clip = 100.0f;
57
58                 virtual EnvironmentMap *create(const std::map<std::string, Renderable *> &) const;
59         };
60
61 protected:
62         struct Face
63         {
64                 Framebuffer fbo;
65                 Camera camera;
66         };
67
68         unsigned size;
69         Renderable &environment;
70         TextureCube env_tex;
71         Texture2D depth_buf;
72         Face faces[6];
73         Vector3 fixed_position;
74         bool use_fixed_pos = false;
75
76         TextureCube irradiance;
77         const Program &irradiance_shprog;
78         Framebuffer irradiance_fbo;
79         const Program &specular_shprog;
80         std::vector<Framebuffer> specular_fbos;
81         ProgramData prefilter_shdata;
82         const Mesh &fullscreen_mesh;
83
84         const Sampler &sampler;
85         const Sampler &mip_sampler;
86         ProgramData shdata;
87         bool rendered = false;
88         bool in_setup_frame = false;
89         unsigned update_interval = 1;
90         unsigned update_delay = 0;
91
92 public:
93         EnvironmentMap(unsigned size, PixelFormat, Renderable &content, Renderable &env);
94
95         /** Creates an EnvironmentMap with prefiltering for varying amounts of
96         roughness.  Levels specifies the number of prefilter mipmap levels and must
97         be valid for the size. */
98         EnvironmentMap(unsigned size, PixelFormat, unsigned levels, Renderable &content, Renderable &env);
99
100         /** Sets a fixed position to render the environment map from.  This can be
101         useful if the content renderable does not have a model matrix. */
102         void set_fixed_position(const Vector3 &);
103
104         void set_depth_clip(float, float);
105
106         /** Sets the interval in frames between environment map updates.  A value of
107         0 means an update is only done when manually requested. */
108         void set_update_interval(unsigned);
109
110         /** Request that the environment map is updated on the next frame. */
111         void queue_update();
112
113         virtual void setup_frame(Renderer &);
114         virtual void finish_frame();
115
116         virtual void render(Renderer &, Tag = Tag()) const;
117
118         virtual void set_debug_name(const std::string &);
119 };
120
121 } // namespace GL
122 } // namespace Msp
123
124 #endif