# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/expandResourceDatabase.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.org                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #include <vlGraphics/expandResourceDatabase.hpp>
    00033 #include <vlCore/ResourceDatabase.hpp>
    00034 #include <vlGraphics/Actor.hpp>
    00035 #include <vlGraphics/Camera.hpp>
    00036 #include <vlGraphics/Light.hpp>
    00037 #include <vlGraphics/ClipPlane.hpp>
    00038 #include <set>
    00039 #include <algorithm>
    00040
    00041 using namespace vl;
    00042
    00043 namespace
    00044 {
    00045   struct expanded_res
    00046   {
    00047     std::set< ref<Renderable> > ren;
    00048     std::set< ref<Actor> > act;
    00049     std::set< ref<Transform> > tr;
    00050     std::set< ref<Effect> > fx;
    00051     std::set< ref<Shader> > sh;
    00052     std::set< ref<RenderState> > rs;
    00053     std::set< ref<Uniform> > un;
    00054     std::set< ref<Viewport> > vp;
    00055     std::set< ref<Camera> > cam;
    00056     std::set< ref<Object> > misc;
    00057   };
    00058
    00059   std::vector< ref<RenderState> > sortRenderStates(const std::set< ref<RenderState> >& rs_set)
    00060   {
    00061     std::vector< ref<RenderState> > rs;
    00062     rs.insert( rs.end(), rs_set.begin(), rs_set.end() );
    00063
    00064     // for some reason GCC 3.4.5 does not allow operator() to sort
    00065     struct rs_less
    00066     {
    00067       static bool compare(const ref<RenderState>& a, const ref<RenderState>& b)
    00068       {
    00069         return a->type() < b->type();
    00070       }
    00071     };
    00072
    00073     std::sort( rs.begin(), rs.end(), rs_less::compare );
    00074     return rs;
    00075   }
    00076
    00077   void expandShader(Shader* shader, expanded_res& set)
    00078   {
    00079     // Shader
    00080     set.sh.insert( shader );
    00081     // Uniforms
    00082     if (shader->getUniformSet())
    00083       for(size_t i=0; i<shader->uniforms().size(); ++i)
    00084         set.un.insert(shader->uniforms()[i].get());
    00085     // RenderStates
    00086     if (shader->getRenderStateSet())
    00087       for(size_t i=0; i<shader->renderStatesCount(); ++i)
    00088         set.rs.insert( shader->renderStates()[i].mRS );
    00089   }
    00090
    00091   void expandEffect(Effect* fx, expanded_res& set)
    00092   {
    00093     // Effect
    00094     set.fx.insert(fx);
    00095     // Shaders
    00096     for(int i=0; i<VL_MAX_EFFECT_LOD; ++i)
    00097     {
    00098       if( fx->lod(i) )
    00099       {
    00100         for(int j=0; j<fx->lod(i)->size(); ++j)
    00101         {
    00102           Shader* shader = fx->lod(i)->at(j);
    00103           set.sh.insert( shader );
    00104           expandShader(shader, set);
    00105         }
    00106       }
    00107     }
    00108   }
    00109
    00110   void expandActor(Actor* actor, expanded_res& set)
    00111   {
    00112     // Actor
    00113     set.act.insert( actor );
    00114     // Transform
    00115     if (actor->transform())
    00116       set.tr.insert( actor->transform() );
    00117     // Uniforms
    00118     if (actor->getUniformSet())
    00119       for(size_t i=0; i<actor->uniforms().size(); ++i)
    00120         set.un.insert(actor->uniforms()[i].get());
    00121     // Renderables
    00122     for(int i=0; i<VL_MAX_ACTOR_LOD; ++i)
    00123       if ( actor->lod(i) )
    00124         set.ren.insert( actor->lod(i) );
    00125     // Effects
    00126     if (actor->effect())
    00127       expandEffect(actor->effect(), set);
    00128   }
    00129 }
    00130
    00131 // Extracts and sorts Shaders, Effects, Renderables, RenderStates, Transforms etc. from their parent objects.
    00132 void vl::expandResourceDatabase(ResourceDatabase* db)
    00133 {
    00134   expanded_res set;
    00135
    00136   for(size_t i=0; i<db->resources().size(); ++i)
    00137   {
    00138     Object* res = db->resources()[i].get();
    00139
    00140     if( res->isOfType( Renderable::Type() ) )
    00141       set.ren.insert( res->as<Renderable>() );
    00142     else
    00143     if( res->isOfType( Actor::Type() ) )
    00144       expandActor(res->as<Actor>(), set);
    00145     else
    00146     if( res->isOfType( Transform::Type() ) )
    00147       set.tr.insert( res->as<Transform>() );
    00148     else
    00149     if( res->isOfType( Effect::Type() ) )
    00150       expandEffect( res->as<Effect>(), set );
    00151     else
    00152     if( res->isOfType( Shader::Type() ) )
    00153       expandShader( res->as<Shader>(), set );
    00154     else
    00155     if( res->isOfType( RenderState::Type() ) )
    00156       set.rs.insert( res->as<RenderState>() );
    00157     else
    00158     if( res->isOfType( Uniform::Type() ) )
    00159       set.un.insert( res->as<Uniform>() );
    00160     else
    00161     if( res->isOfType( Viewport::Type() ) )
    00162       set.vp.insert( res->as<Viewport>() );
    00163     else
    00164     if( res->isOfType( Camera::Type() ) )
    00165     {
    00166       Camera* camera = res->as<Camera>();
    00167       set.cam.insert( camera );
    00168       if ( camera->viewport() )
    00169         set.vp.insert( camera->viewport() );
    00170       if ( camera->boundTransform() )
    00171         set.tr.insert( camera->boundTransform() );
    00172     }
    00173     else
    00174       set.misc.insert( res );
    00175   }
    00176
    00177   // reconstruct the resources
    00178   db->resources().clear();
    00179   db->resources().insert( db->resources().end(), set.vp.begin(), set.vp.end() );
    00180   db->resources().insert( db->resources().end(), set.cam.begin(), set.cam.end() );
    00181   db->resources().insert( db->resources().end(), set.tr.begin(), set.tr.end() );
    00182   std::vector< ref<RenderState> > sorted_rs = sortRenderStates( set.rs );
    00183   db->resources().insert( db->resources().end(), sorted_rs.begin(), sorted_rs.end() );
    00184   db->resources().insert( db->resources().end(), set.un.begin(), set.un.end() );
    00185   db->resources().insert( db->resources().end(), set.sh.begin(), set.sh.end() );
    00186   db->resources().insert( db->resources().end(), set.fx.begin(), set.fx.end() );
    00187   db->resources().insert( db->resources().end(), set.ren.begin(), set.ren.end() );
    00188   db->resources().insert( db->resources().end(), set.act.begin(), set.act.end() );
    00189   db->resources().insert( db->resources().end(), set.misc.begin(), set.misc.end() );
    00190 }

Visualization Library v2011.09.1162 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu Jan 26 2012 00:43:37.
Permission is granted to use this page to write and publish articles regarding Visualization Library.