]> git.tdb.fi Git - libs/gl.git/blob - source/effects/ambientocclusion.cpp
Store the ambient occlusion rotate lookup texture in resources
[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         rotate_lookup(get_or_create_rotate_lookup()),
17         occlude_target(w, h, (RENDER_COLOR,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.tex2d";
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 seed = 1;
45         unsigned char data[64];
46         for(unsigned i=0; i<16; ++i)
47         {
48                 Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(random(seed));
49                 unsigned char c = (cos(a)*0.5f+0.5f)*255;
50                 unsigned char s = (sin(a)*0.5f+0.5f)*255;
51                 data[i*4  ] = c;
52                 data[i*4+1] = s;
53                 data[i*4+2] = 255-s;
54                 data[i*4+3] = ((i+i/4)%2)*255;
55         }
56         rotate_lookup->image(0, data);
57
58         return *rotate_lookup;
59 }
60
61 float AmbientOcclusion::random(unsigned &seed)
62 {
63         static const unsigned modulus = (1U<<31)-1;
64         seed = (static_cast<UInt64>(seed)*48271)%modulus;  // minstd
65         return static_cast<float>(seed)/(modulus-1);
66 }
67
68 void AmbientOcclusion::set_n_samples(unsigned n)
69 {
70         if(n<1 || n>32)
71                 throw out_of_range("AmbientOcclusion::set_n_samples");
72
73         unsigned seed = 1;
74         float radius_divisor = (n-1)*(n-1);
75         Vector3 sample_points[32];
76         for(unsigned i=0; i<n; ++i)
77         {
78                 Vector3 v(random(seed)-0.5f, random(seed)-0.5f, random(seed)-0.5f);
79                 sample_points[i] = normalize(v)*(0.1f+0.9f*i*i/radius_divisor);
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         Renderer::Push push(renderer);
103         renderer.set_texture("source", &color, &nearest_clamp_sampler);
104         renderer.set_texture("depth", &depth, &nearest_clamp_sampler);
105         renderer.set_texture("occlusion", &occlude_target.get_target_texture(RENDER_COLOR), &linear_sampler);
106         renderer.set_texture("rotate", &rotate_lookup, &nearest_sampler);
107         renderer.set_shader_program(&occlude_shader, &shdata);
108
109         {
110                 BindRestore bind_fbo(occlude_target.get_framebuffer());
111                 quad.draw(renderer);
112         }
113
114         renderer.set_shader_program(&combine_shader);
115         quad.draw(renderer);
116 }
117
118
119 AmbientOcclusion::Template::Template():
120         n_samples(16),
121         occlusion_radius(0.5f),
122         darkness(1.0f),
123         edge_depth_threshold(0.1f)
124 { }
125
126 AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
127 {
128         RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
129         ao->set_n_samples(n_samples);
130         ao->set_occlusion_radius(occlusion_radius);
131         ao->set_darkness(darkness);
132         ao->set_edge_depth_threshold(edge_depth_threshold);
133         return ao.release();
134 }
135
136
137 AmbientOcclusion::Template::Loader::Loader(Template &t):
138         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
139 {
140         add("darkness", &Template::darkness);
141         add("edge_depth_threshold", &Template::edge_depth_threshold);
142         add("occlusion_radius", &Template::occlusion_radius);
143         add("samples", &Template::n_samples);
144 }
145
146 } // namespace GL
147 } // namespace Msp