Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGMaterialPool.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #include <stdlib.h>
00040 #include <stdio.h>
00041 
00042 #include <OSGConfig.h>
00043 
00044 #include "OSGNodePtr.h"
00045 #include "OSGMaterialPool.h"
00046 #include "OSGMaterialGroup.h"
00047 #include "OSGMaterialDrawable.h"
00048 
00049 OSG_USING_NAMESPACE
00050 
00054 /*-------------------------------------------------------------------------*/
00055 /*                                Sync                                     */
00056 
00057 void MaterialPool::changed(BitVector whichField, UInt32 origin)
00058 {
00059     if(origin & ChangedOrigin::Abstract)
00060     {
00061         MFMaterialPtr::iterator matIt  = _mfMaterials.begin();
00062         MFMaterialPtr::iterator matEnd = _mfMaterials.end ();
00063         
00064         while(matIt != matEnd)
00065         {
00066             addRefCP(*matIt);
00067             ++matIt;
00068         }
00069     }
00070 
00071     if(whichField & MaterialsFieldMask)
00072     {
00073         sync();
00074     }
00075     Inherited::changed(whichField, origin);
00076 }
00077 
00078 /*-------------------------------------------------------------------------*/
00079 /*                                Dump                                     */
00080 
00081 void MaterialPool::dump(      UInt32    uiIndent, 
00082                          const BitVector bvFlags) const
00083 {
00084     Inherited::dump(uiIndent, bvFlags);
00085 }
00086 
00087 /*-------------------------------------------------------------------------*/
00088 /*                            Constructors                                 */
00089 
00090 MaterialPool::MaterialPool(void) :
00091     Inherited(),
00092     _mats()
00093 {
00094 }
00095 
00096 MaterialPool::MaterialPool(const MaterialPool &source) :
00097     Inherited(source),
00098     _mats(source._mats)
00099 {
00100 }
00101 
00102 /*-------------------------------------------------------------------------*/
00103 /*                             Destructor                                  */
00104 
00105 MaterialPool::~MaterialPool(void)
00106 {
00107 }
00108 
00109 void MaterialPool::onDestroy(void)
00110 {
00111     // if we're in shutdown this is the prototype ...
00112     if(OSG::GlobalSystemState == OSG::Shutdown)
00113         return;
00114 
00115     MFMaterialPtr::iterator       matIt        = _mfMaterials.begin();
00116     MFMaterialPtr::const_iterator endMaterials = _mfMaterials.end  ();
00117 
00118     FINFO(("MaterialPool::onDestroy : subrefing %u materials\n", _mfMaterials.size()));
00119     while(matIt != endMaterials)
00120     {
00121         subRefCP(*matIt);
00122         ++matIt;
00123     }
00124 }
00125 
00126 /*-------------------------------------------------------------------------*/
00127 /*                                Init                                     */
00128  
00129 void MaterialPool::initMethod(void)
00130 {
00131 }
00132 
00133 
00134 void MaterialPool::sync(void)
00135 {
00136     // should be safe as the materials interface is protected.
00137     if(_mfMaterials.size() == _mats.size())
00138     {
00139         FINFO(("MaterialPool::sync : %u materials already synced, nothing to do.\n", _mfMaterials.size()));
00140         return;
00141     }
00142 
00143     MFMaterialPtr::iterator       matIt        = _mfMaterials.begin();
00144     MFMaterialPtr::const_iterator endMaterials = _mfMaterials.end  ();
00145 
00146     while(matIt != endMaterials)
00147     {
00148         // this is already done in the osb loader!
00149         //addRefCP(*matIt);
00150         _mats.insert(*matIt);
00151         ++matIt;
00152     }
00153     FINFO(("MaterialPool::sync : synced %u materials\n", _mfMaterials.size()));
00154 }
00155 
00156 /*-------------------------------------------------------------------------*/
00157 /*                             Access                                      */
00158 
00159 void MaterialPool::add(const MaterialPtr &mat)
00160 {
00161     if(mat == NullFC)
00162         return;
00163 
00164     if(_mats.count(mat) > 0)
00165         return;
00166 
00167     addRefCP(mat);
00168     _mats.insert(mat);
00169     _mfMaterials.push_back(mat);
00170 }
00171 
00172 void MaterialPool::add(const NodePtr &root)
00173 {
00174     traverse(root, osgTypedMethodFunctor1ObjPtrCPtrRef<Action::ResultE,
00175                    MaterialPool, NodePtr>(this, &MaterialPool::addMaterialCB));
00176 }
00177 
00178 void MaterialPool::add(const MaterialPoolPtr &mp)
00179 {
00180     if(mp == getPtr())
00181         return;
00182 
00183     MFMaterialPtr::iterator       matIt        = mp->_mfMaterials.begin();
00184     MFMaterialPtr::const_iterator endMaterials = mp->_mfMaterials.end  ();
00185 
00186     while(matIt != endMaterials)
00187     {
00188         add(*matIt);
00189         ++matIt;
00190     }
00191 }
00192 
00193 Int32 MaterialPool::find(const MaterialPtr &mat) const
00194 {
00195     if(_mats.count(mat) == 0)
00196         return -1;
00197 
00198     UInt32 index;
00199 
00200     for(index = 0; index < _mfMaterials.size(); ++index)
00201     {
00202         if(_mfMaterials[index] == mat)
00203             return index;
00204     }
00205     return -1;
00206 }
00207 
00208 void MaterialPool::sub(const MaterialPtr &mat)
00209 {
00210     if(_mats.count(mat) == 0)
00211     {
00212         SWARNING << "MaterialPool(" << this << ")::sub: " << mat 
00213                  << " is not one of my materials!" << std::endl;
00214         return;
00215     }
00216 
00217     MFMaterialPtr::iterator matIt = _mfMaterials.find(mat);
00218 
00219     if(matIt != _mfMaterials.end())
00220     {
00221         _mats.erase(mat);
00222         subRefCP(mat);
00223 
00224         _mfMaterials.erase(matIt);
00225     }
00226     else
00227     {
00228         SWARNING << "MaterialPool(" << this << ")::sub: " << mat 
00229                  << " is not one of my materials!" << std::endl;
00230     }
00231 }
00232 
00233 void MaterialPool::sub(const NodePtr &root)
00234 {
00235     traverse(root, osgTypedMethodFunctor1ObjPtrCPtrRef<Action::ResultE,
00236                    MaterialPool, NodePtr>(this, &MaterialPool::subMaterialCB));
00237 }
00238 
00239 void MaterialPool::sub(const MaterialPoolPtr &mp)
00240 {
00241     if(mp == getPtr())
00242         return;
00243 
00244     MFMaterialPtr::iterator       matIt        = mp->_mfMaterials.begin();
00245     MFMaterialPtr::const_iterator endMaterials = mp->_mfMaterials.end  ();
00246 
00247     while(matIt != endMaterials)
00248     {
00249         sub(*matIt);
00250         ++matIt;
00251     }
00252 }
00253 
00254 void MaterialPool::sub(UInt32 index)
00255 {
00256     MFMaterialPtr::iterator matIt = _mfMaterials.begin();
00257 
00258     matIt += index;
00259 
00260     if(matIt != _mfMaterials.end())
00261     {
00262         subRefCP(*matIt);
00263         _mats.erase(*matIt);
00264         _mfMaterials.erase(matIt);
00265     }
00266 }
00267 
00268 void MaterialPool::get(std::vector<MaterialPtr> &mats)
00269 {
00270     mats.clear();
00271     mats.reserve(_mfMaterials.getSize());
00272     MFMaterialPtr::iterator       matIt        = _mfMaterials.begin();
00273     MFMaterialPtr::const_iterator endMaterials = _mfMaterials.end  ();
00274 
00275     while(matIt != endMaterials)
00276     {
00277         mats.push_back(*matIt);
00278         ++matIt;
00279     }
00280 }
00281 
00282 const std::set<MaterialPtr> &MaterialPool::get(void)
00283 {
00284     return _mats;
00285 }
00286 
00287 void MaterialPool::clear(void)
00288 {
00289     MFMaterialPtr::iterator       matIt        = _mfMaterials.begin();
00290     MFMaterialPtr::const_iterator endMaterials = _mfMaterials.end  ();
00291 
00292     while(matIt != endMaterials)
00293     {
00294         subRefCP(*matIt);
00295         ++matIt;
00296     }
00297     _mfMaterials.clear();
00298     _mats.clear();
00299 }
00300 
00301 Action::ResultE MaterialPool::addMaterialCB(NodePtr &node)
00302 {
00303     FieldContainerPtr core = node->getCore();
00304 
00305     if(core == NullFC)
00306         return Action::Continue;
00307 
00308     MaterialGroupPtr mg = MaterialGroupPtr::dcast(core);
00309     if(mg != NullFC)
00310     {
00311         add(mg->getMaterial());
00312         return Action::Continue;
00313     }
00314 
00315     MaterialPoolPtr mp = MaterialPoolPtr::dcast(core);
00316     if(mp != NullFC)
00317     {
00318         add(mp);
00319         return Action::Continue;
00320     }
00321 
00322     MaterialDrawablePtr md = MaterialDrawablePtr::dcast(core);
00323     if(md != NullFC)
00324     {
00325         add(md->getMaterial());
00326         return Action::Continue;
00327     }
00328 
00329     const char *typename2 = core->getType().getName().str();
00330     if(!strcmp(typename2, "DVRVolume"))
00331     {
00332         Field *field = core->getField("renderMaterial");
00333         if(field != NULL)
00334         {
00335             add(MaterialPtr::dcast(((SFFieldContainerPtr *) field)->getValue()));
00336         }
00337     }
00338 
00339     return Action::Continue;
00340 }
00341 
00342 Action::ResultE MaterialPool::subMaterialCB(NodePtr &node)
00343 {
00344     FieldContainerPtr core = node->getCore();
00345 
00346     if(core == NullFC)
00347         return Action::Continue;
00348 
00349     MaterialGroupPtr mg = MaterialGroupPtr::dcast(core);
00350     if(mg != NullFC)
00351     {
00352         sub(mg->getMaterial());
00353         return Action::Continue;
00354     }
00355 
00356     // ignore MaterialPool node!
00357 #if 0
00358     MaterialPoolPtr mp = MaterialPoolPtr::dcast(core);
00359     if(mp != NullFC)
00360     {
00361         sub(mp);
00362         return Action::Continue;
00363     }
00364 #endif
00365 
00366     MaterialDrawablePtr md = MaterialDrawablePtr::dcast(core);
00367     if(md != NullFC)
00368     {
00369         sub(md->getMaterial());
00370         return Action::Continue;
00371     }
00372 
00373     const char *typename2 = core->getType().getName().str();
00374     if(!strcmp(typename2, "DVRVolume"))
00375     {
00376         Field *field = core->getField("renderMaterial");
00377         if(field != NULL)
00378         {
00379             sub(MaterialPtr::dcast(((SFFieldContainerPtr *) field)->getValue()));
00380         }
00381     }
00382 
00383     return Action::Continue;
00384 }
00385 
00386 /*-------------------------------------------------------------------------*/
00387 /*                              cvs id's                                   */
00388 
00389 #ifdef __sgi
00390 #pragma set woff 1174
00391 #endif
00392 
00393 #ifdef OSG_LINUX_ICC
00394 #pragma warning( disable : 177 )
00395 #endif
00396 
00397 namespace
00398 {
00399     static Char8 cvsid_cpp[] = "@(#)$Id: OSGMaterialPool.cpp,v 1.1 2005/04/30 15:03:19 a-m-z Exp $";
00400     static Char8 cvsid_hpp[] = OSGMATERIALPOOL_HEADER_CVSID;
00401     static Char8 cvsid_inl[] = OSGMATERIALPOOL_INLINE_CVSID;
00402 }

Generated on Thu Aug 25 04:07:32 2005 for OpenSG by  doxygen 1.4.3