]> git.tdb.fi Git - libs/gl.git/blob - source/effects/environmentmap.cpp
Remove deprecated functions from Renderer
[libs/gl.git] / source / effects / environmentmap.cpp
1 #include <algorithm>
2 #include <cmath>
3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
5 #include "environmentmap.h"
6 #include "error.h"
7 #include "mesh.h"
8 #include "renderer.h"
9 #include "resources.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, Renderable &c, Renderable &e):
17         EnvironmentMap(s, f, 1, c, e)
18 { }
19
20 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable &c, Renderable &e):
21         Effect(c),
22         size(s),
23         environment(e),
24         irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
25         specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
26         fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
27         sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
28         mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
29 {
30         if(!l || (1U<<(l-1))>=s)
31                 throw invalid_argument("EnvironmentMap::EnvironmentMap");
32
33         env_tex.storage(f, size, l);
34         depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
35         for(unsigned i=0; i<6; ++i)
36         {
37                 TextureCubeFace face = static_cast<TextureCubeFace>(i);
38                 faces[i].fbo.set_format((COLOR_ATTACHMENT,f, DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
39                 faces[i].fbo.attach(COLOR_ATTACHMENT, env_tex, face, 0);
40                 faces[i].fbo.attach(DEPTH_ATTACHMENT, depth_buf);
41                 faces[i].camera.set_look_direction(TextureCube::get_face_direction(face));
42                 faces[i].camera.set_up_direction(TextureCube::get_t_direction(face));
43                 faces[i].camera.set_field_of_view(Geometry::Angle<float>::right());
44                 faces[i].camera.set_aspect_ratio(1);
45                 faces[i].camera.set_depth_clip(0.1, 100);
46         }
47
48         irradiance.storage(f, size/4, 1);
49         irradiance_fbo.set_format((COLOR_ATTACHMENT,f));
50         irradiance_fbo.attach_layered(COLOR_ATTACHMENT, irradiance);
51
52         if(l>1)
53         {
54                 specular_fbos.resize(l-1);
55                 for(unsigned i=1; i<l; ++i)
56                 {
57                         specular_fbos[i-1].set_format((COLOR_ATTACHMENT,f));
58                         specular_fbos[i-1].attach_layered(COLOR_ATTACHMENT, env_tex, i);
59                 }
60
61                 LinAl::Matrix<float, 3, 3> face_matrices[6];
62                 for(unsigned i=0; i<6; ++i)
63                 {
64                         GL::TextureCubeFace face = static_cast<TextureCubeFace>(i);
65                         GL::Vector3 columns[3];
66                         columns[0] = GL::TextureCube::get_s_direction(face);
67                         columns[1] = GL::TextureCube::get_t_direction(face);
68                         columns[2] = GL::TextureCube::get_face_direction(face);
69                         face_matrices[i] = LinAl::Matrix<float, 3, 3>::from_columns(columns);
70                 }
71
72                 prefilter_shdata.uniform_array("faces", 6, &face_matrices[0]);
73                 prefilter_shdata.uniform("n_samples", 128);
74                 prefilter_shdata.uniform("roughness", 1.0f);
75         }
76
77         shdata.uniform("env_world_matrix", LinAl::SquareMatrix<float, 3>::identity());
78 }
79
80 void EnvironmentMap::set_fixed_position(const Vector3 &p)
81 {
82         fixed_position = p;
83         use_fixed_pos = true;
84 }
85
86 void EnvironmentMap::set_depth_clip(float n, float f)
87 {
88         for(unsigned i=0; i<6; ++i)
89                 faces[i].camera.set_depth_clip(n, f);
90 }
91
92 void EnvironmentMap::set_update_interval(unsigned i)
93 {
94         update_interval = i;
95         update_delay = min(update_delay, update_interval-1);
96 }
97
98 void EnvironmentMap::queue_update()
99 {
100         update_delay = 0;
101 }
102
103 void EnvironmentMap::setup_frame(Renderer &renderer)
104 {
105         if(rendered)
106                 return;
107
108         rendered = true;
109         content.setup_frame(renderer);
110
111         if(update_delay)
112         {
113                 if(update_interval)
114                         --update_delay;
115                 return;
116         }
117         update_delay = update_interval-1;
118         environment.setup_frame(renderer);
119
120         SetFlag set_in(in_setup_frame);
121
122         Vector3 center;
123         if(use_fixed_pos)
124                 center = fixed_position;
125         else
126         {
127                 const Matrix *matrix = content.get_matrix();
128                 if(!matrix)
129                         return;
130
131                 center = matrix->column(3).slice<3>(0);
132         }
133
134         Renderer::Push push(renderer);
135
136         for(unsigned i=0; i<6; ++i)
137         {
138                 faces[i].camera.set_position(center);
139                 renderer.set_framebuffer(&faces[i].fbo);
140                 renderer.set_camera(faces[i].camera);
141                 environment.render(renderer);
142         }
143
144         renderer.set_framebuffer(&irradiance_fbo);
145         renderer.set_shader_program(&irradiance_shprog, &prefilter_shdata);
146         renderer.set_texture("environment_map", &env_tex, &sampler);
147         fullscreen_mesh.draw(renderer);
148
149         renderer.set_shader_program(&specular_shprog);
150         for(unsigned i=0; i<specular_fbos.size(); ++i)
151         {
152                 prefilter_shdata.uniform("roughness", 1.0f-sqrt(1.0f-static_cast<float>(i+1)/specular_fbos.size()));
153                 renderer.set_framebuffer(&specular_fbos[i]);
154                 fullscreen_mesh.draw(renderer);
155         }
156 }
157
158 void EnvironmentMap::finish_frame()
159 {
160         if(rendered)
161         {
162                 rendered = false;
163                 content.finish_frame();
164                 environment.finish_frame();
165         }
166 }
167
168 void EnvironmentMap::render(Renderer &renderer, Tag tag) const
169 {
170         if(in_setup_frame)
171                 return;
172         if(!is_enabled_for_method(tag))
173                 return content.render(renderer, tag);
174
175         Renderer::Push _push_rend(renderer);
176
177         renderer.set_texture("environment_map", &env_tex, &mip_sampler);
178         renderer.set_texture("irradiance_map", &irradiance, &sampler);
179         renderer.add_shader_data(shdata);
180         content.render(renderer, tag);
181 }
182
183 void EnvironmentMap::set_debug_name(const string &name)
184 {
185 #ifdef DEBUG
186         env_tex.set_debug_name(name+"/environment.tex");
187         depth_buf.set_debug_name(name+"/environment_depth.rbuf");
188         static const char *const face_names[] = { "X+", "X-", "Y+", "Y-", "Z+", "Z-" };
189         for(unsigned i=0; i<6; ++i)
190         {
191                 faces[i].fbo.set_debug_name(format("%s [FBO:%s]", name, face_names[i]));
192                 faces[i].camera.set_debug_name(format("%s/%s.camera", name, face_names[i]));
193         }
194
195         irradiance.set_debug_name(name+"/irradiance.tex");
196         irradiance_fbo.set_debug_name(name+" [FBO:irradiance]");
197         for(unsigned i=0; i<specular_fbos.size(); ++i)
198                 specular_fbos[i].set_debug_name(format("%s [FBO:specular_mip%d]", name, i+1));
199         prefilter_shdata.set_debug_name(name+" [UBO:prefilter]");
200         shdata.set_debug_name(name+" [UBO]");
201 #else
202         (void)name;
203 #endif
204 }
205
206
207 EnvironmentMap *EnvironmentMap::Template::create(const map<string, Renderable *> &renderables) const
208 {
209         Renderable *content = get_item(renderables, content_name);
210         Renderable *environment = get_item(renderables, environment_name);
211         if(!content || !environment)
212                 throw invalid_operation("EnvironmentMap::Template::create");
213
214         RefPtr<EnvironmentMap> env_map = new EnvironmentMap(size, format, roughness_levels, *content, *environment);
215         if(use_fixed_position)
216                 env_map->set_fixed_position(fixed_position);
217         env_map->set_depth_clip(near_clip, far_clip);
218
219         return env_map.release();
220 }
221
222
223 DataFile::Loader::ActionMap EnvironmentMap::Template::Loader::shared_actions;
224
225 EnvironmentMap::Template::Loader::Loader(Template &t, Collection &c):
226         DerivedObjectLoader<Template, Effect::Template::Loader>(t, c)
227 {
228         set_actions(shared_actions);
229 }
230
231 void EnvironmentMap::Template::Loader::init_actions()
232 {
233         Effect::Template::Loader::init_actions();
234         add("depth_clip", &Template::near_clip, &Template::far_clip);
235         add("environment", &Template::environment_name);
236         add("fixed_position", &Loader::fixed_position);
237         add("format", &Template::format);
238         add("roughness_levels", &Template::roughness_levels);
239         add("size", &Template::size);
240 }
241
242 void EnvironmentMap::Template::Loader::fixed_position(float x, float y, float z)
243 {
244         obj.fixed_position = Vector3(x, y, z);
245         obj.use_fixed_position = true;
246 }
247
248 } // namespace GL
249 } // namespace Msp