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