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