]> git.tdb.fi Git - libs/gl.git/blob - source/environmentmap.cpp
Improve documentation for a number of classes
[libs/gl.git] / source / environmentmap.cpp
1 #include <cmath>
2 #include "environmentmap.h"
3 #include "renderer.h"
4 #include "texunit.h"
5
6 namespace Msp {
7 namespace GL {
8
9 EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e):
10         Effect(r),
11         size(s),
12         environment(e),
13         rendered(false)
14 {
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)
20         {
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();
24         }
25
26         // XXX Make the depth range configurable
27         camera.set_field_of_view(Geometry::Angle<float>::right());
28         camera.set_aspect(1);
29         camera.set_depth_clip(0.1, 100);
30
31 }
32
33 void EnvironmentMap::setup_frame() const
34 {
35         if(rendered)
36                 return;
37
38         rendered = true;
39         renderable.setup_frame();
40         environment.setup_frame();
41
42         const Matrix *matrix = renderable.get_matrix();
43         Vector3 position = (*matrix)*Vector3();
44         camera.set_position(position);
45         for(unsigned i=0; i<6; ++i)
46         {
47                 TextureCubeFace face = TextureCube::enumerate_faces(i);
48                 fbo[i].bind();
49                 fbo[i].clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
50                 camera.set_look_direction(env_tex.get_face_direction(face));
51                 camera.set_up_direction(env_tex.get_t_direction(face));
52                 Renderer env_renderer(&camera);
53                 env_renderer.exclude(renderable);
54                 env_renderer.exclude(*this);
55                 env_renderer.render(environment);
56         }
57         Framebuffer::unbind();
58 }
59
60 void EnvironmentMap::finish_frame() const
61 {
62         if(rendered)
63         {
64                 rendered = false;
65                 renderable.finish_frame();
66                 environment.finish_frame();
67         }
68 }
69
70 void EnvironmentMap::render(Renderer &renderer, const Tag &tag) const
71 {
72         if(!enabled_passes.count(tag))
73                 return renderer.render(renderable, tag);
74
75         Renderer::Push _push_rend(renderer);
76
77         unsigned unit = renderer.allocate_effect_texunit();
78         shdata.uniform("environment", static_cast<int>(unit));
79         Bind _bind_env(env_tex, unit);
80
81         const Matrix &view_matrix = renderer.get_camera()->get_matrix();
82         // XXX The camera should maybe have store its own object matrix
83         float env_mdata[9];
84         env_mdata[0] = view_matrix[0];
85         env_mdata[1] = view_matrix[4];
86         env_mdata[2] = view_matrix[8];
87         env_mdata[3] = view_matrix[1];
88         env_mdata[4] = view_matrix[5];
89         env_mdata[5] = view_matrix[9];
90         env_mdata[6] = view_matrix[2];
91         env_mdata[7] = view_matrix[6];
92         env_mdata[8] = view_matrix[10];
93         shdata.uniform_matrix3("env_eye_matrix", env_mdata);
94
95         renderer.add_shader_data(shdata);
96         renderer.render(renderable, tag);
97 }
98
99 } // namespace GL
100 } // namespace Msp