3 #include "environmentmap.h"
14 EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e):
17 irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
18 specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
19 fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
20 sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
21 mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
26 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, Renderable &r, Renderable &e):
29 irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
30 specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
31 fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
32 sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
33 mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
38 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable &r, Renderable &e):
41 irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
42 specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
43 fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
44 sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
45 mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
50 void EnvironmentMap::init(unsigned s, PixelFormat f, unsigned l)
52 if(!l || (1U<<(l-1))>=s)
53 throw invalid_argument("EnvironmentMap::EnvironmentMap");
60 env_tex.storage(f, size, l);
61 depth_buf.storage(DEPTH_COMPONENT32F, size, size);
62 for(unsigned i=0; i<6; ++i)
64 TextureCubeFace face = TextureCube::enumerate_faces(i);
65 faces[i].fbo.attach(COLOR_ATTACHMENT0, env_tex, face, 0);
66 faces[i].fbo.attach(DEPTH_ATTACHMENT, depth_buf);
67 faces[i].fbo.require_complete();
68 faces[i].camera.set_look_direction(TextureCube::get_face_direction(face));
69 faces[i].camera.set_up_direction(TextureCube::get_t_direction(face));
70 faces[i].camera.set_field_of_view(Geometry::Angle<float>::right());
71 faces[i].camera.set_aspect_ratio(1);
72 faces[i].camera.set_depth_clip(0.1, 100);
75 irradiance.storage(f, size/4, 1);
76 irradiance_fbo.attach_layered(COLOR_ATTACHMENT0, irradiance);
80 specular_fbos.resize(l-1);
81 for(unsigned i=1; i<l; ++i)
82 specular_fbos[i-1].attach_layered(COLOR_ATTACHMENT0, env_tex, i);
84 LinAl::Matrix<float, 3, 3> face_matrices[6];
85 for(unsigned i=0; i<6; ++i)
87 GL::TextureCubeFace face = GL::TextureCube::enumerate_faces(i);
88 GL::Vector3 columns[3];
89 columns[0] = GL::TextureCube::get_s_direction(face);
90 columns[1] = GL::TextureCube::get_t_direction(face);
91 columns[2] = GL::TextureCube::get_face_direction(face);
92 face_matrices[i] = LinAl::Matrix<float, 3, 3>::from_columns(columns);
95 prefilter_shdata.uniform_array("faces", 6, &face_matrices[0]);
96 prefilter_shdata.uniform("n_samples", 128);
97 prefilter_shdata.uniform("roughness", 1.0f);
100 shdata.uniform("env_world_matrix", LinAl::SquareMatrix<float, 3>::identity());
103 void EnvironmentMap::set_depth_clip(float n, float f)
105 for(unsigned i=0; i<6; ++i)
106 faces[i].camera.set_depth_clip(n, f);
109 void EnvironmentMap::set_update_interval(unsigned i)
112 update_delay = min(update_delay, update_interval-1);
115 void EnvironmentMap::queue_update()
120 void EnvironmentMap::setup_frame(Renderer &renderer)
126 renderable.setup_frame(renderer);
134 update_delay = update_interval-1;
135 environment.setup_frame(renderer);
137 const Matrix *matrix = renderable.get_matrix();
141 Renderer::Push push(renderer);
142 Renderer::Exclude exclude1(renderer, renderable);
143 Renderer::Exclude exclude2(renderer, *this);
145 Vector3 center = matrix->column(3).slice<3>(0);
147 BindRestore bind_fbo(faces[0].fbo);
148 for(unsigned i=0; i<6; ++i)
150 faces[i].camera.set_position(center);
152 faces[i].fbo.clear();
153 renderer.set_camera(faces[i].camera);
154 renderer.render(environment);
157 irradiance_fbo.bind();
158 renderer.set_shader_program(&irradiance_shprog, &prefilter_shdata);
159 renderer.set_texture("environment_map", &env_tex, &sampler);
160 fullscreen_mesh.draw(renderer);
162 renderer.set_shader_program(&specular_shprog);
163 for(unsigned i=0; i<specular_fbos.size(); ++i)
165 prefilter_shdata.uniform("roughness", 1.0f-sqrt(1.0f-static_cast<float>(i+1)/specular_fbos.size()));
166 specular_fbos[i].bind();
167 fullscreen_mesh.draw(renderer);
171 void EnvironmentMap::finish_frame()
176 renderable.finish_frame();
177 environment.finish_frame();
181 void EnvironmentMap::render(Renderer &renderer, Tag tag) const
183 if(!enabled_passes.count(tag))
184 return renderer.render(renderable, tag);
186 Renderer::Push _push_rend(renderer);
188 renderer.set_texture("environment_map", &env_tex, &mip_sampler);
189 renderer.set_texture("irradiance_map", &irradiance, &sampler);
190 renderer.add_shader_data(shdata);
191 renderer.render(renderable, tag);
194 void EnvironmentMap::set_debug_name(const string &name)
197 env_tex.set_debug_name(name+"/environment.texcb");
198 depth_buf.set_debug_name(name+"/environment_depth.rbuf");
199 static const char *face_names[] = { "X+", "X-", "Y+", "Y-", "Z+", "Z-" };
200 for(unsigned i=0; i<6; ++i)
202 faces[i].fbo.set_debug_name(format("%s [FBO:%s]", name, face_names[i]));
203 faces[i].camera.set_debug_name(format("%s/%s.camera", name, face_names[i]));
206 irradiance.set_debug_name(name+"/irradiance.texcb");
207 irradiance_fbo.set_debug_name(name+" [FBO:irradiance]");
208 for(unsigned i=0; i<specular_fbos.size(); ++i)
209 specular_fbos[i].set_debug_name(format("%s [FBO:specular_mip%d]", name, i+1));
210 prefilter_shdata.set_debug_name(name+" [UBO:prefilter]");
211 shdata.set_debug_name(name+" [UBO]");