]> git.tdb.fi Git - libs/gl.git/blob - source/effects/environmentmap.cpp
Check the flat qualifier from the correct member
[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 #include "texunit.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e):
15         Effect(r),
16         environment(e),
17         irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
18         specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
19         fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
20         sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
21         mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
22 {
23         init(s, RGB8, 1);
24 }
25
26 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, Renderable &r, Renderable &e):
27         Effect(r),
28         environment(e),
29         irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
30         specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
31         fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
32         sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
33         mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
34 {
35         init(s, f, 1);
36 }
37
38 EnvironmentMap::EnvironmentMap(unsigned s, PixelFormat f, unsigned l, Renderable &r, Renderable &e):
39         Effect(r),
40         environment(e),
41         irradiance_shprog(Resources::get_global().get<Program>("_envmap_irradiance.glsl.shader")),
42         specular_shprog(Resources::get_global().get<Program>("_envmap_specular.glsl.shader")),
43         fullscreen_mesh(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
44         sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
45         mip_sampler(Resources::get_global().get<Sampler>("_mip_clamp.samp"))
46 {
47         init(s, f, l);
48 }
49
50 void EnvironmentMap::init(unsigned s, PixelFormat f, unsigned l)
51 {
52         if(!l || (1U<<(l-1))>=s)
53                 throw invalid_argument("EnvironmentMap::EnvironmentMap");
54
55         size = s;
56         rendered = false;
57         update_interval = 1;
58         update_delay = 0;
59
60         env_tex.storage(f, size, l);
61         depth_buf.storage(DEPTH_COMPONENT32F, size, size);
62         for(unsigned i=0; i<6; ++i)
63         {
64                 TextureCubeFace face = TextureCube::enumerate_faces(i);
65                 faces[i].fbo.attach(COLOR_ATTACHMENT0, env_tex, face, 0);
66                 faces[i].fbo.attach(DEPTH_ATTACHMENT, depth_buf);
67                 faces[i].fbo.require_complete();
68                 faces[i].camera.set_look_direction(TextureCube::get_face_direction(face));
69                 faces[i].camera.set_up_direction(TextureCube::get_t_direction(face));
70                 faces[i].camera.set_field_of_view(Geometry::Angle<float>::right());
71                 faces[i].camera.set_aspect_ratio(1);
72                 faces[i].camera.set_depth_clip(0.1, 100);
73         }
74
75         irradiance.storage(f, size/4, 1);
76         irradiance_fbo.attach_layered(COLOR_ATTACHMENT0, irradiance);
77
78         if(l>1)
79         {
80                 specular_fbos.resize(l-1);
81                 for(unsigned i=1; i<l; ++i)
82                         specular_fbos[i-1].attach_layered(COLOR_ATTACHMENT0, env_tex, i);
83
84                 LinAl::Matrix<float, 3, 3> face_matrices[6];
85                 for(unsigned i=0; i<6; ++i)
86                 {
87                         GL::TextureCubeFace face = GL::TextureCube::enumerate_faces(i);
88                         GL::Vector3 columns[3];
89                         columns[0] = GL::TextureCube::get_s_direction(face);
90                         columns[1] = GL::TextureCube::get_t_direction(face);
91                         columns[2] = GL::TextureCube::get_face_direction(face);
92                         face_matrices[i] = LinAl::Matrix<float, 3, 3>::from_columns(columns);
93                 }
94
95                 prefilter_shdata.uniform_array("faces", 6, &face_matrices[0]);
96                 prefilter_shdata.uniform("n_samples", 128);
97                 prefilter_shdata.uniform("roughness", 1.0f);
98         }
99
100         shdata.uniform("env_world_matrix", LinAl::SquareMatrix<float, 3>::identity());
101 }
102
103 void EnvironmentMap::set_depth_clip(float n, float f)
104 {
105         for(unsigned i=0; i<6; ++i)
106                 faces[i].camera.set_depth_clip(n, f);
107 }
108
109 void EnvironmentMap::set_update_interval(unsigned i)
110 {
111         update_interval = i;
112         update_delay = min(update_delay, update_interval-1);
113 }
114
115 void EnvironmentMap::queue_update()
116 {
117         update_delay = 0;
118 }
119
120 void EnvironmentMap::setup_frame(Renderer &renderer)
121 {
122         if(rendered)
123                 return;
124
125         rendered = true;
126         renderable.setup_frame(renderer);
127
128         if(update_delay)
129         {
130                 if(update_interval)
131                         --update_delay;
132                 return;
133         }
134         update_delay = update_interval-1;
135         environment.setup_frame(renderer);
136
137         const Matrix *matrix = renderable.get_matrix();
138         if(!matrix)
139                 return;
140
141         Renderer::Push push(renderer);
142         Renderer::Exclude exclude1(renderer, renderable);
143         Renderer::Exclude exclude2(renderer, *this);
144
145         Vector3 center = matrix->column(3).slice<3>(0);
146
147         BindRestore bind_fbo(faces[0].fbo);
148         for(unsigned i=0; i<6; ++i)
149         {
150                 faces[i].camera.set_position(center);
151                 faces[i].fbo.bind();
152                 faces[i].fbo.clear();
153                 renderer.set_camera(faces[i].camera);
154                 renderer.render(environment);
155         }
156
157         irradiance_fbo.bind();
158         renderer.set_shader_program(&irradiance_shprog, &prefilter_shdata);
159         renderer.set_texture("environment_map", &env_tex, &sampler);
160         fullscreen_mesh.draw(renderer);
161
162         renderer.set_shader_program(&specular_shprog);
163         for(unsigned i=0; i<specular_fbos.size(); ++i)
164         {
165                 prefilter_shdata.uniform("roughness", 1.0f-sqrt(1.0f-static_cast<float>(i+1)/specular_fbos.size()));
166                 specular_fbos[i].bind();
167                 fullscreen_mesh.draw(renderer);
168         }
169 }
170
171 void EnvironmentMap::finish_frame()
172 {
173         if(rendered)
174         {
175                 rendered = false;
176                 renderable.finish_frame();
177                 environment.finish_frame();
178         }
179 }
180
181 void EnvironmentMap::render(Renderer &renderer, Tag tag) const
182 {
183         if(!enabled_passes.count(tag))
184                 return renderer.render(renderable, tag);
185
186         Renderer::Push _push_rend(renderer);
187
188         renderer.set_texture("environment_map", &env_tex, &mip_sampler);
189         renderer.set_texture("irradiance_map", &irradiance, &sampler);
190         renderer.add_shader_data(shdata);
191         renderer.render(renderable, tag);
192 }
193
194 void EnvironmentMap::set_debug_name(const string &name)
195 {
196 #ifdef DEBUG
197         env_tex.set_debug_name(name+"/environment.texcb");
198         depth_buf.set_debug_name(name+"/environment_depth.rbuf");
199         static const char *face_names[] = { "X+", "X-", "Y+", "Y-", "Z+", "Z-" };
200         for(unsigned i=0; i<6; ++i)
201         {
202                 faces[i].fbo.set_debug_name(format("%s [FBO:%s]", name, face_names[i]));
203                 faces[i].camera.set_debug_name(format("%s/%s.camera", name, face_names[i]));
204         }
205
206         irradiance.set_debug_name(name+"/irradiance.texcb");
207         irradiance_fbo.set_debug_name(name+" [FBO:irradiance]");
208         for(unsigned i=0; i<specular_fbos.size(); ++i)
209                 specular_fbos[i].set_debug_name(format("%s [FBO:specular_mip%d]", name, i+1));
210         prefilter_shdata.set_debug_name(name+" [UBO:prefilter]");
211         shdata.set_debug_name(name+" [UBO]");
212 #else
213         (void)name;
214 #endif
215 }
216
217 } // namespace GL
218 } // namespace Msp