]> git.tdb.fi Git - libs/gl.git/blob - source/effects/ambientocclusion.cpp
Fix use of camera matrices in the ambient occlusion effect
[libs/gl.git] / source / effects / ambientocclusion.cpp
1 #include <algorithm>
2 #include "ambientocclusion.h"
3 #include "blend.h"
4 #include "camera.h"
5 #include "renderer.h"
6 #include "resources.h"
7 #include "shader.h"
8 #include "tests.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
16         occlude_target(w, h, (RENDER_COLOR,R8)),
17         occlude_shader(Resources::get_global().get<Program>("_ambientocclusion_occlude.glsl.shader")),
18         combine_shader(Resources::get_global().get<Program>("_ambientocclusion_combine.glsl.shader")),
19         quad(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
20         linear_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
21         nearest_sampler(Resources::get_global().get<Sampler>("_nearest_clamp.samp"))
22 {
23         unsigned seed = 1;
24         rotate_lookup.storage(RGBA8, 4, 4, 1);
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*4  ] = c;
32                 data[i*4+1] = s;
33                 data[i*4+2] = 255-s;
34                 data[i*4+3] = ((i+i/4)%2)*255;
35         }
36         rotate_lookup.image(0, data);
37
38         set_n_samples(16);
39         set_occlusion_radius(0.5f);
40         set_darkness(1.0f);
41         set_edge_depth_threshold(0.1f);
42 }
43
44 float AmbientOcclusion::random(unsigned &seed)
45 {
46         static const unsigned modulus = (1U<<31)-1;
47         seed = (static_cast<UInt64>(seed)*48271)%modulus;  // minstd
48         return static_cast<float>(seed)/(modulus-1);
49 }
50
51 void AmbientOcclusion::set_n_samples(unsigned n)
52 {
53         if(n<1 || n>32)
54                 throw out_of_range("AmbientOcclusion::set_n_samples");
55
56         unsigned seed = 1;
57         float radius_divisor = (n-1)*(n-1);
58         Vector3 sample_points[32];
59         for(unsigned i=0; i<n; ++i)
60         {
61                 Vector3 v(random(seed)-0.5f, random(seed)-0.5f, random(seed)-0.5f);
62                 sample_points[i] = normalize(v)*(0.1f+0.9f*i*i/radius_divisor);
63         }
64         shdata.uniform3_array("sample_points", n, &sample_points[0].x);
65         shdata.uniform("n_samples", static_cast<int>(n));
66 }
67
68 void AmbientOcclusion::set_occlusion_radius(float r)
69 {
70         shdata.uniform("occlusion_radius", r);
71 }
72
73 void AmbientOcclusion::set_darkness(float darkness)
74 {
75         shdata.uniform("darkness", darkness);
76 }
77
78 void AmbientOcclusion::set_edge_depth_threshold(float edt)
79 {
80         shdata.uniform("edge_depth_threshold", edt);
81 }
82
83 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
84 {
85         Renderer::Push push(renderer);
86         renderer.set_texture("source", &color, &nearest_sampler);
87         renderer.set_texture("depth", &depth, &nearest_sampler);
88         renderer.set_texture("occlusion", &occlude_target.get_target_texture(RENDER_COLOR), &linear_sampler);
89         renderer.set_texture("rotate", &rotate_lookup, &nearest_sampler);
90         renderer.set_shader_program(&occlude_shader, &shdata);
91
92         {
93                 BindRestore bind_fbo(occlude_target.get_framebuffer());
94                 quad.draw(renderer);
95         }
96
97         renderer.set_shader_program(&combine_shader);
98         quad.draw(renderer);
99 }
100
101
102 AmbientOcclusion::Template::Template():
103         n_samples(16),
104         occlusion_radius(0.5f),
105         darkness(1.0f),
106         edge_depth_threshold(0.1f)
107 { }
108
109 AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
110 {
111         RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
112         ao->set_n_samples(n_samples);
113         ao->set_occlusion_radius(occlusion_radius);
114         ao->set_darkness(darkness);
115         ao->set_edge_depth_threshold(edge_depth_threshold);
116         return ao.release();
117 }
118
119
120 AmbientOcclusion::Template::Loader::Loader(Template &t):
121         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
122 {
123         add("darkness", &Template::darkness);
124         add("edge_depth_threshold", &Template::edge_depth_threshold);
125         add("occlusion_radius", &Template::occlusion_radius);
126         add("samples", &Template::n_samples);
127 }
128
129 } // namespace GL
130 } // namespace Msp