2 #include "ambientocclusion.h"
15 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
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_sampler(Resources::get_global().get<Sampler>("_nearest_clamp.samp"))
24 rotate_lookup.storage(RGBA8, 4, 4, 1);
25 unsigned char data[64];
26 for(unsigned i=0; i<16; ++i)
28 Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(random(seed));
29 unsigned char c = (cos(a)*0.5f+0.5f)*255;
30 unsigned char s = (sin(a)*0.5f+0.5f)*255;
34 data[i*4+3] = ((i+i/4)%2)*255;
36 rotate_lookup.image(0, data);
39 set_occlusion_radius(0.5f);
41 set_edge_depth_threshold(0.1f);
44 float AmbientOcclusion::random(unsigned &seed)
46 static const unsigned modulus = (1U<<31)-1;
47 seed = (static_cast<UInt64>(seed)*48271)%modulus; // minstd
48 return static_cast<float>(seed)/(modulus-1);
51 void AmbientOcclusion::set_n_samples(unsigned n)
54 throw out_of_range("AmbientOcclusion::set_n_samples");
57 float radius_divisor = (n-1)*(n-1);
58 Vector3 sample_points[32];
59 for(unsigned i=0; i<n; ++i)
61 Vector3 v(random(seed)-0.5f, random(seed)-0.5f, random(seed)-0.5f);
62 sample_points[i] = normalize(v)*(0.1f+0.9f*i*i/radius_divisor);
64 shdata.uniform3_array("sample_points", n, &sample_points[0].x);
65 shdata.uniform("n_samples", static_cast<int>(n));
68 void AmbientOcclusion::set_occlusion_radius(float r)
70 shdata.uniform("occlusion_radius", r);
73 void AmbientOcclusion::set_darkness(float darkness)
75 shdata.uniform("darkness", darkness);
78 void AmbientOcclusion::set_edge_depth_threshold(float edt)
80 shdata.uniform("edge_depth_threshold", edt);
83 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
85 Renderer::Push push(renderer);
86 renderer.set_texture("source", &color, &nearest_sampler);
87 renderer.set_texture("depth", &depth, &nearest_sampler);
88 renderer.set_texture("occlusion", &occlude_target.get_target_texture(RENDER_COLOR), &linear_sampler);
89 renderer.set_texture("rotate", &rotate_lookup, &nearest_sampler);
90 renderer.set_shader_program(&occlude_shader, &shdata);
93 BindRestore bind_fbo(occlude_target.get_framebuffer());
97 renderer.set_shader_program(&combine_shader);
102 AmbientOcclusion::Template::Template():
104 occlusion_radius(0.5f),
106 edge_depth_threshold(0.1f)
109 AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
111 RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
112 ao->set_n_samples(n_samples);
113 ao->set_occlusion_radius(occlusion_radius);
114 ao->set_darkness(darkness);
115 ao->set_edge_depth_threshold(edge_depth_threshold);
120 AmbientOcclusion::Template::Loader::Loader(Template &t):
121 DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
123 add("darkness", &Template::darkness);
124 add("edge_depth_threshold", &Template::edge_depth_threshold);
125 add("occlusion_radius", &Template::occlusion_radius);
126 add("samples", &Template::n_samples);