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