3 #include <msp/strings/format.h>
4 #include "environmentmap.h"
14 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, Renderable &r, Renderable &e):
15 EnvironmentMap(s, f, 1, r, e)
18 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable &r, Renderable &e):
22 irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
23 specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
24 fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
25 sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
26 mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
28 if(!l || (1U<<(l-1))>=s)
29 throw invalid_argument("EnvironmentMap::EnvironmentMap");
31 env_tex.storage(f, size, l);
32 depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
33 for(unsigned i=0; i<6; ++i)
35 TextureCubeFace face = static_cast<TextureCubeFace>(i);
36 faces[i].fbo.set_format((COLOR_ATTACHMENT,f, DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
37 faces[i].fbo.attach(COLOR_ATTACHMENT, env_tex, face, 0);
38 faces[i].fbo.attach(DEPTH_ATTACHMENT, depth_buf);
39 faces[i].camera.set_look_direction(TextureCube::get_face_direction(face));
40 faces[i].camera.set_up_direction(TextureCube::get_t_direction(face));
41 faces[i].camera.set_field_of_view(Geometry::Angle<float>::right());
42 faces[i].camera.set_aspect_ratio(1);
43 faces[i].camera.set_depth_clip(0.1, 100);
46 irradiance.storage(f, size/4, 1);
47 irradiance_fbo.set_format((COLOR_ATTACHMENT,f));
48 irradiance_fbo.attach_layered(COLOR_ATTACHMENT, irradiance);
52 specular_fbos.resize(l-1);
53 for(unsigned i=1; i<l; ++i)
55 specular_fbos[i-1].set_format((COLOR_ATTACHMENT,f));
56 specular_fbos[i-1].attach_layered(COLOR_ATTACHMENT, env_tex, i);
59 LinAl::Matrix<float, 3, 3> face_matrices[6];
60 for(unsigned i=0; i<6; ++i)
62 GL::TextureCubeFace face = static_cast<TextureCubeFace>(i);
63 GL::Vector3 columns[3];
64 columns[0] = GL::TextureCube::get_s_direction(face);
65 columns[1] = GL::TextureCube::get_t_direction(face);
66 columns[2] = GL::TextureCube::get_face_direction(face);
67 face_matrices[i] = LinAl::Matrix<float, 3, 3>::from_columns(columns);
70 prefilter_shdata.uniform_array("faces", 6, &face_matrices[0]);
71 prefilter_shdata.uniform("n_samples", 128);
72 prefilter_shdata.uniform("roughness", 1.0f);
75 shdata.uniform("env_world_matrix", LinAl::SquareMatrix<float, 3>::identity());
78 void EnvironmentMap::set_fixed_position(const Vector3 &p)
84 void EnvironmentMap::set_depth_clip(float n, float f)
86 for(unsigned i=0; i<6; ++i)
87 faces[i].camera.set_depth_clip(n, f);
90 void EnvironmentMap::set_update_interval(unsigned i)
93 update_delay = min(update_delay, update_interval-1);
96 void EnvironmentMap::queue_update()
101 void EnvironmentMap::setup_frame(Renderer &renderer)
107 renderable.setup_frame(renderer);
115 update_delay = update_interval-1;
116 environment.setup_frame(renderer);
120 center = fixed_position;
123 const Matrix *matrix = renderable.get_matrix();
127 center = matrix->column(3).slice<3>(0);
130 Renderer::Push push(renderer);
131 Renderer::Exclude exclude1(renderer, renderable);
132 Renderer::Exclude exclude2(renderer, *this);
134 for(unsigned i=0; i<6; ++i)
136 faces[i].camera.set_position(center);
137 renderer.set_framebuffer(&faces[i].fbo);
138 renderer.set_camera(faces[i].camera);
139 renderer.render(environment);
142 renderer.set_framebuffer(&irradiance_fbo);
143 renderer.set_shader_program(&irradiance_shprog, &prefilter_shdata);
144 renderer.set_texture("environment_map", &env_tex, &sampler);
145 fullscreen_mesh.draw(renderer);
147 renderer.set_shader_program(&specular_shprog);
148 for(unsigned i=0; i<specular_fbos.size(); ++i)
150 prefilter_shdata.uniform("roughness", 1.0f-sqrt(1.0f-static_cast<float>(i+1)/specular_fbos.size()));
151 renderer.set_framebuffer(&specular_fbos[i]);
152 fullscreen_mesh.draw(renderer);
156 void EnvironmentMap::finish_frame()
161 renderable.finish_frame();
162 environment.finish_frame();
166 void EnvironmentMap::render(Renderer &renderer, Tag tag) const
168 if(!enabled_passes.count(tag))
169 return renderer.render(renderable, tag);
171 Renderer::Push _push_rend(renderer);
173 renderer.set_texture("environment_map", &env_tex, &mip_sampler);
174 renderer.set_texture("irradiance_map", &irradiance, &sampler);
175 renderer.add_shader_data(shdata);
176 renderer.render(renderable, tag);
179 void EnvironmentMap::set_debug_name(const string &name)
182 env_tex.set_debug_name(name+"/environment.texcb");
183 depth_buf.set_debug_name(name+"/environment_depth.rbuf");
184 static const char *const face_names[] = { "X+", "X-", "Y+", "Y-", "Z+", "Z-" };
185 for(unsigned i=0; i<6; ++i)
187 faces[i].fbo.set_debug_name(format("%s [FBO:%s]", name, face_names[i]));
188 faces[i].camera.set_debug_name(format("%s/%s.camera", name, face_names[i]));
191 irradiance.set_debug_name(name+"/irradiance.texcb");
192 irradiance_fbo.set_debug_name(name+" [FBO:irradiance]");
193 for(unsigned i=0; i<specular_fbos.size(); ++i)
194 specular_fbos[i].set_debug_name(format("%s [FBO:specular_mip%d]", name, i+1));
195 prefilter_shdata.set_debug_name(name+" [UBO:prefilter]");
196 shdata.set_debug_name(name+" [UBO]");