3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
5 #include "environmentmap.h"
16 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, Renderable &c, Renderable &e):
17 EnvironmentMap(s, f, 1, c, e)
20 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable &c, Renderable &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"))
30 if(!l || (1U<<(l-1))>=s)
31 throw invalid_argument("EnvironmentMap::EnvironmentMap");
33 env_tex.storage(f, size, l);
34 depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
35 for(unsigned i=0; i<6; ++i)
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);
48 irradiance.storage(f, size/4, 1);
49 irradiance_fbo.set_format((COLOR_ATTACHMENT,f));
50 irradiance_fbo.attach_layered(COLOR_ATTACHMENT, irradiance);
54 specular_fbos.resize(l-1);
55 for(unsigned i=1; i<l; ++i)
57 specular_fbos[i-1].set_format((COLOR_ATTACHMENT,f));
58 specular_fbos[i-1].attach_layered(COLOR_ATTACHMENT, env_tex, i);
61 LinAl::Matrix<float, 3, 3> face_matrices[6];
62 for(unsigned i=0; i<6; ++i)
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);
72 prefilter_shdata.uniform_array("faces", 6, &face_matrices[0]);
73 prefilter_shdata.uniform("n_samples", 128);
74 prefilter_shdata.uniform("roughness", 1.0f);
77 shdata.uniform("env_world_matrix", LinAl::SquareMatrix<float, 3>::identity());
80 void EnvironmentMap::set_fixed_position(const Vector3 &p)
86 void EnvironmentMap::set_depth_clip(float n, float f)
88 for(unsigned i=0; i<6; ++i)
89 faces[i].camera.set_depth_clip(n, f);
92 void EnvironmentMap::set_update_interval(unsigned i)
95 update_delay = min(update_delay, update_interval-1);
98 void EnvironmentMap::queue_update()
103 void EnvironmentMap::setup_frame(Renderer &renderer)
109 content.setup_frame(renderer);
117 update_delay = update_interval-1;
118 environment.setup_frame(renderer);
120 SetFlag set_in(in_setup_frame);
124 center = fixed_position;
127 const Matrix *matrix = content.get_matrix();
131 center = matrix->column(3).slice<3>(0);
134 Renderer::Push push(renderer);
136 for(unsigned i=0; i<6; ++i)
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);
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);
149 renderer.set_shader_program(&specular_shprog);
150 for(unsigned i=0; i<specular_fbos.size(); ++i)
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);
158 void EnvironmentMap::finish_frame()
163 content.finish_frame();
164 environment.finish_frame();
168 void EnvironmentMap::render(Renderer &renderer, Tag tag) const
172 if(!is_enabled_for_method(tag))
173 return content.render(renderer, tag);
175 Renderer::Push _push_rend(renderer);
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);
183 void EnvironmentMap::set_debug_name(const string &name)
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)
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]));
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]");
207 EnvironmentMap *EnvironmentMap::Template::create(const map<string, Renderable *> &renderables) const
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");
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);
219 return env_map.release();
223 DataFile::Loader::ActionMap EnvironmentMap::Template::Loader::shared_actions;
225 EnvironmentMap::Template::Loader::Loader(Template &t, Collection &c):
226 DerivedObjectLoader<Template, Effect::Template::Loader>(t, c)
228 set_actions(shared_actions);
231 void EnvironmentMap::Template::Loader::init_actions()
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);
242 void EnvironmentMap::Template::Loader::fixed_position(float x, float y, float z)
244 obj.fixed_position = Vector3(x, y, z);
245 obj.use_fixed_position = true;