Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / 3d / u_instance.cpp
blobcb2a273edb9216222f3741faa1b2cf78c27d33b6
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #include "std3d.h"
19 #include "nel/3d/u_instance.h"
20 #include "nel/3d/transform_shape.h"
21 #include "nel/3d/mesh_base.h"
22 #include "nel/3d/driver_user.h"
23 #include "nel/3d/mesh_multi_lod_instance.h"
24 #include "nel/3d/seg_remanence.h"
25 #include "nel/misc/debug.h"
26 #include "nel/3d/scene.h"
27 #include "nel/3d/shape_bank.h"
30 using namespace NLMISC;
32 #ifdef DEBUG_NEW
33 #define new DEBUG_NEW
34 #endif
36 namespace NL3D
40 // ***************************************************************************
42 void UInstance::getShapeAABBox(NLMISC::CAABBox &bbox) const
44 CTransformShape *object = getObjectPtr();
45 object->getAABBox(bbox);
48 // ***************************************************************************
49 void UInstance::setBlendShapeFactor (const std::string &blendShapeName, float factor, bool /* dynamic */)
51 CTransformShape *object = getObjectPtr();
52 CMeshBaseInstance *mi= dynamic_cast<CMeshBaseInstance*>(object);
54 if (mi)
56 mi->setBlendShapeFactor (blendShapeName, factor);
60 // ***************************************************************************
61 void UInstance::changeMRMDistanceSetup(float distanceFinest, float distanceMiddle, float distanceCoarsest)
63 CTransformShape *object = getObjectPtr();
64 CMeshBaseInstance *mi= dynamic_cast<CMeshBaseInstance*>(object);
66 // Just for CMeshBaseInstance.
67 if (mi)
69 mi->changeMRMDistanceSetup(distanceFinest, distanceMiddle, distanceCoarsest);
74 // ***************************************************************************
75 void UInstance::setShapeDistMax(float distMax)
77 CTransformShape *object = getObjectPtr();
78 if(object && object->Shape)
80 object->Shape->setDistMax(distMax);
84 // ***************************************************************************
85 float UInstance::getShapeDistMax() const
87 CTransformShape *object = getObjectPtr();
88 if(object && object->Shape)
90 return object->Shape->getDistMax();
92 else
93 return -1;
97 // ***************************************************************************
98 void UInstance::selectTextureSet(uint id)
100 CTransformShape *object = getObjectPtr();
101 CMeshBaseInstance *mbi = dynamic_cast<CMeshBaseInstance *>(object);
102 if (mbi)
103 mbi->selectTextureSet(id);
107 // ***************************************************************************
108 void UInstance::enableAsyncTextureMode(bool enable)
110 CTransformShape *object = getObjectPtr();
111 if(object->isMeshBaseInstance())
113 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
114 mbi->enableAsyncTextureMode(enable) ;
117 // ***************************************************************************
118 bool UInstance::getAsyncTextureMode() const
120 CTransformShape *object = getObjectPtr();
121 if(object->isMeshBaseInstance())
123 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
124 return mbi->getAsyncTextureMode() ;
126 else
127 return false;
129 // ***************************************************************************
130 void UInstance::startAsyncTextureLoading()
132 CTransformShape *object = getObjectPtr();
133 if(object->isMeshBaseInstance())
135 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
136 mbi->startAsyncTextureLoading(getPos());
139 // ***************************************************************************
140 bool UInstance::isAsyncTextureReady()
142 CTransformShape *object = getObjectPtr();
143 if(object->isMeshBaseInstance())
145 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
146 return mbi->isAsyncTextureReady();
148 else
149 return true;
151 // ***************************************************************************
152 void UInstance::setAsyncTextureDistance(float dist)
154 CTransformShape *object = getObjectPtr();
155 if(object->isMeshBaseInstance())
157 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
158 mbi->setAsyncTextureDistance(dist);
161 // ***************************************************************************
162 float UInstance::getAsyncTextureDistance() const
164 CTransformShape *object = getObjectPtr();
165 if(object->isMeshBaseInstance())
167 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
168 return mbi->getAsyncTextureDistance();
170 else
171 return 0.f;
173 // ***************************************************************************
174 void UInstance::setAsyncTextureDirty(bool flag)
176 CTransformShape *object = getObjectPtr();
177 if(object->isMeshBaseInstance())
179 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
180 mbi->setAsyncTextureDirty(flag);
183 // ***************************************************************************
184 bool UInstance::isAsyncTextureDirty() const
186 CTransformShape *object = getObjectPtr();
187 if(object->isMeshBaseInstance())
189 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
190 return mbi->isAsyncTextureDirty();
192 else
193 return false;
196 // ***************************************************************************
197 void UInstance::setCoarseMeshDist(float dist)
199 CTransformShape *object = getObjectPtr();
200 CMeshMultiLodInstance *mmli = dynamic_cast<CMeshMultiLodInstance *>(object);
201 if (mmli) mmli->setCoarseMeshDist(dist);
204 // ***************************************************************************
205 float UInstance::getCoarseMeshDist() const
207 CTransformShape *object = getObjectPtr();
208 CMeshMultiLodInstance *mmli = dynamic_cast<CMeshMultiLodInstance *>(object);
209 return mmli ? mmli->getCoarseMeshDist() : -1.f;
212 // ***************************************************************************
213 void UInstance::setSliceTime(float duration)
215 CTransformShape *object = getObjectPtr();
216 CSegRemanence *sr = dynamic_cast<CSegRemanence *>(object);
217 if (!sr) return;
218 sr->setSliceTime(duration);
221 // ***************************************************************************
222 float UInstance::getSliceTime() const
224 CTransformShape *object = getObjectPtr();
225 CSegRemanence *sr = dynamic_cast<CSegRemanence *>(object);
226 if (!sr) return 0.f;
227 return sr->getSliceTime();
230 // ***************************************************************************
231 bool UInstance::supportMaterialRendering(UDriver &drv, bool forceBaseCaps)
233 CTransformShape *object = getObjectPtr();
234 IDriver *driver = static_cast<CDriverUser*>(&drv)->getDriver ();
235 const uint count = object->getNumMaterial ();
236 uint i;
237 for (i=0; i<count; i++)
239 if (!object->getMaterial (i)->isSupportedByDriver(*driver, forceBaseCaps))
240 return false;
242 return true;
245 // ***************************************************************************
247 uint UInstance::getNumMaterials() const
249 CMeshBaseInstance *mi= dynamic_cast<CMeshBaseInstance*>(_Object);
250 if(mi)
251 return (uint)mi->Materials.size();
252 else
253 return 0;
256 // ***************************************************************************
258 UInstanceMaterial UInstance::getMaterial(uint materialId)
260 CMeshBaseInstance *mi= dynamic_cast<CMeshBaseInstance*>(_Object);
261 nlassertex (mi, ("Should be a CMeshBaseInstance object. Call getNumMaterials() first."));
263 // create user mats.
264 return UInstanceMaterial (mi, &mi->Materials[materialId], &mi->AsyncTextures[materialId]);
267 // ***************************************************************************
269 bool UInstance::canStartStop()
271 CTransformShape *object = getObjectPtr();
272 return object->canStartStop();
275 // ***************************************************************************
277 void UInstance::start()
279 CTransformShape *object = getObjectPtr();
280 object->start();
283 // ***************************************************************************
285 void UInstance::stop()
287 CTransformShape *object = getObjectPtr();
288 object->stop();
291 // ***************************************************************************
293 bool UInstance::isStarted() const
295 CTransformShape *object = getObjectPtr();
296 return object->isStarted();
299 // ***************************************************************************
301 float UInstance::getDistMax() const
303 CTransformShape *object = getObjectPtr();
304 return object->getDistMax();
307 // ***************************************************************************
309 void UInstance::setDistMax(float distMax)
311 CTransformShape *object = getObjectPtr();
312 object->setDistMax(distMax);
315 // ***************************************************************************
316 UShape UInstance::getShape() const
319 CTransformShape *object = getObjectPtr();
320 if(!object)
321 return UShape();
323 // get the shape name
324 return UShape(object->Shape);
327 // ***************************************************************************
328 const std::string &UInstance::getShapeName() const
331 static std::string emptyStr;
333 CTransformShape *object = getObjectPtr();
334 if(!object)
335 return emptyStr;
337 // get the shape bank
338 CScene *scene= object->getOwnerScene();
339 CShapeBank *sb= scene->getShapeBank();
340 if(!sb)
341 return emptyStr;
343 // get the shape name
344 const std::string *str= sb->getShapeNameFromShapePtr(object->Shape);
345 if(str)
346 return *str;
347 else
348 return emptyStr;
351 // ***************************************************************************
352 void UInstance::cast(UTransform object)
354 attach(dynamic_cast<CTransformShape*>(object.getObjectPtr()));
357 // ***************************************************************************
358 bool UInstance::getDefaultPos (CVector &pos) const
360 CTransformShape *object = getObjectPtr();
361 if(object && object->isMeshBaseInstance())
363 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
364 CMeshBase *mb= (CMeshBase*)(IShape*)(mbi->Shape);
365 pos= mb->getDefaultPos()->getDefaultValue();
366 return true;
368 return false;
371 // ***************************************************************************
372 bool UInstance::getDefaultRotQuat (CQuat &rotQuat) const
374 CTransformShape *object = getObjectPtr();
375 if(object && object->isMeshBaseInstance())
377 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
378 CMeshBase *mb= (CMeshBase*)(IShape*)(mbi->Shape);
379 rotQuat= mb->getDefaultRotQuat()->getDefaultValue();
380 return true;
382 return false;
385 // ***************************************************************************
386 bool UInstance::getDefaultScale (CVector &scale) const
388 CTransformShape *object = getObjectPtr();
389 if(object && object->isMeshBaseInstance())
391 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
392 CMeshBase *mb= (CMeshBase*)(IShape*)(mbi->Shape);
393 scale= mb->getDefaultScale()->getDefaultValue();
394 return true;
396 return false;
399 // ***************************************************************************
400 void UInstance::setRelativeScale (const CVector &rs)
402 CTransformShape *object = getObjectPtr();
403 if(object && object->isMeshBaseInstance())
405 // get the default scale
406 CMeshBaseInstance *mbi = static_cast<CMeshBaseInstance *>(object);
407 CMeshBase *mb= (CMeshBase*)(IShape*)(mbi->Shape);
408 CVector scale= mb->getDefaultScale()->getDefaultValue();
409 // scale it by the relative one
410 scale.x*= rs.x;
411 scale.y*= rs.y;
412 scale.z*= rs.z;
413 object->setScale(scale);
418 } // NL3D