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