/
OgreItem.cpp
375 lines (338 loc) · 13.7 KB
/
OgreItem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org
Copyright (c) 2000-2014 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR WISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "OgreStableHeaders.h"
#include "OgreItem.h"
#include "OgreMeshManager.h"
#include "OgreMesh2.h"
#include "OgreSubMesh2.h"
#include "OgreSubItem.h"
#include "OgreHlmsManager.h"
#include "OgreException.h"
#include "OgreSceneManager.h"
#include "OgreLogManager.h"
#include "Animation/OgreSkeletonInstance.h"
#include "OgreRoot.h"
#include "OgreSceneNode.h"
#include "OgreMeshManager2.h"
namespace Ogre {
extern const FastArray<Real> c_DefaultLodMesh;
//-----------------------------------------------------------------------
Item::Item( IdType id, ObjectMemoryManager *objectMemoryManager, SceneManager *manager )
: MovableObject( id, objectMemoryManager, manager, 10u ),
mInitialised( false )
{
mObjectData.mQueryFlags[mObjectData.mIndex] = SceneManager::QUERY_ENTITY_DEFAULT_MASK;
}
//-----------------------------------------------------------------------
Item::Item( IdType id, ObjectMemoryManager *objectMemoryManager, SceneManager *manager,
const MeshPtr& mesh ) :
MovableObject( id, objectMemoryManager, manager, 10u ),
mMesh( mesh ),
mInitialised( false )
{
_initialise();
mObjectData.mQueryFlags[mObjectData.mIndex] = SceneManager::QUERY_ENTITY_DEFAULT_MASK;
}
//-----------------------------------------------------------------------
void Item::_initialise(bool forceReinitialise)
{
if( forceReinitialise )
_deinitialise();
if (mInitialised)
return;
if (mMesh->isBackgroundLoaded() && !mMesh->isLoaded())
{
// register for a callback when mesh is finished loading
// do this before asking for load to happen to avoid race
mMesh->addListener(this);
}
// On-demand load
mMesh->load();
// If loading failed, or deferred loading isn't done yet, defer
// Will get a callback in the case of deferred loading
// Skeletons are cascade-loaded so no issues there
if (!mMesh->isLoaded())
return;
// Is mesh skeletally animated?
if( mMesh->hasSkeleton() && !mMesh->getSkeleton().isNull() && mManager )
{
const SkeletonDef *skeletonDef = mMesh->getSkeleton().get();
mSkeletonInstance = mManager->createSkeletonInstance( skeletonDef );
}
mLodMesh = mMesh->_getLodValueArray();
// Build main subItem list
buildSubItems();
{
//Without filling the renderables list, the RenderQueue won't
//catch our sub entities and thus we won't be rendered
mRenderables.reserve( mSubItems.size() );
SubItemVec::iterator itor = mSubItems.begin();
SubItemVec::iterator end = mSubItems.end();
while( itor != end )
{
mRenderables.push_back( &(*itor) );
++itor;
}
}
Aabb aabb( mMesh->getAabb() );
mObjectData.mLocalAabb->setFromAabb( aabb, mObjectData.mIndex );
mObjectData.mWorldAabb->setFromAabb( aabb, mObjectData.mIndex );
mObjectData.mLocalRadius[mObjectData.mIndex] = aabb.getRadius();
mObjectData.mWorldRadius[mObjectData.mIndex] = aabb.getRadius();
mInitialised = true;
}
//-----------------------------------------------------------------------
void Item::_deinitialise(void)
{
if (!mInitialised)
return;
// Delete submeshes
mSubItems.clear();
mRenderables.clear();
// If mesh is skeletally animated: destroy instance
assert( mManager || !mSkeletonInstance );
if( mSkeletonInstance )
{
mSkeletonInstance->_decrementRefCount();
if( mSkeletonInstance->_getRefCount() == 0u )
mManager->destroySkeletonInstance( mSkeletonInstance );
mSkeletonInstance = 0;
}
mInitialised = false;
}
//-----------------------------------------------------------------------
Item::~Item()
{
_deinitialise();
// Unregister our listener
mMesh->removeListener(this);
}
//-----------------------------------------------------------------------
const MeshPtr& Item::getMesh(void) const
{
return mMesh;
}
//-----------------------------------------------------------------------
SubItem* Item::getSubItem(size_t index)
{
if (index >= mSubItems.size())
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"Index out of bounds.",
"Item::getSubItem");
return &mSubItems[index];
}
//-----------------------------------------------------------------------
const SubItem* Item::getSubItem(size_t index) const
{
if (index >= mSubItems.size())
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"Index out of bounds.",
"Item::getSubItem");
return &mSubItems[index];
}
//-----------------------------------------------------------------------
size_t Item::getNumSubItems(void) const
{
return mSubItems.size();
}
//-----------------------------------------------------------------------
void Item::setDatablock( HlmsDatablock *datablock )
{
SubItemVec::iterator itor = mSubItems.begin();
SubItemVec::iterator end = mSubItems.end();
while( itor != end )
{
itor->setDatablock( datablock );
++itor;
}
}
//-----------------------------------------------------------------------
void Item::setDatablock( IdString datablockName )
{
HlmsManager *hlmsManager = Root::getSingleton().getHlmsManager();
HlmsDatablock *datablock = hlmsManager->getDatablock( datablockName );
setDatablock( datablock );
}
#if 0
//-----------------------------------------------------------------------
Item* Item::clone( const String& newName ) const
{
if (!mManager)
{
OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
"Cannot clone an Item that wasn't created through a "
"SceneManager", "Item::clone");
}
Item* newEnt = mManager->createItem( newName, getMesh()->getName() );
if( mInitialised )
{
// Copy material settings
SubItemVec::const_iterator i;
unsigned int n = 0;
for (i = mSubItems.begin(); i != mSubItems.end(); ++i, ++n)
newEnt->getSubItem(n)->setDatablock( i->getDatablock() );
}
return newEnt;
}
#endif
//-----------------------------------------------------------------------
void Item::setDatablockOrMaterialName( const String& name, const String& groupName /* = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME */)
{
// Set for all subentities
SubItemVec::iterator i;
for (i = mSubItems.begin(); i != mSubItems.end(); ++i)
{
i->setDatablockOrMaterialName(name, groupName);
}
}
//-----------------------------------------------------------------------
void Item::setMaterialName( const String& name, const String& groupName /* = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME */)
{
// Set for all subentities
SubItemVec::iterator i;
for (i = mSubItems.begin(); i != mSubItems.end(); ++i)
{
i->setMaterialName(name, groupName);
}
}
//-----------------------------------------------------------------------
void Item::setMaterial( const MaterialPtr& material )
{
// Set for all subentities
SubItemVec::iterator i;
for (i = mSubItems.begin(); i != mSubItems.end(); ++i)
{
i->setMaterial(material);
}
}
//-----------------------------------------------------------------------
const String& Item::getMovableType(void) const
{
return ItemFactory::FACTORY_TYPE_NAME;
}
//-----------------------------------------------------------------------
void Item::buildSubItems(void)
{
// Create SubEntities
size_t numSubMeshes = mMesh->getNumSubMeshes();
mSubItems.reserve( numSubMeshes );
for( size_t i=0; i<numSubMeshes; ++i )
{
SubMesh *subMesh = mMesh->getSubMesh(i);
mSubItems.push_back( SubItem( this, subMesh ) );
//Try first Hlms materials, then the low level ones.
mSubItems.back().setDatablockOrMaterialName( subMesh->mMaterialName, mMesh->getGroup() );
}
}
//-----------------------------------------------------------------------
void Item::useSkeletonInstanceFrom(Item* master)
{
if( mMesh->getSkeletonName() != master->mMesh->getSkeletonName() )
{
OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
"Cannot share skeleton instance if meshes use different skeletons",
"Item::useSkeletonInstanceFrom" );
}
if( mSkeletonInstance )
{
mSkeletonInstance->_decrementRefCount();
if( mSkeletonInstance->_getRefCount() == 0u )
mManager->destroySkeletonInstance( mSkeletonInstance );
}
mSkeletonInstance = master->mSkeletonInstance;
mSkeletonInstance->_incrementRefCount();
}
//-----------------------------------------------------------------------
void Item::stopUsingSkeletonInstanceFromMaster()
{
if( mSkeletonInstance )
{
assert( mSkeletonInstance->_getRefCount() > 1u &&
"This skeleton is Item is not sharing its skeleton!" );
mSkeletonInstance->_decrementRefCount();
if( mSkeletonInstance->_getRefCount() == 0u )
mManager->destroySkeletonInstance( mSkeletonInstance );
const SkeletonDef *skeletonDef = mMesh->getSkeleton().get();
mSkeletonInstance = mManager->createSkeletonInstance( skeletonDef );
}
}
//-----------------------------------------------------------------------
bool Item::sharesSkeletonInstance() const
{
return mSkeletonInstance && mSkeletonInstance->_getRefCount() > 1u;
}
//-----------------------------------------------------------------------
void Item::_notifyParentNodeMemoryChanged(void)
{
if( mSkeletonInstance /*&& !mSharedTransformEntity*/ )
{
mSkeletonInstance->setParentNode( mSkeletonInstance->getParentNode() );
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
String ItemFactory::FACTORY_TYPE_NAME = "Item";
//-----------------------------------------------------------------------
const String& ItemFactory::getType(void) const
{
return FACTORY_TYPE_NAME;
}
//-----------------------------------------------------------------------
MovableObject* ItemFactory::createInstanceImpl( IdType id,
ObjectMemoryManager *objectMemoryManager,
SceneManager *manager,
const NameValuePairList* params )
{
// must have mesh parameter
MeshPtr pMesh;
if (params != 0)
{
String groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME;
NameValuePairList::const_iterator ni;
ni = params->find("resourceGroup");
if (ni != params->end())
{
groupName = ni->second;
}
ni = params->find("mesh");
if (ni != params->end())
{
// Get mesh (load if required)
pMesh = MeshManager::getSingleton().load( ni->second, groupName );
}
}
if (pMesh.isNull())
{
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
"'mesh' parameter required when constructing an Item.",
"ItemFactory::createInstance");
}
return OGRE_NEW Item( id, objectMemoryManager, manager, pMesh );
}
//-----------------------------------------------------------------------
void ItemFactory::destroyInstance( MovableObject* obj)
{
OGRE_DELETE obj;
}
}