/src/ogre/OgreMain/src/OgreInstanceBatch.cpp
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | #include "OgreInstanceManager.h" |
30 | | #include "OgreInstanceBatch.h" |
31 | | #include "OgreInstancedEntity.h" |
32 | | #include "OgreRenderQueue.h" |
33 | | #include "OgreLodListener.h" |
34 | | |
35 | | namespace Ogre |
36 | | { |
37 | | const String MOT_INSTANCE_BATCH = "InstanceBatch"; |
38 | | |
39 | | InstanceBatch::InstanceBatch( InstanceManager *creator, MeshPtr &meshReference, |
40 | | const MaterialPtr &material, size_t instancesPerBatch, |
41 | | const Mesh::IndexMap *indexToBoneMap, const String &batchName ) : |
42 | 0 | Renderable(), |
43 | 0 | MovableObject(), |
44 | 0 | mInstancesPerBatch( instancesPerBatch ), |
45 | 0 | mCreator( creator ), |
46 | 0 | mMaterial( material ), |
47 | 0 | mMeshReference( meshReference ), |
48 | 0 | mIndexToBoneMap( indexToBoneMap ), |
49 | 0 | mBoundingRadius( 0 ), |
50 | 0 | mBoundsDirty( false ), |
51 | 0 | mBoundsUpdated( false ), |
52 | 0 | mCurrentCamera( 0 ), |
53 | 0 | mDirtyAnimation(true), |
54 | 0 | mTechnSupportsSkeletal( true ), |
55 | 0 | mCameraDistLastUpdateFrameNumber( std::numeric_limits<unsigned long>::max() ), |
56 | 0 | mCachedCamera( 0 ), |
57 | 0 | mTransformSharingDirty(true), |
58 | 0 | mRemoveOwnVertexData(false), |
59 | 0 | mRemoveOwnIndexData(false) |
60 | 0 | { |
61 | 0 | assert( mInstancesPerBatch ); |
62 | | |
63 | | //Force batch visibility to be always visible. The instanced entities |
64 | | //have individual visibility flags. If none matches the scene's current, |
65 | | //then this batch won't rendered. |
66 | 0 | mVisibilityFlags = std::numeric_limits<Ogre::uint32>::max(); |
67 | |
|
68 | 0 | if( indexToBoneMap ) |
69 | 0 | { |
70 | 0 | assert( !(meshReference->hasSkeleton() && indexToBoneMap->empty()) ); |
71 | 0 | } |
72 | |
|
73 | 0 | mFullBoundingBox.setExtents( -Vector3::ZERO, Vector3::ZERO ); |
74 | |
|
75 | 0 | mName = batchName; |
76 | 0 | if (mCreator != NULL) |
77 | 0 | { |
78 | 0 | mCustomParams.resize( mCreator->getNumCustomParams() * mInstancesPerBatch, Vector4f(0) ); |
79 | 0 | } |
80 | | |
81 | 0 | } |
82 | | |
83 | | InstanceBatch::~InstanceBatch() |
84 | 0 | { |
85 | 0 | deleteAllInstancedEntities(); |
86 | | |
87 | | //Remove the parent scene node automatically |
88 | 0 | SceneNode *sceneNode = getParentSceneNode(); |
89 | 0 | if( sceneNode ) |
90 | 0 | { |
91 | 0 | sceneNode->detachAllObjects(); |
92 | 0 | sceneNode->getParentSceneNode()->removeAndDestroyChild( sceneNode ); |
93 | 0 | } |
94 | |
|
95 | 0 | if( mRemoveOwnVertexData ) |
96 | 0 | OGRE_DELETE mRenderOperation.vertexData; |
97 | 0 | if( mRemoveOwnIndexData ) |
98 | 0 | OGRE_DELETE mRenderOperation.indexData; |
99 | |
|
100 | 0 | } |
101 | | |
102 | | void InstanceBatch::_setInstancesPerBatch( size_t instancesPerBatch ) |
103 | 0 | { |
104 | 0 | OgreAssert(mInstancedEntities.empty(), |
105 | 0 | "Instances per batch can only be changed before building the batch"); |
106 | 0 | mInstancesPerBatch = instancesPerBatch; |
107 | 0 | } |
108 | | //----------------------------------------------------------------------- |
109 | | bool InstanceBatch::checkSubMeshCompatibility( const SubMesh* baseSubMesh ) |
110 | 0 | { |
111 | 0 | OgreAssert(baseSubMesh->operationType == RenderOperation::OT_TRIANGLE_LIST, |
112 | 0 | "Only meshes with OT_TRIANGLE_LIST are supported"); |
113 | | |
114 | 0 | if( !mCustomParams.empty() && mCreator->getInstancingTechnique() != InstanceManager::HWInstancingBasic ) |
115 | 0 | { |
116 | | //Implementing this for ShaderBased is impossible. All other variants can be. |
117 | 0 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Custom parameters not supported for this " |
118 | 0 | "technique. Do you dare implementing it?" |
119 | 0 | "See InstanceManager::setNumCustomParams " |
120 | 0 | "documentation.", |
121 | 0 | "InstanceBatch::checkSubMeshCompatibility"); |
122 | 0 | } |
123 | | |
124 | 0 | return true; |
125 | 0 | } |
126 | | //----------------------------------------------------------------------- |
127 | | void InstanceBatch::_updateBounds(void) |
128 | 0 | { |
129 | 0 | mFullBoundingBox.setNull(); |
130 | 0 | Real maxScale = 0; |
131 | |
|
132 | 0 | for(auto *e : mInstancedEntities) |
133 | 0 | { |
134 | | //Only increase the bounding box for those objects we know are in the scene |
135 | 0 | if(e->isInScene()) |
136 | 0 | { |
137 | 0 | maxScale = std::max(maxScale, e->getMaxScaleCoef()); |
138 | 0 | mFullBoundingBox.merge( e->_getDerivedPosition() ); |
139 | 0 | } |
140 | 0 | } |
141 | |
|
142 | 0 | Real addToBound = maxScale * _getMeshReference()->getBoundingSphereRadius(); |
143 | 0 | mFullBoundingBox.setMaximum(mFullBoundingBox.getMaximum() + addToBound); |
144 | 0 | mFullBoundingBox.setMinimum(mFullBoundingBox.getMinimum() - addToBound); |
145 | | |
146 | |
|
147 | 0 | mBoundingRadius = Math::boundingRadiusFromAABBCentered( mFullBoundingBox ); |
148 | 0 | if (mParentNode) { |
149 | 0 | mParentNode->needUpdate(); |
150 | 0 | } |
151 | 0 | mBoundsUpdated = true; |
152 | 0 | mBoundsDirty = false; |
153 | 0 | } |
154 | | |
155 | | //----------------------------------------------------------------------- |
156 | | void InstanceBatch::updateVisibility(void) |
157 | 0 | { |
158 | 0 | mVisible = false; |
159 | |
|
160 | 0 | InstancedEntityVec::const_iterator itor = mInstancedEntities.begin(); |
161 | 0 | InstancedEntityVec::const_iterator end = mInstancedEntities.end(); |
162 | |
|
163 | 0 | while( itor != end && !mVisible ) |
164 | 0 | { |
165 | | //Trick to force Ogre not to render us if none of our instances is visible |
166 | | //Because we do Camera::isVisible(), it is better if the SceneNode from the |
167 | | //InstancedEntity is not part of the scene graph (i.e. ultimate parent is root node) |
168 | | //to avoid unnecessary wasteful calculations |
169 | 0 | mVisible |= (*itor)->findVisible( mCurrentCamera ); |
170 | 0 | ++itor; |
171 | 0 | } |
172 | 0 | } |
173 | | //----------------------------------------------------------------------- |
174 | | void InstanceBatch::createAllInstancedEntities() |
175 | 0 | { |
176 | 0 | mInstancedEntities.reserve( mInstancesPerBatch ); |
177 | 0 | mUnusedEntities.reserve( mInstancesPerBatch ); |
178 | |
|
179 | 0 | for( size_t i=0; i<mInstancesPerBatch; ++i ) |
180 | 0 | { |
181 | 0 | InstancedEntity *instance = generateInstancedEntity(i); |
182 | 0 | mInstancedEntities.push_back( instance ); |
183 | 0 | mUnusedEntities.push_back( instance ); |
184 | 0 | } |
185 | 0 | } |
186 | | //----------------------------------------------------------------------- |
187 | | InstancedEntity* InstanceBatch::generateInstancedEntity(size_t num) |
188 | 0 | { |
189 | 0 | return OGRE_NEW InstancedEntity(this, static_cast<uint32>(num)); |
190 | 0 | } |
191 | | //----------------------------------------------------------------------- |
192 | | void InstanceBatch::deleteAllInstancedEntities() |
193 | 0 | { |
194 | 0 | for (auto *e : mInstancedEntities) |
195 | 0 | { |
196 | 0 | if(e->getParentSceneNode() ) |
197 | 0 | e->getParentSceneNode()->detachObject(e); |
198 | 0 | OGRE_DELETE e; |
199 | 0 | } |
200 | 0 | } |
201 | | //----------------------------------------------------------------------- |
202 | | void InstanceBatch::deleteUnusedInstancedEntities() |
203 | 0 | { |
204 | 0 | for (auto *e : mUnusedEntities) |
205 | 0 | OGRE_DELETE e; |
206 | |
|
207 | 0 | mUnusedEntities.clear(); |
208 | 0 | } |
209 | | //----------------------------------------------------------------------- |
210 | | void InstanceBatch::makeMatrixCameraRelative3x4( Matrix3x4f *mat3x4, size_t count ) |
211 | 0 | { |
212 | 0 | const Vector3 &cameraRelativePosition = mCurrentCamera->getDerivedPosition(); |
213 | |
|
214 | 0 | for( size_t i=0; i<count; i++ ) |
215 | 0 | { |
216 | 0 | mat3x4[i].setTrans(mat3x4[i].getTrans() - Vector<3, float>(cameraRelativePosition)); |
217 | 0 | } |
218 | 0 | } |
219 | | //----------------------------------------------------------------------- |
220 | | RenderOperation InstanceBatch::build( const SubMesh* baseSubMesh ) |
221 | 0 | { |
222 | 0 | if( checkSubMeshCompatibility( baseSubMesh ) ) |
223 | 0 | { |
224 | | //Only triangle list at the moment |
225 | 0 | mRenderOperation.operationType = RenderOperation::OT_TRIANGLE_LIST; |
226 | 0 | mRenderOperation.srcRenderable = this; |
227 | 0 | mRenderOperation.useIndexes = true; |
228 | 0 | setupVertices( baseSubMesh ); |
229 | 0 | setupIndices( baseSubMesh ); |
230 | |
|
231 | 0 | createAllInstancedEntities(); |
232 | 0 | } |
233 | |
|
234 | 0 | return mRenderOperation; |
235 | 0 | } |
236 | | //----------------------------------------------------------------------- |
237 | | void InstanceBatch::buildFrom( const SubMesh *baseSubMesh, const RenderOperation &renderOperation ) |
238 | 0 | { |
239 | 0 | mRenderOperation = renderOperation; |
240 | 0 | createAllInstancedEntities(); |
241 | 0 | } |
242 | | //----------------------------------------------------------------------- |
243 | | InstancedEntity* InstanceBatch::createInstancedEntity() |
244 | 0 | { |
245 | 0 | InstancedEntity *retVal = 0; |
246 | |
|
247 | 0 | if( !mUnusedEntities.empty() ) |
248 | 0 | { |
249 | 0 | retVal = mUnusedEntities.back(); |
250 | 0 | mUnusedEntities.pop_back(); |
251 | |
|
252 | 0 | retVal->setInUse(true); |
253 | 0 | } |
254 | |
|
255 | 0 | return retVal; |
256 | 0 | } |
257 | | //----------------------------------------------------------------------- |
258 | | void InstanceBatch::removeInstancedEntity( InstancedEntity *instancedEntity ) |
259 | 0 | { |
260 | 0 | OgreAssert(instancedEntity->mBatchOwner == this, |
261 | 0 | "Trying to remove an InstancedEntity from scene created with a different InstanceBatch"); |
262 | 0 | OgreAssert(instancedEntity->isInUse(), "Trying to remove an InstancedEntity that is already removed"); |
263 | | |
264 | 0 | if( instancedEntity->getParentSceneNode() ) |
265 | 0 | instancedEntity->getParentSceneNode()->detachObject( instancedEntity ); |
266 | |
|
267 | 0 | instancedEntity->setInUse(false); |
268 | 0 | instancedEntity->stopSharingTransform(); |
269 | | |
270 | | //Put it back into the queue |
271 | 0 | mUnusedEntities.push_back( instancedEntity ); |
272 | 0 | } |
273 | | //----------------------------------------------------------------------- |
274 | | void InstanceBatch::getInstancedEntitiesInUse( InstancedEntityVec &outEntities, |
275 | | CustomParamsVec &outParams ) |
276 | 0 | { |
277 | 0 | for (auto *e : mInstancedEntities) { |
278 | 0 | if (e->isInUse()) { |
279 | 0 | outEntities.push_back(e); |
280 | 0 | for( unsigned char i=0; i<mCreator->getNumCustomParams(); ++i ) |
281 | 0 | outParams.push_back(_getCustomParam(e, i)); |
282 | 0 | } |
283 | 0 | } |
284 | 0 | } |
285 | | //----------------------------------------------------------------------- |
286 | | void InstanceBatch::defragmentBatchNoCull( InstancedEntityVec &usedEntities, |
287 | | CustomParamsVec &usedParams ) |
288 | 0 | { |
289 | 0 | const size_t maxInstancesToCopy = std::min( mInstancesPerBatch, usedEntities.size() ); |
290 | 0 | InstancedEntityVec::iterator first = usedEntities.end() - maxInstancesToCopy; |
291 | 0 | CustomParamsVec::iterator firstParams = usedParams.end() - maxInstancesToCopy * |
292 | 0 | mCreator->getNumCustomParams(); |
293 | | |
294 | | //Copy from the back to front, into m_instancedEntities |
295 | 0 | mInstancedEntities.insert( mInstancedEntities.begin(), first, usedEntities.end() ); |
296 | | //Remove them from the array |
297 | 0 | usedEntities.resize( usedEntities.size() - maxInstancesToCopy ); |
298 | |
|
299 | 0 | mCustomParams.insert( mCustomParams.begin(), firstParams, usedParams.end() ); |
300 | 0 | } |
301 | | //----------------------------------------------------------------------- |
302 | | void InstanceBatch::defragmentBatchDoCull( InstancedEntityVec &usedEntities, |
303 | | CustomParamsVec &usedParams ) |
304 | 0 | { |
305 | | //Get the the entity closest to the minimum bbox edge and put into "first" |
306 | 0 | InstancedEntityVec::const_iterator itor = usedEntities.begin(); |
307 | 0 | InstancedEntityVec::const_iterator end = usedEntities.end(); |
308 | |
|
309 | 0 | Vector3 vMinPos = Vector3::ZERO, firstPos = Vector3::ZERO; |
310 | 0 | InstancedEntity *first = 0; |
311 | |
|
312 | 0 | if( !usedEntities.empty() ) |
313 | 0 | { |
314 | 0 | first = *usedEntities.begin(); |
315 | 0 | firstPos = first->_getDerivedPosition(); |
316 | 0 | vMinPos = first->_getDerivedPosition(); |
317 | 0 | } |
318 | |
|
319 | 0 | while( itor != end ) |
320 | 0 | { |
321 | 0 | const Vector3 &vPos = (*itor)->_getDerivedPosition(); |
322 | |
|
323 | 0 | vMinPos.x = std::min( vMinPos.x, vPos.x ); |
324 | 0 | vMinPos.y = std::min( vMinPos.y, vPos.y ); |
325 | 0 | vMinPos.z = std::min( vMinPos.z, vPos.z ); |
326 | |
|
327 | 0 | if( vMinPos.squaredDistance( vPos ) < vMinPos.squaredDistance( firstPos ) ) |
328 | 0 | { |
329 | 0 | firstPos = vPos; |
330 | 0 | } |
331 | |
|
332 | 0 | ++itor; |
333 | 0 | } |
334 | | |
335 | | //Now collect entities closest to 'first' |
336 | 0 | while( !usedEntities.empty() && mInstancedEntities.size() < mInstancesPerBatch ) |
337 | 0 | { |
338 | 0 | InstancedEntityVec::iterator closest = usedEntities.begin(); |
339 | 0 | InstancedEntityVec::iterator it = usedEntities.begin(); |
340 | 0 | InstancedEntityVec::iterator e = usedEntities.end(); |
341 | |
|
342 | 0 | Vector3 closestPos; |
343 | 0 | closestPos = (*closest)->_getDerivedPosition(); |
344 | |
|
345 | 0 | while( it != e ) |
346 | 0 | { |
347 | 0 | const Vector3 &vPos = (*it)->_getDerivedPosition(); |
348 | |
|
349 | 0 | if( firstPos.squaredDistance( vPos ) < firstPos.squaredDistance( closestPos ) ) |
350 | 0 | { |
351 | 0 | closest = it; |
352 | 0 | closestPos = vPos; |
353 | 0 | } |
354 | |
|
355 | 0 | ++it; |
356 | 0 | } |
357 | |
|
358 | 0 | mInstancedEntities.push_back( *closest ); |
359 | | //Now the custom params |
360 | 0 | const size_t idx = closest - usedEntities.begin(); |
361 | 0 | for( unsigned char i=0; i<mCreator->getNumCustomParams(); ++i ) |
362 | 0 | { |
363 | 0 | mCustomParams.push_back( usedParams[idx + i] ); |
364 | 0 | } |
365 | | |
366 | | //Remove 'closest' from usedEntities & usedParams using swap and pop_back trick |
367 | 0 | *closest = *(usedEntities.end() - 1); |
368 | 0 | usedEntities.pop_back(); |
369 | |
|
370 | 0 | for( unsigned char i=1; i<=mCreator->getNumCustomParams(); ++i ) |
371 | 0 | { |
372 | 0 | usedParams[idx + mCreator->getNumCustomParams() - i] = *(usedParams.end() - 1); |
373 | 0 | usedParams.pop_back(); |
374 | 0 | } |
375 | 0 | } |
376 | 0 | } |
377 | | //----------------------------------------------------------------------- |
378 | | void InstanceBatch::_defragmentBatch( bool optimizeCulling, InstancedEntityVec &usedEntities, |
379 | | CustomParamsVec &usedParams ) |
380 | 0 | { |
381 | | //Remove and clear what we don't need |
382 | 0 | mInstancedEntities.clear(); |
383 | 0 | mCustomParams.clear(); |
384 | 0 | deleteUnusedInstancedEntities(); |
385 | |
|
386 | 0 | if( !optimizeCulling ) |
387 | 0 | defragmentBatchNoCull( usedEntities, usedParams ); |
388 | 0 | else |
389 | 0 | defragmentBatchDoCull( usedEntities, usedParams ); |
390 | | |
391 | | //Reassign instance IDs and tell we're the new parent |
392 | 0 | uint32 instanceId = 0; |
393 | 0 | for (auto *e : mInstancedEntities) { |
394 | 0 | e->mInstanceId = instanceId++; |
395 | 0 | e->mBatchOwner = this; |
396 | 0 | } |
397 | | |
398 | | //Recreate unused entities, if there's left space in our container |
399 | 0 | assert( (signed)(mInstancesPerBatch) - (signed)(mInstancedEntities.size()) >= 0 ); |
400 | 0 | mInstancedEntities.reserve( mInstancesPerBatch ); |
401 | 0 | mUnusedEntities.reserve( mInstancesPerBatch ); |
402 | 0 | mCustomParams.reserve( mCreator->getNumCustomParams() * mInstancesPerBatch ); |
403 | 0 | for( size_t i=mInstancedEntities.size(); i<mInstancesPerBatch; ++i ) |
404 | 0 | { |
405 | 0 | InstancedEntity *instance = generateInstancedEntity(i); |
406 | 0 | mInstancedEntities.push_back( instance ); |
407 | 0 | mUnusedEntities.push_back( instance ); |
408 | 0 | mCustomParams.push_back( Vector4f(0) ); |
409 | 0 | } |
410 | | |
411 | | //We've potentially changed our bounds |
412 | 0 | if( !isBatchUnused() ) |
413 | 0 | _boundsDirty(); |
414 | 0 | } |
415 | | //----------------------------------------------------------------------- |
416 | | void InstanceBatch::_defragmentBatchDiscard(void) |
417 | 0 | { |
418 | | //Remove and clear what we don't need |
419 | 0 | mInstancedEntities.clear(); |
420 | 0 | deleteUnusedInstancedEntities(); |
421 | 0 | } |
422 | | //----------------------------------------------------------------------- |
423 | | void InstanceBatch::_boundsDirty(void) |
424 | 0 | { |
425 | 0 | if( mCreator && !mBoundsDirty ) |
426 | 0 | mCreator->_addDirtyBatch( this ); |
427 | 0 | mBoundsDirty = true; |
428 | 0 | } |
429 | | //----------------------------------------------------------------------- |
430 | | const String& InstanceBatch::getMovableType(void) const |
431 | 0 | { |
432 | 0 | return MOT_INSTANCE_BATCH; |
433 | 0 | } |
434 | | //----------------------------------------------------------------------- |
435 | | void InstanceBatch::_notifyCurrentCamera( Camera* cam ) |
436 | 0 | { |
437 | 0 | mCurrentCamera = cam; |
438 | | |
439 | | //See DistanceLodStrategy::getValueImpl() |
440 | | //We use our own because our SceneNode is just filled with zeroes, and updating it |
441 | | //with real values is expensive, plus we would need to make sure it doesn't get to |
442 | | //the shader |
443 | 0 | Real depth = Math::Sqrt(getSquaredViewDepth(cam)) - getBoundingRadius(); |
444 | 0 | depth = std::max( depth, Real(0) ); |
445 | |
|
446 | 0 | Real lodValue = depth * cam->_getLodBiasInverse(); |
447 | | |
448 | | //Now calculate Material LOD |
449 | | /*const LodStrategy *materialStrategy = m_material->getLodStrategy(); |
450 | | |
451 | | //Calculate LOD value for given strategy |
452 | | Real lodValue = materialStrategy->getValue( this, cam );*/ |
453 | | |
454 | | //Get the index at this depth |
455 | 0 | unsigned short idx = mMaterial->getLodIndex( lodValue ); |
456 | | |
457 | | //TODO: Replace subEntity for MovableObject |
458 | | // Construct event object |
459 | | /*EntityMaterialLodChangedEvent subEntEvt; |
460 | | subEntEvt.subEntity = this; |
461 | | subEntEvt.camera = cam; |
462 | | subEntEvt.lodValue = lodValue; |
463 | | subEntEvt.previousLodIndex = m_materialLodIndex; |
464 | | subEntEvt.newLodIndex = idx; |
465 | | |
466 | | //Notify LOD event listeners |
467 | | cam->getSceneManager()->_notifyEntityMaterialLodChanged(subEntEvt);*/ |
468 | | |
469 | | // Change LOD index |
470 | 0 | mMaterialLodIndex = idx; |
471 | |
|
472 | 0 | mBeyondFarDistance = false; |
473 | |
|
474 | 0 | if (cam->getUseRenderingDistance() && mUpperDistance > 0) |
475 | 0 | { |
476 | 0 | if (depth > mUpperDistance) |
477 | 0 | mBeyondFarDistance = true; |
478 | 0 | } |
479 | |
|
480 | 0 | if (!mBeyondFarDistance && cam->getUseMinPixelSize() && mMinPixelSize > 0) |
481 | 0 | { |
482 | 0 | Real pixelRatio = cam->getPixelDisplayRatio(); |
483 | |
|
484 | 0 | Ogre::Vector3 objBound = |
485 | 0 | getBoundingBox().getSize() * getParentNode()->_getDerivedScale(); |
486 | 0 | objBound.x = Math::Sqr(objBound.x); |
487 | 0 | objBound.y = Math::Sqr(objBound.y); |
488 | 0 | objBound.z = Math::Sqr(objBound.z); |
489 | 0 | float sqrObjMedianSize = std::max( |
490 | 0 | std::max(std::min(objBound.x, objBound.y), std::min(objBound.x, objBound.z)), |
491 | 0 | std::min(objBound.y, objBound.z)); |
492 | | |
493 | | // If we have a perspective camera calculations are done relative to distance |
494 | 0 | Real sqrDistance = 1; |
495 | |
|
496 | 0 | if (cam->getProjectionType() == PT_PERSPECTIVE) |
497 | 0 | sqrDistance = getSquaredViewDepth(cam->getLodCamera()); // it's ok |
498 | |
|
499 | 0 | mBeyondFarDistance = |
500 | 0 | sqrObjMedianSize < sqrDistance * Math::Sqr(pixelRatio * mMinPixelSize); |
501 | 0 | } |
502 | |
|
503 | 0 | if (mParentNode) |
504 | 0 | { |
505 | 0 | MovableObjectLodChangedEvent evt; |
506 | 0 | evt.movableObject = this; |
507 | 0 | evt.camera = cam; |
508 | |
|
509 | 0 | cam->getSceneManager()->_notifyMovableObjectLodChanged(evt); |
510 | 0 | } |
511 | |
|
512 | 0 | mRenderingDisabled = mListener && !mListener->objectRendering(this, cam); |
513 | | |
514 | | // MovableObject::_notifyCurrentCamera( cam ); // it does not suit |
515 | 0 | } |
516 | | //----------------------------------------------------------------------- |
517 | | const AxisAlignedBox& InstanceBatch::getBoundingBox(void) const |
518 | 0 | { |
519 | 0 | return mFullBoundingBox; |
520 | 0 | } |
521 | | //----------------------------------------------------------------------- |
522 | | Real InstanceBatch::getBoundingRadius(void) const |
523 | 0 | { |
524 | 0 | return mBoundingRadius; |
525 | 0 | } |
526 | | //----------------------------------------------------------------------- |
527 | | Real InstanceBatch::getSquaredViewDepth( const Camera* cam ) const |
528 | 0 | { |
529 | 0 | unsigned long currentFrameNumber = Root::getSingleton().getNextFrameNumber(); |
530 | |
|
531 | 0 | if (mCameraDistLastUpdateFrameNumber != currentFrameNumber || mCachedCamera != cam) |
532 | 0 | { |
533 | 0 | mCachedCameraDist = |
534 | 0 | getBoundingBox().getCenter().squaredDistance(cam->getDerivedPosition()); |
535 | |
|
536 | 0 | mCachedCamera = cam; |
537 | 0 | mCameraDistLastUpdateFrameNumber = currentFrameNumber; |
538 | 0 | } |
539 | |
|
540 | 0 | return mCachedCameraDist; |
541 | 0 | } |
542 | | //----------------------------------------------------------------------- |
543 | | const LightList& InstanceBatch::getLights( void ) const |
544 | 0 | { |
545 | 0 | return queryLights(); |
546 | 0 | } |
547 | | //----------------------------------------------------------------------- |
548 | | void InstanceBatch::_updateRenderQueue( RenderQueue* queue ) |
549 | 0 | { |
550 | | /*if( m_boundsDirty ) |
551 | | _updateBounds();*/ |
552 | |
|
553 | 0 | mDirtyAnimation = false; |
554 | | |
555 | | //Is at least one object in the scene? |
556 | 0 | updateVisibility(); |
557 | |
|
558 | 0 | if( mVisible ) |
559 | 0 | { |
560 | 0 | if( mMeshReference->hasSkeleton() ) { |
561 | 0 | for (auto *e : mInstancedEntities) { |
562 | 0 | mDirtyAnimation |= e->_updateAnimation(); |
563 | 0 | } |
564 | 0 | } |
565 | |
|
566 | 0 | queue->addRenderable( this, mRenderQueueID, mRenderQueuePriority ); |
567 | 0 | } |
568 | | |
569 | | //Reset visibility once we skipped addRenderable (which saves GPU time), because OGRE for some |
570 | | //reason stops updating our render queue afterwards, preventing us to recalculate visibility |
571 | 0 | mVisible = true; |
572 | 0 | } |
573 | | //----------------------------------------------------------------------- |
574 | | void InstanceBatch::visitRenderables( Renderable::Visitor* visitor, bool debugRenderables ) |
575 | 0 | { |
576 | 0 | visitor->visit( this, 0, false ); |
577 | 0 | } |
578 | | //----------------------------------------------------------------------- |
579 | | void InstanceBatch::_setCustomParam( InstancedEntity *instancedEntity, unsigned char idx, |
580 | | const Vector4f &newParam ) |
581 | 0 | { |
582 | 0 | mCustomParams[instancedEntity->mInstanceId * mCreator->getNumCustomParams() + idx] = newParam; |
583 | 0 | } |
584 | | //----------------------------------------------------------------------- |
585 | | const Vector4f& InstanceBatch::_getCustomParam( InstancedEntity *instancedEntity, unsigned char idx ) |
586 | 0 | { |
587 | 0 | return mCustomParams[instancedEntity->mInstanceId * mCreator->getNumCustomParams() + idx]; |
588 | 0 | } |
589 | | } |