X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Feffects%2Fambientocclusion.cpp;h=c7fff846fbbe655b7d25f0d5b7daf886ed42d866;hp=a407c2d9918621b3aa9f057fd93597845635f1e9;hb=HEAD;hpb=7f81f26889b84542b0b35685b0e80383256cdc47 diff --git a/source/effects/ambientocclusion.cpp b/source/effects/ambientocclusion.cpp index a407c2d9..c7fff846 100644 --- a/source/effects/ambientocclusion.cpp +++ b/source/effects/ambientocclusion.cpp @@ -2,19 +2,19 @@ #include "ambientocclusion.h" #include "blend.h" #include "camera.h" +#include "mesh.h" #include "renderer.h" #include "resources.h" -#include "shader.h" -#include "tests.h" +#include "texture2d.h" using namespace std; namespace Msp { namespace GL { -AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float): +AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h): rotate_lookup(get_or_create_rotate_lookup()), - occlude_target(w, h, (RENDER_COLOR,R8)), + occlude_target(w, h, (COLOR_ATTACHMENT,R8)), occlude_shader(Resources::get_global().get("_ambientocclusion_occlude.glsl.shader")), combine_shader(Resources::get_global().get("_ambientocclusion_combine.glsl.shader")), quad(Resources::get_global().get("_fullscreen_quad.mesh")), @@ -32,7 +32,7 @@ const Texture2D &AmbientOcclusion::get_or_create_rotate_lookup() { Resources &resources = Resources::get_global(); - static const string name = "_ambientocclusion_rotate.tex2d"; + static const string name = "_ambientocclusion_rotate.tex"; Texture2D *rotate_lookup = resources.find(name); if(rotate_lookup) return *rotate_lookup; @@ -67,19 +67,19 @@ float AmbientOcclusion::radical_inverse(unsigned n) 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"); - Vector3 sample_points[32]; + vector sample_points(n); for(unsigned i=0; i(i)/n; - float r = sqrt(1.0f-z*z); + float r = static_cast(i)/n; + float z = sqrt(1.0f-r*r); float d = radical_inverse(i); Geometry::Angle a = Geometry::Angle::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.uniform3_array("sample_points", n, &sample_points[0][0]); shdata.uniform("n_samples", static_cast(n)); } @@ -100,29 +100,37 @@ void AmbientOcclusion::set_edge_depth_threshold(float edt) void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth) { + const Framebuffer *out_fbo = renderer.get_framebuffer(); + Renderer::Push push(renderer); 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(RENDER_COLOR), &linear_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_pipeline_key(this); + renderer.set_framebuffer(&occlude_target.get_framebuffer()); + renderer.clear(0); + quad.draw(renderer); + renderer.set_pipeline_key(this, 1); + renderer.set_framebuffer(out_fbo); + renderer.clear(0); renderer.set_shader_program(&combine_shader); 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 {