3 #include "environmentmap.h"
13 EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e):
16 irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
17 specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
18 fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
19 sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
20 mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
25 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, Renderable &r, Renderable &e):
28 irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
29 specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
30 fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
31 sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
32 mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
37 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable &r, Renderable &e):
40 irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
41 specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
42 fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
43 sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
44 mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
49 void EnvironmentMap::init(unsigned s, PixelFormat f, unsigned l)
51 if(!l || (1U<<(l-1))>=s)
52 throw invalid_argument("EnvironmentMap::EnvironmentMap");
59 env_tex.storage(f, size, l);
60 depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
61 for(unsigned i=0; i<6; ++i)
63 TextureCubeFace face = TextureCube::enumerate_faces(i);
64 faces[i].fbo.set_format((COLOR_ATTACHMENT,f, DEPTH_ATTACHMENT,DEPTH_COMPONENT32F));
65 faces[i].fbo.attach(COLOR_ATTACHMENT, env_tex, face, 0);
66 faces[i].fbo.attach(DEPTH_ATTACHMENT, depth_buf);
67 faces[i].camera.set_look_direction(TextureCube::get_face_direction(face));
68 faces[i].camera.set_up_direction(TextureCube::get_t_direction(face));
69 faces[i].camera.set_field_of_view(Geometry::Angle<float>::right());
70 faces[i].camera.set_aspect_ratio(1);
71 faces[i].camera.set_depth_clip(0.1, 100);
74 irradiance.storage(f, size/4, 1);
75 irradiance_fbo.set_format((COLOR_ATTACHMENT,f));
76 irradiance_fbo.attach_layered(COLOR_ATTACHMENT, irradiance);
80 specular_fbos.resize(l-1);
81 for(unsigned i=1; i<l; ++i)
83 specular_fbos[i-1].set_format((COLOR_ATTACHMENT,f));
84 specular_fbos[i-1].attach_layered(COLOR_ATTACHMENT, env_tex, i);
87 LinAl::Matrix<float, 3, 3> face_matrices[6];
88 for(unsigned i=0; i<6; ++i)
90 GL::TextureCubeFace face = GL::TextureCube::enumerate_faces(i);
91 GL::Vector3 columns[3];
92 columns[0] = GL::TextureCube::get_s_direction(face);
93 columns[1] = GL::TextureCube::get_t_direction(face);
94 columns[2] = GL::TextureCube::get_face_direction(face);
95 face_matrices[i] = LinAl::Matrix<float, 3, 3>::from_columns(columns);
98 prefilter_shdata.uniform_array("faces", 6, &face_matrices[0]);
99 prefilter_shdata.uniform("n_samples", 128);
100 prefilter_shdata.uniform("roughness", 1.0f);
103 shdata.uniform("env_world_matrix", LinAl::SquareMatrix<float, 3>::identity());
106 void EnvironmentMap::set_depth_clip(float n, float f)
108 for(unsigned i=0; i<6; ++i)
109 faces[i].camera.set_depth_clip(n, f);
112 void EnvironmentMap::set_update_interval(unsigned i)
115 update_delay = min(update_delay, update_interval-1);
118 void EnvironmentMap::queue_update()
123 void EnvironmentMap::setup_frame(Renderer &renderer)
129 renderable.setup_frame(renderer);
137 update_delay = update_interval-1;
138 environment.setup_frame(renderer);
140 const Matrix *matrix = renderable.get_matrix();
144 Renderer::Push push(renderer);
145 Renderer::Exclude exclude1(renderer, renderable);
146 Renderer::Exclude exclude2(renderer, *this);
148 Vector3 center = matrix->column(3).slice<3>(0);
150 for(unsigned i=0; i<6; ++i)
152 faces[i].camera.set_position(center);
153 renderer.set_framebuffer(&faces[i].fbo);
155 renderer.set_camera(faces[i].camera);
156 renderer.render(environment);
159 renderer.set_framebuffer(&irradiance_fbo);
160 renderer.set_shader_program(&irradiance_shprog, &prefilter_shdata);
161 renderer.set_texture("environment_map", &env_tex, &sampler);
162 fullscreen_mesh.draw(renderer);
164 renderer.set_shader_program(&specular_shprog);
165 for(unsigned i=0; i<specular_fbos.size(); ++i)
167 prefilter_shdata.uniform("roughness", 1.0f-sqrt(1.0f-static_cast<float>(i+1)/specular_fbos.size()));
168 renderer.set_framebuffer(&specular_fbos[i]);
169 fullscreen_mesh.draw(renderer);
173 void EnvironmentMap::finish_frame()
178 renderable.finish_frame();
179 environment.finish_frame();
183 void EnvironmentMap::render(Renderer &renderer, Tag tag) const
185 if(!enabled_passes.count(tag))
186 return renderer.render(renderable, tag);
188 Renderer::Push _push_rend(renderer);
190 renderer.set_texture("environment_map", &env_tex, &mip_sampler);
191 renderer.set_texture("irradiance_map", &irradiance, &sampler);
192 renderer.add_shader_data(shdata);
193 renderer.render(renderable, tag);
196 void EnvironmentMap::set_debug_name(const string &name)
199 env_tex.set_debug_name(name+"/environment.texcb");
200 depth_buf.set_debug_name(name+"/environment_depth.rbuf");
201 static const char *face_names[] = { "X+", "X-", "Y+", "Y-", "Z+", "Z-" };
202 for(unsigned i=0; i<6; ++i)
204 faces[i].fbo.set_debug_name(format("%s [FBO:%s]", name, face_names[i]));
205 faces[i].camera.set_debug_name(format("%s/%s.camera", name, face_names[i]));
208 irradiance.set_debug_name(name+"/irradiance.texcb");
209 irradiance_fbo.set_debug_name(name+" [FBO:irradiance]");
210 for(unsigned i=0; i<specular_fbos.size(); ++i)
211 specular_fbos[i].set_debug_name(format("%s [FBO:specular_mip%d]", name, i+1));
212 prefilter_shdata.set_debug_name(name+" [UBO:prefilter]");
213 shdata.set_debug_name(name+" [UBO]");