]> git.tdb.fi Git - libs/gl.git/blobdiff - builtin_data/_ambientocclusion_occlude.glsl
Add support for integer vertex attributes
[libs/gl.git] / builtin_data / _ambientocclusion_occlude.glsl
index e1900323a6cc9a66432f4d70e29312dcaa5a92c0..7ff813e8d27a1c175d8716a0c20fa7e0a4aabbee 100644 (file)
@@ -1,25 +1,38 @@
-import postprocess;
+import flat_effect;
 import _ambientocclusion;
 
 #pragma MSP stage(fragment)
+layout(location=0) out float frag_out;
 void main()
 {
+       vec3 tex_scale = vec3(1.0/vec2(textureSize(depth, 0)), 0.0);
+
+       vec3 center = get_fragment_position(texcoord);
+       vec3 left = get_fragment_position(texcoord-tex_scale.xz);
+       vec3 right = get_fragment_position(texcoord+tex_scale.xz);
+       vec3 bottom = get_fragment_position(texcoord-tex_scale.zy);
+       vec3 top = get_fragment_position(texcoord+tex_scale.zy);
+
+       vec3 normal = normalize(cross(get_slope(left, center, right), get_slope(bottom, center, top)));
+       vec3 tangent = (abs(normal.x)>abs(normal.y) ? vec3(-normal.z, 0.0, normal.x) : vec3(0.0, -normal.z, normal.y));
+       vec3 binormal = cross(normal, tangent);
+
        vec4 rv = texture(rotate, gl_FragCoord.xy/4.0)*2.0-1.0;
-       mat3 transform = mat3(rv.xy, 0.0, rv.zx, 0.0, 0.0, 0.0, rv.w)*occlusion_radius;
-       vec3 center = unproject(vec3(vertex.xy, texture(depth, texcoord).r));
+       mat3 transform = mat3(tangent, binormal, normal)*mat3(rv.xy, 0.0, rv.zx, 0.0, 0.0, 0.0, 1.0)*occlusion_radius;
+
        float min_depth = project(vec3(center.xy, center.z+occlusion_radius)).z;
        float occlusion_sum = 0.0;
        float count = 0.0;
        for(int i=0; i<n_samples; ++i)
        {
                vec3 psp = project(center+transform*sample_points[i]);
-               float sample = texture(depth, psp.xy*0.5+0.5).r;
-               if(sample>=min_depth)
+               float sample_depth = texture(depth, psp.xy*0.5+0.5).r;
+               if(sample_depth>=min_depth)
                {
-                       if(sample<psp.z)
+                       if(sample_depth<psp.z)
                                occlusion_sum += 1.0;
                        count += 1.0;
                }
        }
-       frag_color = vec4(1.0-occlusion_sum/count, 0.0, 0.0, 1.0);
+       frag_out = 1.0-occlusion_sum/count;
 }