]> git.tdb.fi Git - libs/gl.git/blob - source/effects/ambientocclusion.cpp
Use correct sampler for the ambient occlusion rotate lookup
[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_clamp_sampler(Resources::get_global().get<Sampler>("_nearest_clamp.samp")),
22         nearest_sampler(Resources::get_global().get<Sampler>("_nearest.samp"))
23 {
24         unsigned seed = 1;
25         rotate_lookup.storage(RGBA8, 4, 4, 1);
26         unsigned char data[64];
27         for(unsigned i=0; i<16; ++i)
28         {
29                 Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(random(seed));
30                 unsigned char c = (cos(a)*0.5f+0.5f)*255;
31                 unsigned char s = (sin(a)*0.5f+0.5f)*255;
32                 data[i*4  ] = c;
33                 data[i*4+1] = s;
34                 data[i*4+2] = 255-s;
35                 data[i*4+3] = ((i+i/4)%2)*255;
36         }
37         rotate_lookup.image(0, data);
38
39         set_n_samples(16);
40         set_occlusion_radius(0.5f);
41         set_darkness(1.0f);
42         set_edge_depth_threshold(0.1f);
43 }
44
45 float AmbientOcclusion::random(unsigned &seed)
46 {
47         static const unsigned modulus = (1U<<31)-1;
48         seed = (static_cast<UInt64>(seed)*48271)%modulus;  // minstd
49         return static_cast<float>(seed)/(modulus-1);
50 }
51
52 void AmbientOcclusion::set_n_samples(unsigned n)
53 {
54         if(n<1 || n>32)
55                 throw out_of_range("AmbientOcclusion::set_n_samples");
56
57         unsigned seed = 1;
58         float radius_divisor = (n-1)*(n-1);
59         Vector3 sample_points[32];
60         for(unsigned i=0; i<n; ++i)
61         {
62                 Vector3 v(random(seed)-0.5f, random(seed)-0.5f, random(seed)-0.5f);
63                 sample_points[i] = normalize(v)*(0.1f+0.9f*i*i/radius_divisor);
64         }
65         shdata.uniform3_array("sample_points", n, &sample_points[0].x);
66         shdata.uniform("n_samples", static_cast<int>(n));
67 }
68
69 void AmbientOcclusion::set_occlusion_radius(float r)
70 {
71         shdata.uniform("occlusion_radius", r);
72 }
73
74 void AmbientOcclusion::set_darkness(float darkness)
75 {
76         shdata.uniform("darkness", darkness);
77 }
78
79 void AmbientOcclusion::set_edge_depth_threshold(float edt)
80 {
81         shdata.uniform("edge_depth_threshold", edt);
82 }
83
84 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
85 {
86         Renderer::Push push(renderer);
87         renderer.set_texture("source", &color, &nearest_clamp_sampler);
88         renderer.set_texture("depth", &depth, &nearest_clamp_sampler);
89         renderer.set_texture("occlusion", &occlude_target.get_target_texture(RENDER_COLOR), &linear_sampler);
90         renderer.set_texture("rotate", &rotate_lookup, &nearest_sampler);
91         renderer.set_shader_program(&occlude_shader, &shdata);
92
93         {
94                 BindRestore bind_fbo(occlude_target.get_framebuffer());
95                 quad.draw(renderer);
96         }
97
98         renderer.set_shader_program(&combine_shader);
99         quad.draw(renderer);
100 }
101
102
103 AmbientOcclusion::Template::Template():
104         n_samples(16),
105         occlusion_radius(0.5f),
106         darkness(1.0f),
107         edge_depth_threshold(0.1f)
108 { }
109
110 AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
111 {
112         RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
113         ao->set_n_samples(n_samples);
114         ao->set_occlusion_radius(occlusion_radius);
115         ao->set_darkness(darkness);
116         ao->set_edge_depth_threshold(edge_depth_threshold);
117         return ao.release();
118 }
119
120
121 AmbientOcclusion::Template::Loader::Loader(Template &t):
122         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
123 {
124         add("darkness", &Template::darkness);
125         add("edge_depth_threshold", &Template::edge_depth_threshold);
126         add("occlusion_radius", &Template::occlusion_radius);
127         add("samples", &Template::n_samples);
128 }
129
130 } // namespace GL
131 } // namespace Msp