1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "nel/3d/water_pool_manager.h"
20 #include "nel/3d/texture_blend.h"
21 #include "nel/3d/water_shape.h"
22 #include "nel/misc/command.h"
23 #include "nel/3d/water_height_map.h"
34 NLMISC_COMMAND(setWaterPool, "Setup a pool of water in the water pool manager",
35 "<ID>,<Size>,<Damping>,<FilterWeight>,<UnitSize>,<WaveIntensity>,<WavePeriod>")
37 CWaterPoolManager::CWaterHeightMapBuild whmb;
38 const uint numArgs = args.size();
39 if (numArgs == 0) return false;
42 NLMISC::fromString(args[0], whmb.ID);
46 NLMISC::fromString(args[1], whmb.Size);
50 NLMISC::fromString(args[2], whmb.FilterWeight);
54 NLMISC::fromString(args[3], whmb.UnitSize);
58 NLMISC::fromString(args[4], whmb.WaveIntensity);
62 NLMISC::fromString(args[5], whmb.WavePeriod);
64 // create the water pool
65 GetWaterPoolManager().createWaterPool(whmb);
70 //===============================================================================================
72 CWaterPoolManager
&GetWaterPoolManager()
74 static CWaterPoolManager singleton
;
78 //===============================================================================================
80 bool CWaterPoolManager::hasPool(uint32 ID
) const
82 return _PoolMap
.count(ID
) != 0;
85 //===============================================================================================
87 CWaterHeightMap
*CWaterPoolManager::createWaterPool(const CWaterHeightMapBuild
¶ms
)
89 CWaterHeightMap
*whm
= _PoolMap
.count(params
.ID
) == 0 ? new CWaterHeightMap
: _PoolMap
[params
.ID
];
90 whm
->setDamping(params
.Damping
);
91 whm
->setFilterWeight(params
.FilterWeight
);
92 whm
->setSize(params
.Size
);
93 whm
->setUnitSize(params
.UnitSize
);
94 whm
->setWaves(params
.WaveIntensity
, params
.WavePeriod
, params
.WaveRadius
, params
.BorderWaves
);
95 whm
->enableWaves(params
.WavesEnabled
);
96 whm
->setName(params
.Name
);
97 _PoolMap
[params
.ID
] = whm
; // in case it was just created
101 //===============================================================================================
103 CWaterHeightMap
&CWaterPoolManager::getPoolByID(uint32 ID
)
105 if(_PoolMap
.count(ID
))
107 return *_PoolMap
[ID
];
111 return *createWaterPool();
115 //===============================================================================================
117 void CWaterPoolManager::reset()
119 for (TPoolMap::iterator it
= _PoolMap
.begin(); it
!= _PoolMap
.end(); ++it
)
127 //===============================================================================================
129 void CWaterPoolManager::registerWaterShape(CWaterShape
*shape
)
131 nlassert(std::find(_WaterShapes
.begin(), _WaterShapes
.end(), shape
) == _WaterShapes
.end()); // Shape registered twice!
132 _WaterShapes
.push_back(shape
);
135 //===============================================================================================
137 void CWaterPoolManager::unRegisterWaterShape(CWaterShape
*shape
)
139 TWaterShapeVect::iterator it
= std::find(_WaterShapes
.begin(), _WaterShapes
.end(), shape
);
140 // nlassert(it != _WaterShapes.end()); // shape not registered!
141 if (it
!= _WaterShapes
.end())
142 _WaterShapes
.erase(it
);
145 //===============================================================================================
146 void CWaterPoolManager::setBlendFactor(IDriver
*drv
, float factor
)
148 nlassert(factor
>= 0 && factor
<= 1);
149 for (TWaterShapeVect::iterator it
= _WaterShapes
.begin(); it
!= _WaterShapes
.end(); ++it
)
152 for (uint k
= 0; k
< 2; ++k
)
154 tb
= dynamic_cast<CTextureBlend
*>((*it
)->getEnvMap(k
));
155 if (tb
&& tb
->setBlendFactor((uint16
) (256.f
* factor
)))
157 tb
->setReleasable(false);
158 drv
->setupTexture(*tb
);
164 //===============================================================================================
165 void CWaterPoolManager::releaseBlendTextures()
167 for (TWaterShapeVect::iterator it
= _WaterShapes
.begin(); it
!= _WaterShapes
.end(); ++it
)
170 for (uint k
= 0; k
< 2; ++k
)
172 tb
= dynamic_cast<CTextureBlend
*>((*it
)->getEnvMap(k
));
175 tb
->setReleasable(true);
182 //===============================================================================================
184 bool CWaterPoolManager::isWaterShapeObserver(const CWaterShape
*shape
) const
186 return std::find(_WaterShapes
.begin(), _WaterShapes
.end(), shape
) != _WaterShapes
.end();
189 //===============================================================================================
191 uint
CWaterPoolManager::getNumPools() const
193 return (uint
)_PoolMap
.size();
196 //===============================================================================================
198 uint
CWaterPoolManager::getPoolID(uint i
) const
200 nlassert(i
< getNumPools());
201 TPoolMap::const_iterator it
= _PoolMap
.begin();
206 //===============================================================================================
208 void CWaterPoolManager::removePool(uint32 ID
)
210 nlassert(hasPool(ID
));
211 TPoolMap::iterator it
= _PoolMap
.find(ID
);
213 _PoolMap
.erase(_PoolMap
.find(ID
));
216 //===============================================================================================
217 void CWaterPoolManager::serial(NLMISC::IStream
&f
)
219 f
.xmlPush("WaterPoolManager");
220 (void)f
.serialVersion(0);
222 TPoolMap::iterator it
;
225 size
= (uint32
)_PoolMap
.size();
226 it
= _PoolMap
.begin();
232 f
.xmlSerial(size
, "NUM_POOLS");
235 f
.xmlPush("PoolDesc");
238 CWaterHeightMap
*whm
= NULL
;
240 f
.xmlSerial(id
, "POOL_ID");
246 uint32 id
= it
->first
;
247 f
.xmlSerial(id
, "POOL_ID");
248 f
.serialPtr(it
->second
);