]> git.tdb.fi Git - libs/gl.git/blob - source/effects/environmentmap.cpp
Consider all attachments when determining Framebuffer's size
[libs/gl.git] / source / effects / environmentmap.cpp
1 #include <algorithm>
2 #include <cmath>
3 #include "environmentmap.h"
4 #include "mesh.h"
5 #include "renderer.h"
6 #include "resources.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e):
14         Effect(r),
15         environment(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"))
21 {
22         init(s, RGB8, 1);
23 }
24
25 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, Renderable &r, Renderable &e):
26         Effect(r),
27         environment(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"))
33 {
34         init(s, f, 1);
35 }
36
37 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable &r, Renderable &e):
38         Effect(r),
39         environment(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"))
45 {
46         init(s, f, l);
47 }
48
49 void EnvironmentMap::init(unsigned s, PixelFormat f, unsigned l)
50 {
51         if(!l || (1U<<(l-1))>=s)
52                 throw invalid_argument("EnvironmentMap::EnvironmentMap");
53
54         size = s;
55         rendered = false;
56         update_interval = 1;
57         update_delay = 0;
58
59         env_tex.storage(f, size, l);
60         depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
61         for(unsigned i=0; i<6; ++i)
62         {
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);
72         }
73
74         irradiance.storage(f, size/4, 1);
75         irradiance_fbo.set_format((COLOR_ATTACHMENT,f));
76         irradiance_fbo.attach_layered(COLOR_ATTACHMENT, irradiance);
77
78         if(l>1)
79         {
80                 specular_fbos.resize(l-1);
81                 for(unsigned i=1; i<l; ++i)
82                 {
83                         specular_fbos[i-1].set_format((COLOR_ATTACHMENT,f));
84                         specular_fbos[i-1].attach_layered(COLOR_ATTACHMENT, env_tex, i);
85                 }
86
87                 LinAl::Matrix<float, 3, 3> face_matrices[6];
88                 for(unsigned i=0; i<6; ++i)
89                 {
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);
96                 }
97
98                 prefilter_shdata.uniform_array("faces", 6, &face_matrices[0]);
99                 prefilter_shdata.uniform("n_samples", 128);
100                 prefilter_shdata.uniform("roughness", 1.0f);
101         }
102
103         shdata.uniform("env_world_matrix", LinAl::SquareMatrix<float, 3>::identity());
104 }
105
106 void EnvironmentMap::set_depth_clip(float n, float f)
107 {
108         for(unsigned i=0; i<6; ++i)
109                 faces[i].camera.set_depth_clip(n, f);
110 }
111
112 void EnvironmentMap::set_update_interval(unsigned i)
113 {
114         update_interval = i;
115         update_delay = min(update_delay, update_interval-1);
116 }
117
118 void EnvironmentMap::queue_update()
119 {
120         update_delay = 0;
121 }
122
123 void EnvironmentMap::setup_frame(Renderer &renderer)
124 {
125         if(rendered)
126                 return;
127
128         rendered = true;
129         renderable.setup_frame(renderer);
130
131         if(update_delay)
132         {
133                 if(update_interval)
134                         --update_delay;
135                 return;
136         }
137         update_delay = update_interval-1;
138         environment.setup_frame(renderer);
139
140         const Matrix *matrix = renderable.get_matrix();
141         if(!matrix)
142                 return;
143
144         Renderer::Push push(renderer);
145         Renderer::Exclude exclude1(renderer, renderable);
146         Renderer::Exclude exclude2(renderer, *this);
147
148         Vector3 center = matrix->column(3).slice<3>(0);
149
150         for(unsigned i=0; i<6; ++i)
151         {
152                 faces[i].camera.set_position(center);
153                 renderer.set_framebuffer(&faces[i].fbo);
154                 renderer.clear();
155                 renderer.set_camera(faces[i].camera);
156                 renderer.render(environment);
157         }
158
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);
163
164         renderer.set_shader_program(&specular_shprog);
165         for(unsigned i=0; i<specular_fbos.size(); ++i)
166         {
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);
170         }
171 }
172
173 void EnvironmentMap::finish_frame()
174 {
175         if(rendered)
176         {
177                 rendered = false;
178                 renderable.finish_frame();
179                 environment.finish_frame();
180         }
181 }
182
183 void EnvironmentMap::render(Renderer &renderer, Tag tag) const
184 {
185         if(!enabled_passes.count(tag))
186                 return renderer.render(renderable, tag);
187
188         Renderer::Push _push_rend(renderer);
189
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);
194 }
195
196 void EnvironmentMap::set_debug_name(const string &name)
197 {
198 #ifdef DEBUG
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)
203         {
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]));
206         }
207
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]");
214 #else
215         (void)name;
216 #endif
217 }
218
219 } // namespace GL
220 } // namespace Msp