]> git.tdb.fi Git - libs/gl.git/blobdiff - source/effects/ambientocclusion.cpp
Store the ambient occlusion rotate lookup texture in resources
[libs/gl.git] / source / effects / ambientocclusion.cpp
index bbff1f25b45d5c7bad60768e96be28f8b0bbf427..b4050aef92428ceeacf44a8924e4a48d07085279 100644 (file)
@@ -13,6 +13,7 @@ namespace Msp {
 namespace GL {
 
 AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
+       rotate_lookup(get_or_create_rotate_lookup()),
        occlude_target(w, h, (RENDER_COLOR,R8)),
        occlude_shader(Resources::get_global().get<Program>("_ambientocclusion_occlude.glsl.shader")),
        combine_shader(Resources::get_global().get<Program>("_ambientocclusion_combine.glsl.shader")),
@@ -21,8 +22,26 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
        nearest_clamp_sampler(Resources::get_global().get<Sampler>("_nearest_clamp.samp")),
        nearest_sampler(Resources::get_global().get<Sampler>("_nearest.samp"))
 {
+       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.tex2d";
+       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)
        {
@@ -34,12 +53,9 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
                data[i*4+2] = 255-s;
                data[i*4+3] = ((i+i/4)%2)*255;
        }
-       rotate_lookup.image(0, data);
+       rotate_lookup->image(0, data);
 
-       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)