# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

instance_group_user.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 
00028 #include "3d/instance_user.h"
00029 #include "nel/misc/debug.h"
00030 #include "3d/instance_group_user.h"
00031 #include "3d/scene_user.h"
00032 #include "3d/mesh_multi_lod_instance.h"
00033 #include "nel/misc/path.h"
00034 #include "nel/misc/file.h"
00035 
00036 using namespace NLMISC;
00037 using namespace std;
00038 
00039 namespace NL3D 
00040 {
00041 
00042 // ***************************************************************************
00043 
00044 UInstanceGroup  *UInstanceGroup::createInstanceGroup (const std::string &instanceGroup)
00045 {
00046         NL3D_MEM_IG
00047         // Create the instance group
00048         CInstanceGroupUser *user=new CInstanceGroupUser;
00049 
00050         // Init the class
00051         if (!user->init (instanceGroup))
00052         {
00053                 // Prb, erase it
00054                 delete user;
00055 
00056                 // Return error code
00057                 return NULL;
00058         }
00059 
00060         // return the good value
00061         return user;
00062 }
00063 
00064 // ***************************************************************************
00065 
00066 void UInstanceGroup::createInstanceGroupAsync (const std::string &instanceGroup, UInstanceGroup **pIG)
00067 {
00068         NL3D_MEM_IG
00069         CAsyncFileManager3D::getInstance().loadIGUser (instanceGroup, pIG);
00070 }
00071 
00072 // ***************************************************************************
00073 
00074 void UInstanceGroup::stopCreateInstanceGroupAsync (UInstanceGroup **ppIG)
00075 {
00076         NL3D_MEM_IG
00077         // Theorically should stop the async file manager but the async file manager can only be stopped
00078         // between tasks (a file reading) so that is no sense to do anything here
00079         while (*ppIG == NULL)
00080         {
00081                 nlSleep (2);
00082         }
00083         if (*ppIG != (UInstanceGroup*)-1)
00084         {
00085                 delete *ppIG;
00086         }
00087 }
00088 
00089 // ***************************************************************************
00090 CInstanceGroupUser::CInstanceGroupUser()
00091 {
00092         NL3D_MEM_IG
00093         _AddToSceneState = StateNotAdded;
00094 }
00095 
00096 // ***************************************************************************
00097 bool CInstanceGroupUser::init (const std::string &instanceGroup)
00098 {
00099         NL3D_MEM_IG
00100         // Create a file
00101         CIFile file;
00102         std::string path = CPath::lookup (instanceGroup, false);
00103         if (!path.empty() && file.open (path))
00104         {
00105                 // Serialize this class
00106                 try
00107                 {
00108                         // Read the class
00109                         _InstanceGroup.serial (file);
00110                 }
00111                 catch (EStream& e)
00112                 {
00113                         // Avoid visual warning
00114                         EStream ee=e;
00115 
00116                         // Serial problem
00117                         return false;
00118                 }
00119         }
00120         else
00121         {
00122                 // Failed.
00123                 return false;
00124         }
00125 
00126         // Ok
00127         return true;
00128 }
00129 
00130 // ***************************************************************************
00131 void CInstanceGroupUser::setTransformNameCallback (ITransformName *pTN)
00132 {
00133         NL3D_MEM_IG
00134         _InstanceGroup.setTransformNameCallback (pTN);
00135 }
00136 
00137 
00138 // ***************************************************************************
00139 void CInstanceGroupUser::setAddRemoveInstanceCallback(IAddRemoveInstance *callback)
00140 {
00141         NL3D_MEM_IG
00142         _InstanceGroup.setAddRemoveInstanceCallback(callback);
00143 }
00144 
00145 
00146 // ***************************************************************************
00147 void CInstanceGroupUser::setIGAddBeginCallback(IIGAddBegin *callback)
00148 {
00149         NL3D_MEM_IG
00150         _InstanceGroup.setIGAddBeginCallback(callback);
00151 }
00152 
00153 
00154 
00155 // ***************************************************************************
00156 void CInstanceGroupUser::addToScene (class UScene& scene, UDriver *driver)
00157 {
00158         NL3D_MEM_IG
00159         // Get driver pointer
00160         IDriver *cDriver= driver ? NLMISC::safe_cast<CDriverUser*>(driver)->getDriver() : NULL;
00161 
00162         // Add to the scene
00163         addToScene (((CSceneUser*)&scene)->getScene(), cDriver);
00164 }
00165 
00166 // ***************************************************************************
00167 void CInstanceGroupUser::getInstanceMatrix(uint instanceNb,NLMISC::CMatrix &dest) const
00168 {
00169         NL3D_MEM_IG
00170         _InstanceGroup.getInstanceMatrix(instanceNb, dest);     
00171 }
00172 
00173 
00174 // ***************************************************************************
00175 void CInstanceGroupUser::addToScene (class CScene& scene, IDriver *driver)
00176 {
00177         NL3D_MEM_IG
00178         _InstanceGroup.addToScene (scene, driver);
00179         // Fill in the map accelerating search of instance by names
00180         for( uint32 i = 0; i < _InstanceGroup._Instances.size(); ++i)
00181         {
00182                 CInstanceUser *pIU = NULL;
00183                 string stmp;
00184                 if (_InstanceGroup._Instances[i] != NULL)
00185                 {
00186                         pIU = new CInstanceUser (&scene, _InstanceGroup._Instances[i]);
00187                         stmp = _InstanceGroup.getInstanceName (i);
00188                         _Instances.insert (map<string,CInstanceUser*>::value_type(stmp, pIU));
00189                 }
00190         }
00191 }
00192 
00193 // ***************************************************************************
00194 void CInstanceGroupUser::addToSceneAsync (class UScene& scene, UDriver *driver)
00195 {
00196         NL3D_MEM_IG
00197         IDriver *cDriver= driver ? NLMISC::safe_cast<CDriverUser*>(driver)->getDriver() : NULL;
00198         // Add to the scene
00199         _InstanceGroup.addToSceneAsync (((CSceneUser*)&scene)->getScene(), cDriver);
00200         _AddToSceneState = StateAdding;
00201         _AddToSceneTempScene = &scene;
00202         _AddToSceneTempDriver = driver;
00203 }
00204 
00205 // ***************************************************************************
00206 void CInstanceGroupUser::stopAddToSceneAsync ()
00207 {
00208         NL3D_MEM_IG
00209         _InstanceGroup.stopAddToSceneAsync ();
00210 }
00211 
00212 // ***************************************************************************
00213 UInstanceGroup::TState CInstanceGroupUser::getAddToSceneState ()
00214 {
00215         NL3D_MEM_IG
00216         UInstanceGroup::TState newState = (UInstanceGroup::TState)_InstanceGroup.getAddToSceneState ();
00217         if ((_AddToSceneState == StateAdding) && (newState == StateAdded))
00218         {
00219                 // Fill in the map accelerating search of instance by names
00220                 for( uint32 i = 0; i < _InstanceGroup._Instances.size(); ++i)
00221                 {
00222                         CInstanceUser *pIU = NULL;
00223                         string stmp;
00224                         if (_InstanceGroup._Instances[i] != NULL)
00225                         {
00226                                 pIU = new CInstanceUser (&((CSceneUser*)_AddToSceneTempScene)->getScene(), _InstanceGroup._Instances[i]);
00227                                 stmp = _InstanceGroup.getInstanceName (i);
00228                                 _Instances.insert (map<string,CInstanceUser*>::value_type(stmp, pIU));
00229                         }
00230                 }
00231                 _AddToSceneState = StateAdded;
00232         }
00233         return newState;
00234 }
00235 
00236 // ***************************************************************************
00237 void CInstanceGroupUser::removeFromScene (class UScene& scene)
00238 {
00239         NL3D_MEM_IG
00240         _InstanceGroup.removeFromScene (((CSceneUser*)&scene)->getScene());
00241         // Remove all instance in the map
00242         map<string,CInstanceUser*>::iterator it = _Instances.begin();
00243         while (it != _Instances.end())
00244         {
00245                 map<string,CInstanceUser*>::iterator itDel = it;
00246                 ++it;
00247                 _Instances.erase (itDel);
00248         }
00249 }
00250 
00251 // ***************************************************************************
00252 uint CInstanceGroupUser::getNumInstance () const
00253 {
00254         NL3D_MEM_IG
00255         return _InstanceGroup.getNumInstance ();
00256 }
00257 
00258 // ***************************************************************************
00259 
00260 const std::string& CInstanceGroupUser::getShapeName (uint instanceNb) const
00261 {
00262         NL3D_MEM_IG
00263         // Check args
00264         if (instanceNb>=_InstanceGroup.getNumInstance ())
00265                 nlerror("getShapeName*(): bad instance Id");
00266         
00267         return _InstanceGroup.getShapeName (instanceNb);
00268 }
00269 
00270 // ***************************************************************************
00271 const std::string& CInstanceGroupUser::getInstanceName (uint instanceNb) const
00272 {
00273         NL3D_MEM_IG
00274         // Check args
00275         if (instanceNb>=_InstanceGroup.getNumInstance ())
00276                 nlerror("getInstanceName*(): bad instance Id");
00277         
00278         return _InstanceGroup.getInstanceName (instanceNb);
00279 }
00280 
00281 // ***************************************************************************
00282 const NLMISC::CVector& CInstanceGroupUser::getInstancePos (uint instanceNb) const
00283 {
00284         NL3D_MEM_IG
00285         // Check args
00286         if (instanceNb>=_InstanceGroup.getNumInstance ())
00287                 nlerror("getInstancePos*(): bad instance Id");
00288         
00289         return _InstanceGroup.getInstancePos (instanceNb);
00290 }
00291 
00292 // ***************************************************************************
00293 const NLMISC::CQuat& CInstanceGroupUser::getInstanceRot (uint instanceNb) const
00294 {
00295         NL3D_MEM_IG
00296         // Check args
00297         if (instanceNb>=_InstanceGroup.getNumInstance ())
00298                 nlerror("getInstanceRot*(): bad instance Id");
00299         
00300         return _InstanceGroup.getInstanceRot (instanceNb);
00301 }
00302 
00303 // ***************************************************************************
00304 const NLMISC::CVector& CInstanceGroupUser::getInstanceScale (uint instanceNb) const
00305 {
00306         NL3D_MEM_IG
00307         // Check args
00308         if (instanceNb>=_InstanceGroup.getNumInstance ())
00309                 nlerror("getInstanceScale*(): bad instance Id");
00310         
00311         return _InstanceGroup.getInstanceScale (instanceNb);
00312 }
00313 
00314 
00315 
00316 // ***************************************************************************
00317 UInstance *CInstanceGroupUser::getByName (std::string &name)
00318 {
00319         NL3D_MEM_IG
00320         map<string,CInstanceUser*>::iterator it = _Instances.find (name);
00321         if (it != _Instances.end())
00322                 return it->second;
00323         else
00324                 return NULL;
00325 }
00326 
00327 // ***************************************************************************
00328 
00329 const UInstance *CInstanceGroupUser::getByName (std::string &name) const
00330 {
00331         NL3D_MEM_IG
00332         map<string,CInstanceUser*>::const_iterator it = _Instances.find (name);
00333         if (it != _Instances.end())
00334                 return it->second;
00335         else
00336                 return NULL;
00337 }
00338 
00339 // ***************************************************************************
00340 
00341 void CInstanceGroupUser::setLightFactor (const std::string &LightName, CRGBA nFactor)
00342 {
00343         NL3D_MEM_IG
00344         _InstanceGroup.setLightFactor (LightName, nFactor);
00345 }
00346 
00347 // ***************************************************************************
00348 void CInstanceGroupUser::setBlendShapeFactor (const std::string &bsName, float rFactor)
00349 {
00350         NL3D_MEM_IG
00351         _InstanceGroup.setBlendShapeFactor (bsName, rFactor);
00352 }
00353 
00354 // ***************************************************************************
00355 
00356 void CInstanceGroupUser::createRoot (UScene &scene)
00357 {
00358         NL3D_MEM_IG
00359         _InstanceGroup.createRoot (((CSceneUser*)&scene)->getScene());
00360 }
00361 
00362 // ***************************************************************************
00363 void CInstanceGroupUser::setClusterSystem (UInstanceGroup *pClusterSystem)
00364 {
00365         NL3D_MEM_IG
00366         _InstanceGroup.setClusterSystem (&((CInstanceGroupUser*)pClusterSystem)->_InstanceGroup);
00367 }
00368 
00369 // ***************************************************************************
00370 bool CInstanceGroupUser::linkToParentCluster(UInstanceGroup *father)
00371 {
00372         NL3D_MEM_IG
00373         if (father)
00374                 return _InstanceGroup.linkToParent(&(NLMISC::safe_cast<CInstanceGroupUser *>(father)->_InstanceGroup));
00375         else
00376         {
00377                 nlwarning("Trying to link a cluster system to a NULL parent cluster");
00378                 return false;
00379         }
00380 }
00381 
00382 // ***************************************************************************
00383 void CInstanceGroupUser::getDynamicPortals (std::vector<std::string> &names)
00384 {
00385         NL3D_MEM_IG
00386         _InstanceGroup.getDynamicPortals (names);
00387 }
00388 
00389 // ***************************************************************************
00390 void CInstanceGroupUser::setDynamicPortal (std::string& name, bool opened)
00391 {
00392         NL3D_MEM_IG
00393         _InstanceGroup.setDynamicPortal (name, opened);
00394 }
00395 
00396 // ***************************************************************************
00397 bool CInstanceGroupUser::getDynamicPortal (std::string& name)
00398 {
00399         NL3D_MEM_IG
00400         return _InstanceGroup.getDynamicPortal (name);
00401 }
00402 
00403 // ***************************************************************************
00404 void CInstanceGroupUser::setPos (const NLMISC::CVector &pos)
00405 {
00406         NL3D_MEM_IG
00407         _InstanceGroup.setPos (pos);
00408 }
00409 
00410 // ***************************************************************************
00411 void CInstanceGroupUser::setRotQuat (const NLMISC::CQuat &q)
00412 {
00413         NL3D_MEM_IG
00414         _InstanceGroup.setRotQuat (q);
00415 }
00416 
00417 // ***************************************************************************
00418 CVector CInstanceGroupUser::getPos ()
00419 {
00420         NL3D_MEM_IG
00421         return _InstanceGroup.getPos ();
00422 }
00423 
00424 // ***************************************************************************
00425 CQuat CInstanceGroupUser::getRotQuat ()
00426 {
00427         NL3D_MEM_IG
00428         return _InstanceGroup.getRotQuat();
00429 }
00430 
00431 
00432 // ***************************************************************************
00433 void                    CInstanceGroupUser::freezeHRC()
00434 {
00435         NL3D_MEM_IG
00436         _InstanceGroup.freezeHRC();
00437 }
00438 
00439 // ***************************************************************************
00440 void                    CInstanceGroupUser::unfreezeHRC()
00441 {
00442         NL3D_MEM_IG
00443         _InstanceGroup.unfreezeHRC();
00444 }
00445 
00446 
00447 // ***************************************************************************
00448 void                    CInstanceGroupUser::setPointLightFactor(const std::string &lightGroupName, NLMISC::CRGBA nFactor)
00449 {
00450         NL3D_MEM_IG
00451         _InstanceGroup.setPointLightFactor(lightGroupName, nFactor);
00452 }
00453 
00454 // ***************************************************************************
00455 bool                    CInstanceGroupUser::getStaticLightSetup(
00456                 const std::string &retrieverIdentifier, sint surfaceId, const NLMISC::CVector &localPos, 
00457                 std::vector<CPointLightInfluence> &pointLightList, uint8 &sunContribution, NLMISC::CRGBA &localAmbient)
00458 {
00459         NL3D_MEM_IG
00460         return _InstanceGroup.getStaticLightSetup(retrieverIdentifier, surfaceId, localPos, pointLightList, 
00461                 sunContribution, localAmbient);
00462 }
00463 
00464 // ***************************************************************************
00465 /*virtual*/ void CInstanceGroupUser::setDistMax(uint instance, float dist)
00466 {
00467         if (instance > _InstanceGroup.getNumInstance())
00468         {
00469                 nlwarning("CInstanceGroupUser::setDistMax : instance index %d is invalid", instance);
00470                 return;
00471         }
00472         if (_InstanceGroup._Instances[instance]) _InstanceGroup._Instances[instance]->setDistMax(dist); 
00473 }
00474 
00475 // ***************************************************************************
00476 /*virtual*/ float CInstanceGroupUser::getDistMax(uint instance) const
00477 {
00478         if (instance > _InstanceGroup.getNumInstance())
00479         {
00480                 nlwarning("CInstanceGroupUser::getDistMax : instance index %d is invalid", instance);
00481                 return -1.f;
00482         }
00483         if (_InstanceGroup._Instances[instance]) return _InstanceGroup._Instances[instance]->getDistMax();
00484         else return -1.f;
00485 }
00486 
00487 // ***************************************************************************
00488 /*virtual*/ void CInstanceGroupUser::setCoarseMeshDist(uint instance, float dist)
00489 {
00490         if (instance > _InstanceGroup.getNumInstance())
00491         {
00492                 nlwarning("CInstanceGroupUser::setCoarseMeshDist : instance index %d is invalid", instance);
00493                 return;
00494         }
00495         if (_InstanceGroup._Instances[instance]) 
00496         {       
00497                 CMeshMultiLodInstance *mmli = dynamic_cast<CMeshMultiLodInstance *>(_InstanceGroup._Instances[instance]);
00498                 if (mmli) mmli->setCoarseMeshDist(dist);
00499         }
00500 }
00501 
00502 // ***************************************************************************
00503 /*virtual*/ float CInstanceGroupUser::getCoarseMeshDist(uint instance) const
00504 {
00505         if (instance > _InstanceGroup.getNumInstance())
00506         {
00507                 nlwarning("getCoarseMeshDist::getDistMax : instance index %d is invalid", instance);
00508                 return -1.f;
00509         }
00510         if (_InstanceGroup._Instances[instance]) 
00511         {
00512                 CMeshMultiLodInstance *mmli = dynamic_cast<CMeshMultiLodInstance *>(_InstanceGroup._Instances[instance]);
00513                 if (mmli) return mmli->getCoarseMeshDist();
00514                 else return -1.f;
00515         }               
00516         else return -1.f;
00517 }
00518 
00519 
00520 } // NL3D