2 #include "ambientocclusion.h"
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"))
23 texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR), &linear_sampler);
26 rotate_lookup.storage(RGBA8, 4, 4, 1);
27 unsigned char data[64];
28 for(unsigned i=0; i<16; ++i)
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;
36 data[i*4+3] = ((i+i/4)%2)*255;
38 rotate_lookup.image(0, data);
40 texturing.attach(3, rotate_lookup, &nearest_sampler);
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());
49 set_occlusion_radius(0.5f);
51 set_edge_depth_threshold(0.1f);
54 float AmbientOcclusion::random(unsigned &seed)
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);
61 void AmbientOcclusion::set_n_samples(unsigned n)
64 throw out_of_range("AmbientOcclusion::set_n_samples");
67 float radius_divisor = (n-1)*(n-1);
68 Vector3 sample_points[32];
69 for(unsigned i=0; i<n; ++i)
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);
74 shdata.uniform3_array("sample_points", n, &sample_points[0].x);
75 shdata.uniform("n_samples", static_cast<int>(n));
78 void AmbientOcclusion::set_occlusion_radius(float r)
80 shdata.uniform("occlusion_radius", r);
83 void AmbientOcclusion::set_darkness(float darkness)
85 shdata.uniform("darkness", darkness);
88 void AmbientOcclusion::set_edge_depth_threshold(float edt)
90 shdata.uniform("edge_depth_threshold", edt);
93 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
95 texturing.attach(0, color, &nearest_sampler);
96 texturing.attach(1, depth, &nearest_sampler);
98 if(renderer.get_camera())
99 shdata.uniform("inverse_projection", invert(renderer.get_camera()->get_projection_matrix()));
101 Renderer::Push push(renderer);
102 renderer.set_texturing(&texturing);
103 renderer.set_shader_program(&occlude_shader, &shdata);
106 BindRestore bind_fbo(occlude_target.get_framebuffer());
110 renderer.set_shader_program(&combine_shader);
115 AmbientOcclusion::Template::Template():
117 occlusion_radius(0.5f),
119 edge_depth_threshold(0.1f)
122 AmbientOcclusion *AmbientOcclusion::Template::create(Resources &res, unsigned width, unsigned height) const
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);
133 AmbientOcclusion::Template::Loader::Loader(Template &t):
134 DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
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);