3 #include "environmentmap.h"
13 EnvironmentMap::EnvironmentMap(Resources &resources, unsigned s, Renderable &r, Renderable &e):
17 sampler(resources.get<Sampler>("_linear_clamp.samp")),
22 env_tex.storage(RGB8, size, 1);
23 depth_buf.storage(DEPTH_COMPONENT32F, size, size);
24 for(unsigned i=0; i<6; ++i)
26 fbo[i].attach(COLOR_ATTACHMENT0, env_tex, TextureCube::enumerate_faces(i), 0);
27 fbo[i].attach(DEPTH_ATTACHMENT, depth_buf);
28 fbo[i].require_complete();
31 camera.set_field_of_view(Geometry::Angle<float>::right());
32 camera.set_aspect_ratio(1);
33 camera.set_depth_clip(0.1, 100);
36 void EnvironmentMap::set_depth_clip(float n, float f)
38 camera.set_depth_clip(n, f);
41 void EnvironmentMap::set_update_interval(unsigned i)
44 update_delay = min(update_delay, update_interval-1);
47 void EnvironmentMap::queue_update()
52 void EnvironmentMap::setup_frame(Renderer &renderer)
58 renderable.setup_frame(renderer);
66 update_delay = update_interval-1;
67 environment.setup_frame(renderer);
69 const Matrix *matrix = renderable.get_matrix();
73 Renderer::Push push(renderer);
74 Renderer::Exclude exclude1(renderer, renderable);
75 Renderer::Exclude exclude2(renderer, *this);
77 camera.set_position(matrix->column(3).slice<3>(0));
79 BindRestore bind_fbo(fbo[0]);
80 for(unsigned i=0; i<6; ++i)
82 TextureCubeFace face = TextureCube::enumerate_faces(i);
85 camera.set_look_direction(TextureCube::get_face_direction(face));
86 camera.set_up_direction(TextureCube::get_t_direction(face));
87 renderer.set_camera(camera);
88 renderer.render(environment);
92 void EnvironmentMap::finish_frame()
97 renderable.finish_frame();
98 environment.finish_frame();
102 void EnvironmentMap::render(Renderer &renderer, const Tag &tag) const
104 if(!enabled_passes.count(tag))
105 return renderer.render(renderable, tag);
107 Renderer::Push _push_rend(renderer);
109 unsigned unit = renderer.allocate_effect_texunit();
110 shdata.uniform("environment_map", static_cast<int>(unit));
111 Bind _bind_sampler(sampler, unit);
112 Bind _bind_env(env_tex, unit);
114 const Matrix &camera_matrix = renderer.get_camera()->get_object_matrix();
115 shdata.uniform("env_eye_matrix", camera_matrix.block<3, 3>(0, 0));
117 renderer.add_shader_data(shdata);
118 renderer.render(renderable, tag);