# 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  

water_pool_manager.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000, 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/water_pool_manager.h"
00029 #include "3d/texture_blend.h"
00030 #include "3d/water_shape.h"
00031 #include "nel/misc/command.h"
00032 #include "water_height_map.h"
00033 
00034 
00035 
00036 namespace NL3D {
00037 
00038 
00039 /*
00040 NLMISC_COMMAND(setWaterPool, "Setup a pool of water in the water pool manager",
00041                            "<ID>,<Size>,<Damping>,<FilterWeight>,<UnitSize>,<WaveIntensity>,<WavePeriod>")
00042 {
00043         CWaterPoolManager::CWaterHeightMapBuild whmb;
00044         const uint numArgs = args.size();
00045         if (numArgs == 0) return false;
00046         if (numArgs == 1)
00047         {
00048                 whmb.ID = ::atoi(args[0].c_str());
00049         }
00050         if (numArgs == 2)
00051         {
00052                 whmb.Size = ::atoi(args[1].c_str());
00053         }
00054         if (numArgs == 3)
00055         {
00056                 whmb.FilterWeight = ::atof(args[2].c_str());
00057         }
00058         if (numArgs == 4)
00059         {
00060                 whmb.UnitSize = ::atof(args[3].c_str());
00061         }
00062         if (numArgs == 5)
00063         {
00064                 whmb.WaveIntensity = ::atof(args[4].c_str());
00065         }
00066         if (numArgs == 4)
00067         {
00068                 whmb.WavePeriod = ::atof(args[5].c_str());
00069         }
00070         // create the water pool
00071         GetWaterPoolManager().createWaterPool(whmb);    
00072         return true;
00073 }
00074 */
00075 
00076 //===============================================================================================
00077 
00078 CWaterPoolManager &GetWaterPoolManager()
00079 {
00080         static CWaterPoolManager singleton;
00081         return singleton;
00082 }
00083 
00084 //===============================================================================================
00085 
00086 bool    CWaterPoolManager::hasPool(uint32 ID) const
00087 {
00088         return _PoolMap.count(ID) != 0;
00089 }
00090 
00091 //===============================================================================================
00092 
00093 CWaterHeightMap *CWaterPoolManager::createWaterPool(const CWaterHeightMapBuild &params)
00094 {
00095         CWaterHeightMap *whm = _PoolMap.count(params.ID) == 0 ? new CWaterHeightMap : _PoolMap[params.ID];      
00096         whm->setDamping(params.Damping);
00097         whm->setFilterWeight(params.FilterWeight);
00098         whm->setSize(params.Size);
00099         whm->setUnitSize(params.UnitSize);
00100         whm->setWaves(params.WaveIntensity, params.WavePeriod, params.WaveRadius, params.BorderWaves);
00101         whm->enableWaves(params.WavesEnabled);
00102         whm->setName(params.Name);
00103         _PoolMap[params.ID] = whm; // in case it was just created
00104         return whm;
00105 }
00106 
00107 //===============================================================================================
00108 
00109 CWaterHeightMap &CWaterPoolManager::getPoolByID(uint32 ID)
00110 {
00111         if(_PoolMap.count(ID))
00112         {
00113                 return *_PoolMap[ID];
00114         }
00115         else
00116         {
00117                 return *createWaterPool();      
00118         }
00119 }
00120         
00121 //===============================================================================================
00122 
00123 void CWaterPoolManager::reset()
00124 {
00125         for (TPoolMap::iterator it = _PoolMap.begin(); it != _PoolMap.end(); ++it)
00126         {
00127                 delete it->second;
00128         }
00129 }
00130         
00131 
00132 //===============================================================================================
00133 
00134 void CWaterPoolManager::registerWaterShape(CWaterShape *shape)
00135 {       
00136         nlassert(std::find(_WaterShapes.begin(), _WaterShapes.end(), shape) == _WaterShapes.end()); // Shape registered twice!
00137         _WaterShapes.push_back(shape);
00138 }
00139 
00140 //===============================================================================================
00141 
00142 void CWaterPoolManager::unRegisterWaterShape(CWaterShape *shape)
00143 {
00144         TWaterShapeVect::iterator it = std::find(_WaterShapes.begin(), _WaterShapes.end(), shape);
00145 //      nlassert(it != _WaterShapes.end()); // shape not registered!
00146         if (it != _WaterShapes.end())
00147                 _WaterShapes.erase(it);
00148 }
00149 
00150 //===============================================================================================
00151 
00152 void CWaterPoolManager::setBlendFactor(IDriver *drv, float factor)
00153 {
00154         nlassert(factor >= 0 && factor <= 1);
00155         for (TWaterShapeVect::iterator it = _WaterShapes.begin(); it != _WaterShapes.end(); ++it)
00156         {
00157                 CTextureBlend *tb;
00158                 for (uint k = 0; k < 2; ++k)
00159                 {
00160                         tb = dynamic_cast<CTextureBlend *>((*it)->getEnvMap(k));
00161                         if (tb && tb->setBlendFactor((uint16) (256.f * factor)))
00162                         {
00163                                 drv->setupTexture(*tb);
00164                         }
00165                 }
00166         }
00167 }
00168 
00169 //===============================================================================================
00170 
00171 bool CWaterPoolManager::isWaterShapeObserver(const CWaterShape *shape) const
00172 {
00173         return std::find(_WaterShapes.begin(), _WaterShapes.end(), shape) != _WaterShapes.end();
00174 }
00175 
00176 //===============================================================================================
00177 
00178 uint            CWaterPoolManager::getNumPools() const
00179 {
00180         return _PoolMap.size();
00181 }
00182 
00183 //===============================================================================================
00184 
00185 uint            CWaterPoolManager::getPoolID(uint i) const
00186 {
00187         nlassert(i < getNumPools());
00188         TPoolMap::const_iterator it =  _PoolMap.begin();
00189         while (i--) ++it;
00190         return it->first;
00191 }
00192 
00193 //===============================================================================================
00194 
00195 void    CWaterPoolManager::removePool(uint32 ID)
00196 {
00197         nlassert(hasPool(ID));
00198         TPoolMap::iterator it = _PoolMap.find(ID);
00199         delete it->second;
00200         _PoolMap.erase(_PoolMap.find(ID));
00201 }
00202 
00203 //===============================================================================================
00204 void CWaterPoolManager::serial(NLMISC::IStream &f)  throw(NLMISC::EStream)
00205 {       
00206         f.xmlPush("WaterPoolManager");
00207         (void)f.serialVersion(0);       
00208         uint32 size;
00209         TPoolMap::iterator it;
00210         if (!f.isReading())
00211         {
00212                 size = _PoolMap.size();
00213                 it  = _PoolMap.begin();
00214         }
00215         else
00216         {
00217                 reset();
00218         }
00219         f.xmlSerial(size, "NUM_POOLS");
00220         while (size --)
00221         {               
00222                 f.xmlPush("PoolDesc");
00223                 if (f.isReading())
00224                 {
00225                         CWaterHeightMap *whm;
00226                         uint32 id;
00227                         f.xmlSerial(id, "POOL_ID");
00228                         f.serialPtr(whm);
00229                         _PoolMap[id] = whm;     
00230                 }
00231                 else
00232                 {
00233                         uint32 id = it->first;
00234                         f.xmlSerial(id, "POOL_ID");
00235                         f.serialPtr(it->second);
00236                         ++it;
00237                 }
00238                 f.xmlPop();
00239         }
00240         f.xmlPop();             
00241 }
00242 
00243 } // NL3D