00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "std3d.h"
00027
00028 #include "nel/3d/landscapeig_manager.h"
00029 #include "nel/3d/u_scene.h"
00030 #include "nel/3d/u_instance_group.h"
00031 #include "nel/misc/common.h"
00032 #include "nel/misc/debug.h"
00033 #include "nel/misc/path.h"
00034 #include "nel/misc/file.h"
00035
00036
00037 #include <fstream>
00038
00039
00040 using namespace NLMISC;
00041 using namespace std;
00042
00043
00044 namespace NL3D
00045 {
00046
00047
00048
00049 CLandscapeIGManager::CInstanceGroupElement::CInstanceGroupElement(UInstanceGroup *ig, const char *fileName)
00050 {
00051 NL3D_MEM_LANDSCAPE_IG
00052 Ig = ig;
00053 AddedToScene = false;
00054 if (fileName != NULL)
00055 FileName = fileName;
00056 }
00057
00058
00059 void CLandscapeIGManager::CInstanceGroupElement::release()
00060 {
00061 NL3D_MEM_LANDSCAPE_IG
00062 delete Ig;
00063 Ig= NULL;
00064 }
00065
00066
00067
00068 CLandscapeIGManager::CLandscapeIGManager()
00069 {
00070 NL3D_MEM_LANDSCAPE_IG
00071 _Scene=NULL;
00072 }
00073
00074 CLandscapeIGManager::~CLandscapeIGManager()
00075 {
00076 NL3D_MEM_LANDSCAPE_IG
00077
00078 if(_Scene!=NULL)
00079 throw Exception("CLandscapeIGManager not reseted");
00080 }
00081
00082 void CLandscapeIGManager::initIG(UScene *scene, const std::string &igDesc)
00083 {
00084 NL3D_MEM_LANDSCAPE_IG
00085 nlassert(scene);
00086 _Scene= scene;
00087
00088
00089 if(igDesc.empty())
00090 return;
00091
00092 string igFile = CPath::lookup(igDesc);
00093
00094
00095
00096 CIFile file;
00097
00098
00099
00100 if (file.open (igFile))
00101 {
00102 char tmpBuff[260];
00103 char delimiterBox[] = "\t";
00104
00105 while(!file.eof())
00106 {
00107
00108 file.getline(tmpBuff, 260);
00109 char *token = strtok(tmpBuff, delimiterBox);
00110
00111 if(token != NULL)
00112 {
00113 if( _ZoneInstanceGroupMap.find(token)!=_ZoneInstanceGroupMap.end() )
00114 throw Exception("CLandscapeIGManager::initIG() found 2 igs with same name in %s", igFile.c_str());
00115 else
00116 {
00117
00118 UInstanceGroup *ig = UInstanceGroup::createInstanceGroup(token);
00119 if (ig)
00120 {
00121
00122 string tokId= token;
00123 strupr(tokId);
00124 _ZoneInstanceGroupMap[tokId]= CInstanceGroupElement(ig, token);
00125 }
00126 else
00127 {
00128 nlwarning ("CLandscapeIGManager::initIG() Can't load instance group '%s' in '%s'", token, igFile.c_str());
00129 }
00130 }
00131 }
00132 }
00133 file.close();
00134 }
00135 else
00136 {
00137 nlwarning ("Couldn't load '%s'", igFile.c_str());
00138 }
00139 }
00140
00141 UInstanceGroup *CLandscapeIGManager::loadZoneIG(const std::string &name)
00142 {
00143 NL3D_MEM_LANDSCAPE_IG
00144 if(name=="")
00145 return NULL;
00146
00147
00148 ItZoneInstanceGroupMap it;
00149 it= _ZoneInstanceGroupMap.find( translateName(name) );
00150
00151
00152 if( it!= _ZoneInstanceGroupMap.end() )
00153 {
00154
00155 if( !it->second.AddedToScene )
00156 {
00157
00158 if (it->second.Ig != NULL)
00159 {
00160 it->second.Ig->addToScene(*_Scene);
00161 it->second.AddedToScene= true;
00162 }
00163 }
00164 return it->second.Ig;
00165 }
00166 else
00167 {
00168 return NULL;
00169 }
00170 }
00171
00172 void CLandscapeIGManager::loadArrayZoneIG(const std::vector<std::string> &names, std::vector<UInstanceGroup *> *dest )
00173 {
00174 NL3D_MEM_LANDSCAPE_IG
00175 if (dest)
00176 {
00177 dest->clear();
00178 dest->reserve(names.size());
00179 }
00180 for(uint i=0; i<names.size(); i++)
00181 {
00182 UInstanceGroup *ig = loadZoneIG(names[i]);
00183 if (dest && ig)
00184 {
00185 dest->push_back(ig);
00186 }
00187 }
00188 }
00189
00190
00191 void CLandscapeIGManager::unloadArrayZoneIG(const std::vector<std::string> &names)
00192 {
00193 NL3D_MEM_LANDSCAPE_IG
00194 for(uint i=0; i<names.size(); i++)
00195 {
00196 unloadZoneIG(names[i]);
00197 }
00198 }
00199
00200
00201 void CLandscapeIGManager::unloadZoneIG(const std::string &name)
00202 {
00203 NL3D_MEM_LANDSCAPE_IG
00204 if(name=="")
00205 return;
00206
00207
00208 ItZoneInstanceGroupMap it;
00209 it= _ZoneInstanceGroupMap.find( translateName(name) );
00210
00211
00212 if( it!= _ZoneInstanceGroupMap.end() )
00213 {
00214
00215 if( it->second.AddedToScene )
00216 {
00217
00218 it->second.Ig->removeFromScene(*_Scene);
00219 it->second.AddedToScene= false;
00220 }
00221 }
00222 }
00223
00224
00225 bool CLandscapeIGManager::isIGAddedToScene(const std::string &name) const
00226 {
00227 NL3D_MEM_LANDSCAPE_IG
00228 if(name=="")
00229 return false;
00230
00231
00232 ConstItZoneInstanceGroupMap it;
00233 it= _ZoneInstanceGroupMap.find( translateName(name) );
00234
00235
00236 if( it!= _ZoneInstanceGroupMap.end() )
00237 return it->second.AddedToScene;
00238 else
00239 return false;
00240 }
00241
00242
00243 UInstanceGroup *CLandscapeIGManager::getIG(const std::string &name) const
00244 {
00245 NL3D_MEM_LANDSCAPE_IG
00246 if(name=="")
00247 return NULL;
00248
00249
00250 ConstItZoneInstanceGroupMap it;
00251 it= _ZoneInstanceGroupMap.find( translateName(name) );
00252
00253
00254 if( it!= _ZoneInstanceGroupMap.end() )
00255 return it->second.Ig;
00256 else
00257 return NULL;
00258 }
00259
00260
00261
00262 std::string CLandscapeIGManager::translateName(const std::string &name) const
00263 {
00264 NL3D_MEM_LANDSCAPE_IG
00265 std::string ret;
00266 ret= name + ".ig";
00267 strupr(ret);
00268
00269 return ret;
00270 }
00271
00272
00273
00274 void CLandscapeIGManager::reset()
00275 {
00276 NL3D_MEM_LANDSCAPE_IG
00277 while( _ZoneInstanceGroupMap.begin() != _ZoneInstanceGroupMap.end() )
00278 {
00279 string name= _ZoneInstanceGroupMap.begin()->first;
00280
00281 unloadZoneIG( name.substr(0, name.find('.')) );
00282
00283
00284 _ZoneInstanceGroupMap.begin()->second.release();
00285 _ZoneInstanceGroupMap.erase(_ZoneInstanceGroupMap.begin());
00286 }
00287
00288 _Scene=NULL;
00289 }
00290
00291
00292
00293 void CLandscapeIGManager::reloadAllIgs()
00294 {
00295 NL3D_MEM_LANDSCAPE_IG
00296 vector<std::string> bkupIgFileNameList;
00297 vector<bool> bkupIgAddedToScene;
00298
00299
00300 while( _ZoneInstanceGroupMap.begin() != _ZoneInstanceGroupMap.end() )
00301 {
00302 string name= _ZoneInstanceGroupMap.begin()->first;
00303
00304
00305 bkupIgFileNameList.push_back(_ZoneInstanceGroupMap.begin()->second.FileName);
00306 bkupIgAddedToScene.push_back(_ZoneInstanceGroupMap.begin()->second.AddedToScene);
00307
00308
00309 unloadZoneIG( name.substr(0, name.find('.')) );
00310
00311
00312 _ZoneInstanceGroupMap.begin()->second.release();
00313 _ZoneInstanceGroupMap.erase(_ZoneInstanceGroupMap.begin());
00314 }
00315
00316
00317 for(uint i=0; i<bkupIgFileNameList.size(); i++)
00318 {
00319 const char *token= bkupIgFileNameList[i].c_str();
00320 UInstanceGroup *ig = UInstanceGroup::createInstanceGroup(token);
00321
00322 string tokId= token;
00323 strupr(tokId);
00324 _ZoneInstanceGroupMap[tokId]= CInstanceGroupElement(ig, token);
00325
00326
00327 if(bkupIgAddedToScene[i])
00328 {
00329 loadZoneIG( tokId.substr(0, tokId.find('.')) );
00330 }
00331 }
00332 }
00333
00334
00335
00336 void CLandscapeIGManager::getAllIG(std::vector<UInstanceGroup *> &dest) const
00337 {
00338 NL3D_MEM_LANDSCAPE_IG
00339 dest.clear();
00340 dest.reserve(_ZoneInstanceGroupMap.size());
00341
00342 for(TZoneInstanceGroupMap::const_iterator it = _ZoneInstanceGroupMap.begin(); it != _ZoneInstanceGroupMap.end(); ++it)
00343 {
00344 dest.push_back(it->second.Ig);
00345 }
00346 }
00347
00348
00349 void CLandscapeIGManager::getAllIGWithNames(std::vector<std::pair<UInstanceGroup *, std::string> > &dest) const
00350 {
00351 NL3D_MEM_LANDSCAPE_IG
00352 dest.clear();
00353 dest.reserve(_ZoneInstanceGroupMap.size());
00354
00355 for(TZoneInstanceGroupMap::const_iterator it = _ZoneInstanceGroupMap.begin(); it != _ZoneInstanceGroupMap.end(); ++it)
00356 {
00357 dest.push_back(std::make_pair(it->second.Ig, it->second.FileName));
00358 }
00359 }
00360
00361
00362 }