]> git.tdb.fi Git - libs/gl.git/blob - source/ambientocclusion.cpp
Improve the ambient occlusion postprocessor
[libs/gl.git] / source / ambientocclusion.cpp
1 #include <algorithm>
2 #include "ambientocclusion.h"
3 #include "blend.h"
4 #include "camera.h"
5 #include "renderer.h"
6 #include "shader.h"
7 #include "tests.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
15         occlude_target(w, h, (RENDER_COLOR,RGB)),
16         occlude_shader("ambientocclusion_occlude.glsl"),
17         combine_shader("ambientocclusion_combine.glsl"),
18         quad(get_fullscreen_quad())
19 {
20         texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR));
21
22         unsigned seed = 1;
23         rotate_lookup.storage(RGBA, 4, 4);
24         rotate_lookup.set_filter(NEAREST);
25         unsigned char data[64];
26         for(unsigned i=0; i<16; ++i)
27         {
28                 Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(random(seed));
29                 unsigned char c = (cos(a)*0.5f+0.5f)*255;
30                 unsigned char s = (sin(a)*0.5f+0.5f)*255;
31                 data[i*3  ] = c;
32                 data[i*3+1] = s;
33                 data[i*3+2] = 255-s;
34                 data[i*3+4] = ((i+i/4)%2)*255;
35         }
36         rotate_lookup.image(0, RGBA, UNSIGNED_BYTE, data);
37
38         texturing.attach(3, rotate_lookup);
39
40         shdata.uniform("source", 0);
41         shdata.uniform("depth", 1);
42         shdata.uniform("occlusion", 2);
43         shdata.uniform("rotate", 3);
44         shdata.uniform("inverse_projection", Matrix());
45
46         set_n_samples(16);
47         set_occlusion_radius(0.5f);
48         set_darkness(1.0f);
49 }
50
51 float AmbientOcclusion::random(unsigned &seed)
52 {
53         static const unsigned modulus = (1U<<31)-1;
54         seed = (seed*48271)%modulus;  // minstd
55         return static_cast<float>(seed)/(modulus-1);
56 }
57
58 void AmbientOcclusion::set_n_samples(unsigned n)
59 {
60         if(n<1 || n>32)
61                 throw out_of_range("AmbientOcclusion::set_n_samples");
62
63         unsigned seed = 1;
64         float radius_divisor = (n-1)*(n-1);
65         Vector3 sample_points[32];
66         for(unsigned i=0; i<n; ++i)
67         {
68                 Vector3 v(random(seed)-0.5f, random(seed)-0.5f, random(seed)-0.5f);
69                 sample_points[i] = normalize(v)*(0.1f+0.9f*i*i/radius_divisor);
70         }
71         shdata.uniform3_array("sample_points", n, &sample_points[0].x);
72         shdata.uniform("n_samples", static_cast<int>(n));
73 }
74
75 void AmbientOcclusion::set_occlusion_radius(float r)
76 {
77         shdata.uniform("occlusion_radius", r);
78 }
79
80 void AmbientOcclusion::set_depth_ratio(float)
81 {
82 }
83
84 void AmbientOcclusion::set_darkness(float darkness)
85 {
86         shdata.uniform("darkness", darkness);
87 }
88
89 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
90 {
91         texturing.attach(0, color);
92         texturing.attach(1, depth);
93
94         if(renderer.get_camera())
95                 shdata.uniform("inverse_projection", invert(renderer.get_camera()->get_projection_matrix()));
96
97         Renderer::Push push(renderer);
98         renderer.set_texturing(&texturing);
99         renderer.set_shader_program(&occlude_shader, &shdata);
100
101         {
102                 BindRestore bind_fbo(occlude_target.get_framebuffer());
103                 quad.draw(renderer);
104         }
105
106         renderer.set_shader_program(&combine_shader);
107         quad.draw(renderer);
108 }
109
110 } // namespace GL
111 } // namespace Msp