]> git.tdb.fi Git - libs/gl.git/commitdiff
Make ambient occlusion edge detection threshold adjustable
authorMikko Rasa <tdb@tdb.fi>
Sat, 15 Jun 2019 12:23:02 +0000 (15:23 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 15 Jun 2019 12:38:25 +0000 (15:38 +0300)
This may be necessary to avoid artifacts when running the postprocessor
at a reduced resolution.

shaderlib/ambientocclusion.glsl
shaderlib/ambientocclusion_combine.glsl
source/ambientocclusion.cpp
source/ambientocclusion.h

index 7f1e79a019a290c8b341f95970d6226e6eecb475..a9873b1d1fe2a4778fed2cd6fe664e404ff13ea5 100644 (file)
@@ -12,6 +12,7 @@ uniform AmbientOcclusionParams
        vec3 sample_points[max_samples];
        int n_samples;
        float occlusion_radius;
+       float edge_depth_threshold;
 };
 
 #pragma MSP stage(fragment)
index 05aea60a6c811d3f802f6a2df53db08a99597d6d..c1e7f27270a1388703ac8cc744901278180f2ac3 100644 (file)
@@ -15,7 +15,7 @@ void main()
                        vec2 sample_coord = texcoord+offset*tex_scale;
                        float occ = texture(occlusion, sample_coord).r;
                        float sample = texture(depth, sample_coord).r;
-                       float z_range = occlusion_radius*length(offset)*0.1;
+                       float z_range = occlusion_radius*length(offset)*edge_depth_threshold;
                        float min_depth = project(vec3(center.xy, center.z+z_range)).z;
                        float max_depth = project(vec3(center.xy, center.z-z_range)).z;
                        if(sample>=min_depth && sample<=max_depth)
index 0079009b513750d3f391019d2529d897b55f6f37..ed7d1636d711d34ee6539fd4ddb11adbb4677f44 100644 (file)
@@ -47,6 +47,7 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
        set_n_samples(16);
        set_occlusion_radius(0.5f);
        set_darkness(1.0f);
+       set_edge_depth_threshold(0.1f);
 }
 
 float AmbientOcclusion::random(unsigned &seed)
@@ -87,6 +88,11 @@ void AmbientOcclusion::set_darkness(float darkness)
        shdata.uniform("darkness", darkness);
 }
 
+void AmbientOcclusion::set_edge_depth_threshold(float edt)
+{
+       shdata.uniform("edge_depth_threshold", edt);
+}
+
 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
 {
        texturing.attach(0, color);
@@ -112,7 +118,8 @@ void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const
 AmbientOcclusion::Template::Template():
        n_samples(16),
        occlusion_radius(0.5f),
-       darkness(1.0f)
+       darkness(1.0f),
+       edge_depth_threshold(0.1f)
 { }
 
 AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
@@ -121,6 +128,7 @@ AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned he
        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();
 }
 
@@ -129,6 +137,7 @@ 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);
 }
index e7829c181dce39bce8c74f69a631a56fcc9990b0..419189942fad13e4b48b9223ef09bce0dc214233 100644 (file)
@@ -32,6 +32,7 @@ public:
                unsigned n_samples;
                float occlusion_radius;
                float darkness;
+               float edge_depth_threshold;
 
                Template();
 
@@ -56,6 +57,7 @@ private:
 public:
        void set_n_samples(unsigned);
        void set_occlusion_radius(float);
+       void set_edge_depth_threshold(float);
 
        // Deprecated
        void set_depth_ratio(float);