2 #include "environmentmap.h"
9 EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e):
15 env_tex.storage(RGB, size);
16 env_tex.set_wrap(CLAMP_TO_EDGE);
17 env_tex.set_min_filter(LINEAR);
18 depth_buf.storage(DEPTH_COMPONENT, size, size);
19 for(unsigned i=0; i<6; ++i)
21 fbo[i].attach(COLOR_ATTACHMENT0, env_tex, TextureCube::enumerate_faces(i), 0);
22 fbo[i].attach(DEPTH_ATTACHMENT, depth_buf);
23 fbo[i].require_complete();
26 // XXX Make the depth range configurable
27 camera.set_field_of_view(Geometry::Angle<float>::right());
29 camera.set_depth_clip(0.1, 100);
31 shdata.uniform("environment", 4);
34 void EnvironmentMap::setup_frame() const
40 renderable.setup_frame();
41 environment.setup_frame();
43 const Matrix *matrix = renderable.get_matrix();
44 Vector3 position = (*matrix)*Vector3();
45 camera.set_position(position);
46 for(unsigned i=0; i<6; ++i)
48 TextureCubeFace face = TextureCube::enumerate_faces(i);
50 fbo[i].clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
51 camera.set_look_direction(env_tex.get_face_direction(face));
52 camera.set_up_direction(env_tex.get_t_direction(face));
53 Renderer env_renderer(&camera);
54 env_renderer.exclude(renderable);
55 env_renderer.render(environment);
57 Framebuffer::unbind();
60 void EnvironmentMap::finish_frame() const
65 renderable.finish_frame();
66 environment.finish_frame();
70 void EnvironmentMap::render(Renderer &renderer, const Tag &tag) const
72 if(!enabled_passes.count(tag))
73 return renderer.render(renderable, tag);
75 const Matrix &view_matrix = renderer.get_camera()->get_matrix();
76 // XXX The camera should maybe have store its own object matrix
78 env_mdata[0] = view_matrix[0];
79 env_mdata[1] = view_matrix[4];
80 env_mdata[2] = view_matrix[8];
81 env_mdata[3] = view_matrix[1];
82 env_mdata[4] = view_matrix[5];
83 env_mdata[5] = view_matrix[9];
84 env_mdata[6] = view_matrix[2];
85 env_mdata[7] = view_matrix[6];
86 env_mdata[8] = view_matrix[10];
87 shdata.uniform_matrix3("env_eye_matrix", env_mdata);
91 Renderer::Push _push_rend(renderer);
92 renderer.add_shader_data(shdata);
93 renderer.render(renderable, tag);
95 env_tex.unbind_from(4);