]> git.tdb.fi Git - libs/gl.git/blob - source/effects/ambientocclusion.cpp
Minor tweaks to MemoryAllocator
[libs/gl.git] / source / effects / ambientocclusion.cpp
1 #include <algorithm>
2 #include "ambientocclusion.h"
3 #include "blend.h"
4 #include "camera.h"
5 #include "mesh.h"
6 #include "renderer.h"
7 #include "resources.h"
8 #include "texture2d.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h):
16         rotate_lookup(get_or_create_rotate_lookup()),
17         occlude_target(w, h, (COLOR_ATTACHMENT,R8)),
18         occlude_shader(Resources::get_global().get<Program>("_ambientocclusion_occlude.glsl.shader")),
19         combine_shader(Resources::get_global().get<Program>("_ambientocclusion_combine.glsl.shader")),
20         quad(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
21         linear_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
22         nearest_clamp_sampler(Resources::get_global().get<Sampler>("_nearest_clamp.samp")),
23         nearest_sampler(Resources::get_global().get<Sampler>("_nearest.samp"))
24 {
25         set_n_samples(16);
26         set_occlusion_radius(0.5f);
27         set_darkness(1.0f);
28         set_edge_depth_threshold(0.1f);
29 }
30
31 const Texture2D &AmbientOcclusion::get_or_create_rotate_lookup()
32 {
33         Resources &resources = Resources::get_global();
34
35         static const string name = "_ambientocclusion_rotate.tex";
36         Texture2D *rotate_lookup = resources.find<Texture2D>(name);
37         if(rotate_lookup)
38                 return *rotate_lookup;
39
40         rotate_lookup = new Texture2D;
41         rotate_lookup->storage(RGBA8, 4, 4, 1);
42         resources.add(name, rotate_lookup);
43
44         unsigned char data[64];
45         for(unsigned i=0; i<16; ++i)
46         {
47                 Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(i*7/16.0f);
48                 unsigned char c = (cos(a)*0.5f+0.5f)*255;
49                 unsigned char s = (sin(a)*0.5f+0.5f)*255;
50                 data[i*4  ] = c;
51                 data[i*4+1] = s;
52                 data[i*4+2] = 255-s;
53                 data[i*4+3] = ((i+i/4)%2)*255;
54         }
55         rotate_lookup->image(0, data);
56
57         return *rotate_lookup;
58 }
59
60 float AmbientOcclusion::radical_inverse(unsigned n)
61 {
62         unsigned inv = ((n&0x55)<<1) | ((n&0xAA)>>1);
63         inv = ((inv&0x33)<<2) | ((inv&0xCC)>>2);
64         inv = ((inv&0x0F)<<4) | ((inv&0xF0)>>4);
65         return inv/256.0f;
66 }
67
68 void AmbientOcclusion::set_n_samples(unsigned n)
69 {
70         if(n<1 || n>128)
71                 throw out_of_range("AmbientOcclusion::set_n_samples");
72
73         vector<Vector3> sample_points(n);
74         for(unsigned i=0; i<n; ++i)
75         {
76                 float r = static_cast<float>(i)/n;
77                 float z = sqrt(1.0f-r*r);
78                 float d = radical_inverse(i);
79                 Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(d);
80                 sample_points[i] = Vector3(cos(a)*r, sin(a)*r, z)*(0.1f+0.9f*d*d);
81         }
82         shdata.uniform3_array("sample_points", n, &sample_points[0].x);
83         shdata.uniform("n_samples", static_cast<int>(n));
84 }
85
86 void AmbientOcclusion::set_occlusion_radius(float r)
87 {
88         shdata.uniform("occlusion_radius", r);
89 }
90
91 void AmbientOcclusion::set_darkness(float darkness)
92 {
93         shdata.uniform("darkness", darkness);
94 }
95
96 void AmbientOcclusion::set_edge_depth_threshold(float edt)
97 {
98         shdata.uniform("edge_depth_threshold", edt);
99 }
100
101 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
102 {
103         const Framebuffer *out_fbo = renderer.get_framebuffer();
104
105         Renderer::Push push(renderer);
106         renderer.set_texture("source", &color, &nearest_clamp_sampler);
107         renderer.set_texture("depth", &depth, &nearest_clamp_sampler);
108         renderer.set_texture("occlusion", &occlude_target.get_target_texture(COLOR_ATTACHMENT), &linear_sampler);
109         renderer.set_texture("rotate", &rotate_lookup, &nearest_sampler);
110         renderer.set_shader_program(&occlude_shader, &shdata);
111
112         renderer.set_pipeline_key(this);
113         renderer.set_framebuffer(&occlude_target.get_framebuffer());
114         renderer.clear(0);
115         quad.draw(renderer);
116
117         renderer.set_pipeline_key(this, 1);
118         renderer.set_framebuffer(out_fbo);
119         renderer.clear(0);
120         renderer.set_shader_program(&combine_shader);
121         quad.draw(renderer);
122 }
123
124 void AmbientOcclusion::set_debug_name(const string &name)
125 {
126 #ifdef DEBUG
127         occlude_target.set_debug_name(name+" [RT]");
128         shdata.set_debug_name(name+" [UBO]");
129 #else
130         (void)name;
131 #endif
132 }
133
134
135 AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
136 {
137         RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
138         ao->set_n_samples(n_samples);
139         ao->set_occlusion_radius(occlusion_radius);
140         ao->set_darkness(darkness);
141         ao->set_edge_depth_threshold(edge_depth_threshold);
142         return ao.release();
143 }
144
145
146 AmbientOcclusion::Template::Loader::Loader(Template &t):
147         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
148 {
149         add("darkness", &Template::darkness);
150         add("edge_depth_threshold", &Template::edge_depth_threshold);
151         add("occlusion_radius", &Template::occlusion_radius);
152         add("samples", &Template::n_samples);
153 }
154
155 } // namespace GL
156 } // namespace Msp