]> git.tdb.fi Git - libs/gl.git/blobdiff - source/ambientocclusion.cpp
Make ambient occlusion edge detection threshold adjustable
[libs/gl.git] / source / ambientocclusion.cpp
index 9e376a456f760ce84933b6f5f204f4cd21246cd2..ed7d1636d711d34ee6539fd4ddb11adbb4677f44 100644 (file)
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2010-2011  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <cmath>
+#include <algorithm>
 #include "ambientocclusion.h"
 #include "blend.h"
-#include "meshbuilder.h"
+#include "camera.h"
+#include "renderer.h"
+#include "shader.h"
 #include "tests.h"
 
-namespace {
-
-const char vertex_shader[] =
-       "varying vec2 texcoord;\n"
-       "void main()\n"
-       "{\n"
-       "       gl_Position = gl_Vertex;\n"
-       "       texcoord = gl_Vertex.xy*0.5+0.5;\n"
-       "}\n";
-
-const char occlude_fs[] =
-       "uniform sampler2D depth;\n"
-       "uniform sampler2D rotate;\n"
-       "uniform vec2 screen_size;\n"
-       "uniform vec2 depth_ratio;\n"
-       "uniform float darkness;\n"
-       "varying vec2 texcoord;\n"
-       "void main()\n"
-       "{\n"
-       "       mat2 transform = mat2(texture2D(rotate, texcoord*screen_size/4.0)*2.0-1.0)\n"
-       "               *mat2(screen_size.y/screen_size.x, 0.0, 0.0, 1.0)*3.0/screen_size.y;\n"
-       "       float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\n"
-       "       float sum = 0.0;\n"
-       "       for(int i=0; i<=3; ++i)\n"
-       "               for(int j=0; j<=3; ++j)\n"
-       "               {\n"
-       "                       vec2 offs = transform*vec2(float(i)-1.5, float(j)-1.5);\n"
-       "                       float dxy = length(offs)*-sample;\n"
-       "                       float dz = depth_ratio.x/(texture2D(depth, texcoord+offs).r-depth_ratio.y)-sample;\n"
-       "                       if(abs(dz)<3*dxy)\n"
-       "                               sum += atan(dz/dxy)/1.570796;\n"
-       "                       else if(dz<0)\n"
-       "                               sum -= 0.8;\n"
-       "               }\n"
-       "       gl_FragColor = vec4(min(1.0-sum*darkness/16.0, 1.0), 0.0, 0.0, 1.0);\n"
-       "}\n";
-
-const char combine_fs[] =
-       "uniform sampler2D color;\n"
-       "uniform sampler2D depth;\n"
-       "uniform sampler2D occlusion;\n"
-       "uniform vec2 screen_size;\n"
-       "uniform vec2 depth_ratio;\n"
-       "varying vec2 texcoord;\n"
-       "void main()\n"
-       "{\n"
-       "       float sample = depth_ratio.x/(texture2D(depth, texcoord).r-depth_ratio.y);\n"
-       "       float sum = 1.0;\n"
-       "       float count = 1.0;\n"
-       "       for(int i=0; i<=3; ++i)\n"
-       "               for(int j=0; j<=3; ++j)\n"
-       "               {\n"
-       "                       vec2 offs = vec2(float(i)-1.5, float(j)-1.5)/screen_size;\n"
-       "                       float dxy = length(offs)*-sample;\n"
-       "                       float dz = depth_ratio.x/(texture2D(depth, texcoord+offs).r-depth_ratio.y)-sample;\n"
-       "                       if(abs(dz)<3*dxy)\n"
-       "                       {\n"
-       "                               sum += texture2D(occlusion, texcoord+offs).r;\n"
-       "                               count += 1.0;\n"
-       "                       }\n"
-       "               }\n"
-       "       gl_FragColor = texture2D(color, texcoord)*sum/count;\n"
-       "}\n";
-
-}
+using namespace std;
 
 namespace Msp {
 namespace GL {
 
-AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float depth_ratio):
-       occlude_shader(vertex_shader, occlude_fs),
-       occlude_shdata(occlude_shader),
-       combine_shader(vertex_shader, combine_fs),
-       combine_shdata(combine_shader),
-       quad(VERTEX2)
+AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
+       occlude_target(w, h, (RENDER_COLOR,RED)),
+       occlude_shader("ambientocclusion_occlude.glsl"),
+       combine_shader("ambientocclusion_combine.glsl"),
+       quad(get_fullscreen_quad())
 {
-       occlusion.storage(GL::RGB, w, h);
-       occlusion.set_min_filter(GL::NEAREST);
-       occlusion.set_mag_filter(GL::NEAREST);
-       fbo.attach(COLOR_ATTACHMENT0, occlusion, 0);
+       occlude_target.set_texture_filter(LINEAR);
+       texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR));
 
-       combine_texturing.attach(2, occlusion);
-
-       rotate_lookup.storage(GL::RGBA, 4, 4);
-       rotate_lookup.set_min_filter(GL::NEAREST);
-       rotate_lookup.set_mag_filter(GL::NEAREST);
+       unsigned seed = 1;
+       rotate_lookup.storage(RGBA, 4, 4, 1);
+       rotate_lookup.set_filter(NEAREST);
        unsigned char data[64];
        for(unsigned i=0; i<16; ++i)
        {
-               float a = ((i*541)%16)*M_PI/32;
-               float c = cos(a);
-               float s = sin(a);
-               data[i*3  ] = static_cast<unsigned char>(127+c*127);
-               data[i*3+1] = static_cast<unsigned char>(127+s*127);
-               data[i*3+2] = static_cast<unsigned char>(127-s*127);
-               data[i*3+4] = static_cast<unsigned char>(127+c*127);
+               Geometry::Angle<float> a = Geometry::Angle<float>::from_turns(random(seed));
+               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+1] = s;
+               data[i*4+2] = 255-s;
+               data[i*4+3] = ((i+i/4)%2)*255;
        }
