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"))
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);
38 shdata.uniform("inverse_projection", Matrix());
41 set_occlusion_radius(0.5f);
43 set_edge_depth_threshold(0.1f);
46 float AmbientOcclusion::random(unsigned &seed)
48 static const unsigned modulus = (1U<<31)-1;
49 seed = (static_cast<UInt64>(seed)*48271)%modulus; // minstd
50 return static_cast<float>(seed)/(modulus-1);
53 void AmbientOcclusion::set_n_samples(unsigned n)
56 throw out_of_range("AmbientOcclusion::set_n_samples");
59 float radius_divisor = (n-1)*(n-1);
60 Vector3 sample_points[32];
61 for(unsigned i=0; i<n; ++i)
63 Vector3 v(random(seed)-0.5f, random(seed)-0.5f, random(seed)-0.5f);
64 sample_points[i] = normalize(v)*(0.1f+0.9f*i*i/radius_divisor);
66 shdata.uniform3_array("sample_points", n, &sample_points[0].x);
67 shdata.uniform("n_samples", static_cast<int>(n));
70 void AmbientOcclusion::set_occlusion_radius(float r)
72 shdata.uniform("occlusion_radius", r);
75 void AmbientOcclusion::set_darkness(float darkness)
77 shdata.uniform("darkness", darkness);
80 void AmbientOcclusion::set_edge_depth_threshold(float edt)
82 shdata.uniform("edge_depth_threshold", edt);
85 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
87 if(renderer.get_camera())
88 shdata.uniform("inverse_projection", invert(renderer.get_camera()->get_projection_matrix()));
90 Renderer::Push push(renderer);
91 renderer.set_texture("source", &color, &nearest_sampler);
92 renderer.set_texture("depth", &depth, &nearest_sampler);
93 renderer.set_texture("occlusion", &occlude_target.get_target_texture(RENDER_COLOR), &linear_sampler);
94 renderer.set_texture("rotate", &rotate_lookup, &nearest_sampler);
95 renderer.set_shader_program(&occlude_shader, &shdata);
98 BindRestore bind_fbo(occlude_target.get_framebuffer());
102 renderer.set_shader_program(&combine_shader);
107 AmbientOcclusion::Template::Template():
109 occlusion_radius(0.5f),
111 edge_depth_threshold(0.1f)
114 AmbientOcclusion *AmbientOcclusion::Template::create(Resources &res, unsigned width, unsigned height) const
116 RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(res, width/size_divisor, height/size_divisor);
117 ao->set_n_samples(n_samples);
118 ao->set_occlusion_radius(occlusion_radius);
119 ao->set_darkness(darkness);
120 ao->set_edge_depth_threshold(edge_depth_threshold);
125 AmbientOcclusion::Template::Loader::Loader(Template &t):
126 DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
128 add("darkness", &Template::darkness);
129 add("edge_depth_threshold", &Template::edge_depth_threshold);
130 add("occlusion_radius", &Template::occlusion_radius);
131 add("samples", &Template::n_samples);