#include "ambientocclusion.h"
#include "blend.h"
#include "camera.h"
+#include "mesh.h"
#include "renderer.h"
-#include "shader.h"
-#include "tests.h"
+#include "resources.h"
+#include "texture2d.h"
using namespace std;
namespace Msp {
namespace GL {
-AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
- occlude_target(w, h, (RENDER_COLOR,R8)),
- occlude_shader("ambientocclusion_occlude.glsl"),
- combine_shader("ambientocclusion_combine.glsl"),
- quad(get_fullscreen_quad()),
- linear_sampler(get_linear_sampler()),
- nearest_sampler(get_nearest_sampler())
+AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h):
+ rotate_lookup(get_or_create_rotate_lookup()),
+ occlude_target(w, h, (COLOR_ATTACHMENT,R8)),
+ occlude_shader(Resources::get_global().get<Program>("_ambientocclusion_occlude.glsl.shader")),
+ combine_shader(Resources::get_global().get<Program>("_ambientocclusion_combine.glsl.shader")),
+ quad(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
+ linear_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
+ nearest_clamp_sampler(Resources::get_global().get<Sampler>("_nearest_clamp.samp")),
+ nearest_sampler(Resources::get_global().get<Sampler>("_nearest.samp"))
{
- texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR), linear_sampler.get());
+ set_n_samples(16);
+ set_occlusion_radius(0.5f);
+ set_darkness(1.0f);
+ set_edge_depth_threshold(0.1f);
+}
+
+const Texture2D &AmbientOcclusion::get_or_create_rotate_lookup()
+{
+ Resources &resources = Resources::get_global();
+
+ static const string name = "_ambientocclusion_rotate.tex";
+ Texture2D *rotate_lookup = resources.find<Texture2D>(name);
+ if(rotate_lookup)
+ return *rotate_lookup;
+
+ rotate_lookup = new Texture2D;
+ rotate_lookup->storage(RGBA8, 4, 4, 1);
+ resources.add(name, rotate_lookup);
- unsigned seed = 1;
- rotate_lookup.storage(RGBA8, 4, 4, 1);
unsigned char data[64];
for(unsigned i=0; i<16; ++i)
{
- Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(random(seed));
+ Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(i*7/16.0f);
unsigned char c = (cos(a)*0.5f+0.5f)*255;
unsigned char s = (sin(a)*0.5f+0.5f)*255;
data[i*4 ] = c;
data[i*4+2] = 255-s;
data[i*4+3] = ((i+i/4)%2)*255;
}
- rotate_lookup.image(0, data);
-
- texturing.attach(3, rotate_lookup, nearest_sampler.get());
+ rotate_lookup->image(0, data);
- shdata.uniform("source", 0);
- shdata.uniform("depth", 1);
- shdata.uniform("occlusion", 2);
- shdata.uniform("rotate", 3);
- shdata.uniform("inverse_projection", Matrix());
-
- set_n_samples(16);
- set_occlusion_radius(0.5f);
- set_darkness(1.0f);
- set_edge_depth_threshold(0.1f);
+ return *rotate_lookup;
}
-float AmbientOcclusion::random(unsigned &seed)
+float AmbientOcclusion::radical_inverse(unsigned n)
{
- static const unsigned modulus = (1U<<31)-1;
- seed = (static_cast<UInt64>(seed)*48271)%modulus; // minstd
- return static_cast<float>(seed)/(modulus-1);
+ unsigned inv = ((n&0x55)<<1) | ((n&0xAA)>>1);
+ inv = ((inv&0x33)<<2) | ((inv&0xCC)>>2);
+ inv = ((inv&0x0F)<<4) | ((inv&0xF0)>>4);
+ return inv/256.0f;
}
void AmbientOcclusion::set_n_samples(unsigned n)
{
- if(n<1 || n>32)
+ if(n<1 || n>128)
throw out_of_range("AmbientOcclusion::set_n_samples");
- unsigned seed = 1;
- float radius_divisor = (n-1)*(n-1);
- Vector3 sample_points[32];
+ vector<Vector3> sample_points(n);
for(unsigned i=0; i<n; ++i)
{
- Vector3 v(random(seed)-0.5f, random(seed)-0.5f, random(seed)-0.5f);
- sample_points[i] = normalize(v)*(0.1f+0.9f*i*i/radius_divisor);
+ float r = static_cast<float>(i)/n;
+ float z = sqrt(1.0f-r*r);
+ float d = radical_inverse(i);
+ Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(d);
+ sample_points[i] = Vector3(cos(a)*r, sin(a)*r, z)*(0.1f+0.9f*d*d);
}
shdata.uniform3_array("sample_points", n, &sample_points[0].x);
shdata.uniform("n_samples", static_cast<int>(n));
void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
{
- texturing.attach(0, color, nearest_sampler.get());
- texturing.attach(1, depth, nearest_sampler.get());
-
- if(renderer.get_camera())
- shdata.uniform("inverse_projection", invert(renderer.get_camera()->get_projection_matrix()));
+ const Framebuffer *out_fbo = renderer.get_framebuffer();
Renderer::Push push(renderer);
- renderer.set_texturing(&texturing);
+ renderer.set_texture("source", &color, &nearest_clamp_sampler);
+ renderer.set_texture("depth", &depth, &nearest_clamp_sampler);
+ renderer.set_texture("occlusion", &occlude_target.get_target_texture(COLOR_ATTACHMENT), &linear_sampler);
+ renderer.set_texture("rotate", &rotate_lookup, &nearest_sampler);
renderer.set_shader_program(&occlude_shader, &shdata);
- {
- BindRestore bind_fbo(occlude_target.get_framebuffer());
- quad->draw(renderer);
- }
+ renderer.set_framebuffer(&occlude_target.get_framebuffer());
+ renderer.clear(0);
+ quad.draw(renderer);
+ renderer.set_framebuffer(out_fbo);
+ renderer.clear(0);
renderer.set_shader_program(&combine_shader);
- quad->draw(renderer);
+ quad.draw(renderer);
}
+void AmbientOcclusion::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+ occlude_target.set_debug_name(name+" [RT]");
+ shdata.set_debug_name(name+" [UBO]");
+#else
+ (void)name;
+#endif
+}
-AmbientOcclusion::Template::Template():
- n_samples(16),
- occlusion_radius(0.5f),
- darkness(1.0f),
- edge_depth_threshold(0.1f)
-{ }
AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
{