-       rotate_lookup.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data);
+       rotate_lookup.image(0, RGBA, UNSIGNED_BYTE, data);
+
+       texturing.attach(3, rotate_lookup);
 
-       occlude_texturing.attach(1, rotate_lookup);
+       shdata.uniform("source", 0);
+       shdata.uniform("depth", 1);
+       shdata.uniform("occlusion", 2);
+       shdata.uniform("rotate", 3);
+       shdata.uniform("inverse_projection", Matrix());
 
-       occlude_shdata.uniform("depth", 0);
-       occlude_shdata.uniform("rotate", 1);
-       occlude_shdata.uniform("screen_size", w, h);
+       set_n_samples(16);
+       set_occlusion_radius(0.5f);
+       set_darkness(1.0f);
+       set_edge_depth_threshold(0.1f);
+}
 
-       combine_shdata.uniform("color", 1);
-       combine_shdata.uniform("depth", 0);
-       combine_shdata.uniform("occlusion", 2);
-       combine_shdata.uniform("screen_size", w, h);
+float AmbientOcclusion::random(unsigned &seed)
+{
+       static const unsigned modulus = (1U<<31)-1;
+       seed = (static_cast<UInt64>(seed)*48271)%modulus;  // minstd
+       return static_cast<float>(seed)/(modulus-1);
+}
 
-       set_depth_ratio(depth_ratio);
-       set_darkness(1.5);
+void AmbientOcclusion::set_n_samples(unsigned n)
+{
+       if(n<1 || n>32)
+               throw out_of_range("AmbientOcclusion::set_n_samples");
 
-       MeshBuilder bld(quad);
-       bld.begin(TRIANGLE_STRIP);
-       bld.vertex(-1, 1);
-       bld.vertex(-1, -1);
-       bld.vertex(1, 1);
-       bld.vertex(1, -1);
-       bld.end();
+       unsigned seed = 1;
+       float radius_divisor = (n-1)*(n-1);
+       Vector3 sample_points[32];
+       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);
+       }
+       shdata.uniform3_array("sample_points", n, &sample_points[0].x);
+       shdata.uniform("n_samples", static_cast<int>(n));
 }
 
-void AmbientOcclusion::set_depth_ratio(float depth_ratio)
+void AmbientOcclusion::set_occlusion_radius(float r)
 {
-       depth_ratio = 1/depth_ratio;
+       shdata.uniform("occlusion_radius", r);
+}
 
-       occlude_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
-       combine_shdata.uniform("depth_ratio", depth_ratio, 1+depth_ratio);
+void AmbientOcclusion::set_depth_ratio(float)
+{
 }
 
 void AmbientOcclusion::set_darkness(float darkness)
 {
-       occlude_shdata.uniform("darkness", darkness);
+       shdata.uniform("darkness", darkness);
 }
 
-void AmbientOcclusion::render(const Texture2D &color, const Texture2D &depth)
+void AmbientOcclusion::set_edge_depth_threshold(float edt)
 {
-       occlude_texturing.attach(0, depth);
-       combine_texturing.attach(0, depth);
-       combine_texturing.attach(1, color);
+       shdata.uniform("edge_depth_threshold", edt);
+}
+
+void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
+{
+       texturing.attach(0, color);
+       texturing.attach(1, depth);
+
+       if(renderer.get_camera())
+               shdata.uniform("inverse_projection", invert(renderer.get_camera()->get_projection_matrix()));
 
-       Bind unbind_dtest(static_cast<DepthTest *>(0), true);
-       Bind unbind_blend(static_cast<Blend *>(0), true);
+       Renderer::Push push(renderer);
+       renderer.set_texturing(&texturing);
+       renderer.set_shader_program(&occlude_shader, &shdata);
 
        {
-               GL::Bind bind_fbo(fbo, true);
-               GL::Bind bind_tex(occlude_texturing);
-               GL::Bind bind_shader(occlude_shader);
-               occlude_shdata.apply();
-               quad.draw();
+               BindRestore bind_fbo(occlude_target.get_framebuffer());
+               quad.draw(renderer);
        }
 
-       GL::Bind bind_tex(combine_texturing);
-       GL::Bind bind_shader(combine_shader);
-       combine_shdata.apply();
-       quad.draw();
+       renderer.set_shader_program(&combine_shader);
+       quad.draw(renderer);
+}
+
+
+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
+{
+       RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
+       ao->set_n_samples(n_samples);
+       ao->set_occlusion_radius(occlusion_radius);
+       ao->set_darkness(darkness);
+       ao->set_edge_depth_threshold(edge_depth_threshold);
+       return ao.release();
+}
+
+
+AmbientOcclusion::Template::Loader::Loader(Template &t):
+       DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
+{
+       add("darkness", &Template::darkness);
+       add("edge_depth_threshold", &Template::edge_depth_threshold);
+       add("occlusion_radius", &Template::occlusion_radius);
+       add("samples", &Template::n_samples);
 }
 
 } // namespace GL