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 //--------------------------------------------------------------------------- 00040 // Includes 00041 //--------------------------------------------------------------------------- 00042 00043 #include <stdlib.h> 00044 #include <stdio.h> 00045 00046 #include <OSGConfig.h> 00047 00048 #include <OSGSimpleGeometry.h> 00049 #include "OSGSwitchMaterial.h" 00050 00051 OSG_USING_NAMESPACE 00052 00053 /***************************************************************************\ 00054 * Description * 00055 \***************************************************************************/ 00056 00061 /***************************************************************************\ 00062 * Class variables * 00063 \***************************************************************************/ 00064 00065 /***************************************************************************\ 00066 * Class methods * 00067 \***************************************************************************/ 00068 00069 void SwitchMaterial::initMethod (void) 00070 { 00071 } 00072 00073 00074 /***************************************************************************\ 00075 * Instance methods * 00076 \***************************************************************************/ 00077 00078 /*-------------------------------------------------------------------------*\ 00079 - private - 00080 \*-------------------------------------------------------------------------*/ 00081 00082 /*----------------------- constructors & destructors ----------------------*/ 00083 00084 SwitchMaterial::SwitchMaterial(void) : 00085 Inherited() 00086 { 00087 } 00088 00089 SwitchMaterial::SwitchMaterial(const SwitchMaterial &source) : 00090 Inherited(source) 00091 { 00092 } 00093 00094 SwitchMaterial::~SwitchMaterial(void) 00095 { 00096 // ACHTUNG _pState wird der State vom jeweiligen aktiven Material zugewiesen. 00097 // wenn nun die Materialien zerstoert werden dann wird im Material::~Material 00098 // ein subRefCP(_pState) aufgerufen. Dieser subRefCP wuerde dann nochmal mit dem 00099 // gleichen ungueltigen pointer fuer das SwitchMaterial aufgerufen werden und 00100 // das kracht natürlich, deswegen wird er jetzt einfach auf NullFC gesetzt! 00101 _pState = NullFC; 00102 MFMaterialPtr::iterator matIt = _mfMaterials.begin(); 00103 MFMaterialPtr::const_iterator matEnd = _mfMaterials.end (); 00104 00105 FINFO(("SwitchMaterial::onDestroy : subrefing %u materials\n", _mfMaterials.size())); 00106 while(matIt != matEnd) 00107 { 00108 subRefCP(*matIt); 00109 ++matIt; 00110 } 00111 _mfMaterials.clear(); 00112 } 00113 00114 /*----------------------------- class specific ----------------------------*/ 00115 00116 void SwitchMaterial::changed(BitVector whichField, UInt32 origin) 00117 { 00118 if(origin & ChangedOrigin::Abstract) 00119 { 00120 MFMaterialPtr::iterator matIt = _mfMaterials.begin(); 00121 MFMaterialPtr::iterator matEnd = _mfMaterials.end (); 00122 00123 while(matIt != matEnd) 00124 { 00125 addRefCP(*matIt); 00126 00127 ++matIt; 00128 } 00129 } 00130 00131 // Don't need to call rebuildState it is allready called in the 00132 // base class method Material::changed() 00133 #if 0 00134 if(whichField & SwitchMaterial::ChoiceFieldMask) 00135 { 00136 makeState(); 00137 rebuildState(); 00138 } 00139 #endif 00140 00141 Inherited::changed(whichField, origin); 00142 } 00143 00144 void SwitchMaterial::addMaterial(MaterialPtr mat) 00145 { 00146 if(mat == NullFC) 00147 return; 00148 00149 _mfMaterials.push_back(mat); 00150 addRefCP(mat); 00151 } 00152 00153 void SwitchMaterial::subMaterial(MaterialPtr mat) 00154 { 00155 if(mat == NullFC) 00156 return; 00157 00158 UInt32 i; 00159 00160 for(i = 0; i < _mfMaterials.size(); ++i) 00161 { 00162 if(_mfMaterials[i] == mat) 00163 { 00164 subRefCP(mat); 00165 _mfMaterials.erase(_mfMaterials.begin() + i); 00166 return; 00167 } 00168 } 00169 00170 SWARNING << "SwitchMaterial::subMaterial(" << this << ") has no material " 00171 << mat << std::endl; 00172 } 00173 00174 bool SwitchMaterial::hasMaterial(MaterialPtr mat) 00175 { 00176 UInt32 i; 00177 00178 for(i = 0; i < _mfMaterials.size(); ++i) 00179 { 00180 if(_mfMaterials[i] == mat) 00181 return true; 00182 } 00183 00184 return false; 00185 } 00186 00187 MaterialPtr SwitchMaterial::getMaterial(UInt32 index) 00188 { 00189 if(index >= _mfMaterials.size()) 00190 { 00191 FWARNING(("SwitchMaterial::getMaterial : index %u out of range\n", index)); 00192 return NullFC; 00193 } 00194 return _mfMaterials[index]; 00195 } 00196 00197 MaterialPtr SwitchMaterial::getCurrentMaterial(void) 00198 { 00199 UInt32 choice = getChoice(); 00200 if(choice >= _mfMaterials.size()) 00201 { 00202 FWARNING(("SwitchMaterial::getCurrentMaterial : current choice %u out of range\n", choice)); 00203 return NullFC; 00204 } 00205 return _mfMaterials[choice]; 00206 } 00207 00211 void SwitchMaterial::draw(Geometry *OSG_CHECK_ARG(geo), DrawActionBase *OSG_CHECK_ARG(action)) 00212 { 00213 } 00214 00220 void SwitchMaterial::draw(DrawFunctor &OSG_CHECK_ARG(func), DrawActionBase *OSG_CHECK_ARG(action)) 00221 { 00222 } 00223 00227 StatePtr SwitchMaterial::makeState(void) 00228 { 00229 UInt32 choice = getChoice(); 00230 if(choice >= _mfMaterials.size()) 00231 { 00232 if(!_mfMaterials.empty()) 00233 SWARNING << "SwitchMaterial::makeState: choice index (" << choice << ") out of range!" << std::endl; 00234 return OSG::getDefaultMaterial()->makeState(); 00235 } 00236 00237 if(_mfMaterials[choice] != NullFC) 00238 { 00239 return _mfMaterials[choice]->makeState(); 00240 } 00241 00242 return OSG::getDefaultMaterial()->makeState(); 00243 } 00244 00248 void SwitchMaterial::rebuildState(void) 00249 { 00250 UInt32 choice = getChoice(); 00251 if(choice >= _mfMaterials.size()) 00252 { 00253 if(!_mfMaterials.empty()) 00254 SWARNING << "SwitchMaterial::rebuildState: choice index (" << choice << ") out of range!" << std::endl; 00255 OSG::getDefaultMaterial()->rebuildState(); 00256 //_pState = OSG::getDefaultMaterial()->getState(); 00257 return; 00258 } 00259 00260 if(_mfMaterials[choice] != NullFC) 00261 { 00262 _mfMaterials[choice]->rebuildState(); 00263 //_pState = _mfMaterials[choice]->getState(); 00264 } 00265 else 00266 { 00267 OSG::getDefaultMaterial()->rebuildState(); 00268 //_pState = OSG::getDefaultMaterial()->getState(); 00269 } 00270 } 00271 00272 StatePtr SwitchMaterial::getState(UInt32 index) 00273 { 00274 UInt32 choice = getChoice(); 00275 if(choice >= _mfMaterials.size()) 00276 { 00277 if(!_mfMaterials.empty()) 00278 SWARNING << "SwitchMaterial::getState: choice index out of range!" << std::endl; 00279 return NullFC; 00280 } 00281 00282 if(_mfMaterials[choice] != NullFC) 00283 { 00284 if(_mfMaterials[choice]->getState(index) == NullFC) 00285 rebuildState(); 00286 00287 return _mfMaterials[choice]->getState(index); 00288 } 00289 00290 return NullFC; 00291 } 00292 00293 bool SwitchMaterial::isMultiPass(void) const 00294 { 00295 UInt32 choice = getChoice(); 00296 if(choice >= _mfMaterials.size()) 00297 { 00298 if(!_mfMaterials.empty()) 00299 SWARNING << "SwitchMaterial::isMultiPass: choice index out of range!" << std::endl; 00300 return false; 00301 } 00302 00303 if(_mfMaterials[choice] != NullFC) 00304 return _mfMaterials[choice]->isMultiPass(); 00305 00306 return false; 00307 } 00308 00309 UInt32 SwitchMaterial::getNPasses(void) const 00310 { 00311 UInt32 choice = getChoice(); 00312 if(choice >= _mfMaterials.size()) 00313 { 00314 if(!_mfMaterials.empty()) 00315 SWARNING << "SwitchMaterial::getNPasses: choice index out of range!" << std::endl; 00316 return 1; 00317 } 00318 00319 if(_mfMaterials[choice] != NullFC) 00320 return _mfMaterials[choice]->getNPasses(); 00321 00322 return 1; 00323 } 00324 00327 bool SwitchMaterial::isTransparent(void) const 00328 { 00329 UInt32 choice = getChoice(); 00330 if(choice >= _mfMaterials.size()) 00331 { 00332 if(!_mfMaterials.empty()) 00333 SWARNING << "SwitchMaterial::isTransparent: choice index out of range!" << std::endl; 00334 return false; 00335 } 00336 00337 if(_mfMaterials[choice] != NullFC) 00338 return _mfMaterials[choice]->isTransparent(); 00339 00340 return false; 00341 } 00342 00343 Int32 SwitchMaterial::getRealSortKey(void) const 00344 { 00345 UInt32 choice = getChoice(); 00346 if(choice >= _mfMaterials.size()) 00347 { 00348 if(!_mfMaterials.empty()) 00349 SWARNING << "SwitchMaterial::isTransparent: choice index out of range!" << std::endl; 00350 return Inherited::getRealSortKey(); 00351 } 00352 00353 if(_mfMaterials[choice] != NullFC) 00354 return _mfMaterials[choice]->getRealSortKey(); 00355 00356 return Inherited::getRealSortKey(); 00357 } 00358 00359 void SwitchMaterial::dump( UInt32 , 00360 const BitVector ) const 00361 { 00362 SLOG << "Dump SwitchMaterial NI" << std::endl; 00363 } 00364 00365 /*------------------------------------------------------------------------*/ 00366 /* cvs id's */ 00367 00368 #ifdef OSG_SGI_CC 00369 #pragma set woff 1174 00370 #endif 00371 00372 #ifdef OSG_LINUX_ICC 00373 #pragma warning( disable : 177 ) 00374 #endif 00375 00376 namespace 00377 { 00378 static Char8 cvsid_cpp [] = "@(#)$Id: OSGSwitchMaterial.cpp,v 1.4 2006/08/22 10:11:44 a-m-z Exp $"; 00379 static Char8 cvsid_hpp [] = OSGSWITCHMATERIALBASE_HEADER_CVSID; 00380 static Char8 cvsid_inl [] = OSGSWITCHMATERIALBASE_INLINE_CVSID; 00381 00382 static Char8 cvsid_fields_hpp[] = OSGSWITCHMATERIALFIELDS_HEADER_CVSID; 00383 } 00384 00385 #ifdef __sgi 00386 #pragma reset woff 1174 00387 #endif 00388
1.5.5