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/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
;
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
);
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.
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();
97 // ***************************************************************************
98 void UInstance::selectTextureSet(uint id
)
100 CTransformShape
*object
= getObjectPtr();
101 CMeshBaseInstance
*mbi
= dynamic_cast<CMeshBaseInstance
*>(object
);
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() ;
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();
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();
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();
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
);
218 sr
->setSliceTime(duration
);
221 // ***************************************************************************
222 float UInstance::getSliceTime() const
224 CTransformShape
*object
= getObjectPtr();
225 CSegRemanence
*sr
= dynamic_cast<CSegRemanence
*>(object
);
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 ();
237 for (i
=0; i
<count
; i
++)
239 if (!object
->getMaterial (i
)->isSupportedByDriver(*driver
, forceBaseCaps
))
245 // ***************************************************************************
247 uint
UInstance::getNumMaterials() const
249 CMeshBaseInstance
*mi
= dynamic_cast<CMeshBaseInstance
*>(_Object
);
251 return (uint
)mi
->Materials
.size();
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."));
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();
283 // ***************************************************************************
285 void UInstance::stop()
287 CTransformShape
*object
= getObjectPtr();
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();
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();
337 // get the shape bank
338 CScene
*scene
= object
->getOwnerScene();
339 CShapeBank
*sb
= scene
->getShapeBank();
343 // get the shape name
344 const std::string
*str
= sb
->getShapeNameFromShapePtr(object
->Shape
);
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();
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();
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();
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
413 object
->setScale(scale
);