OgreInstancedGeometry.h
Go to the documentation of this file.
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-2013 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 #ifndef __InstancedGeometry_H__
29 #define __InstancedGeometry_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreMovableObject.h"
33 #include "OgreSimpleRenderable.h"
34 #include "OgreSkeleton.h"
35 #include "OgreSkeletonInstance.h"
36 #include "OgreAnimationTrack.h"
37 #include "OgreBone.h"
38 #include "OgreIteratorWrappers.h"
39 #include "OgreMesh.h"
40 #include "OgreHeaderPrefix.h"
41 
42 namespace Ogre {
43 
106  {
107  public:
121  {
122  public:
123  OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {}
125  {
126  delete vertexData;
127  delete indexData;
128  }
131  };
136  {
139  };
144  {
154  unsigned int ID;
155  };
160  {
165  unsigned int ID;
166  };
168 
169  // forward declarations
170  class LODBucket;
171  class MaterialBucket;
172  class BatchInstance;
173  class InstancedObject;
174 
180  {
181  protected:
182 
202  unsigned short mTexCoordIndex;
204 
205  template<typename T>
206  void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset)
207  {
208  if (indexOffset == 0)
209  {
210  memcpy(dst, src, sizeof(T) * count);
211  }
212  else
213  {
214  while(count--)
215  {
216  *dst++ = static_cast<T>(*src++ + indexOffset);
217  }
218  }
219  }
220 
221  void _initGeometryBucket(const VertexData* vData, const IndexData* iData);
222  void _initGeometryBucket(GeometryBucket* bucket);
223 
224  public:
225  GeometryBucket(MaterialBucket* parent, const String& formatString,
226  const VertexData* vData, const IndexData* iData);
227  GeometryBucket(const String& name, MaterialBucket* parent, const String& formatString,
228  const VertexData* vData, const IndexData* iData);
229  GeometryBucket(MaterialBucket* parent,const String& formatString,GeometryBucket*bucket);
230  GeometryBucket(const String& name, MaterialBucket* parent,const String& formatString,GeometryBucket*bucket);
231  virtual ~GeometryBucket();
232  MaterialBucket* getParent(void) { return mParent; }
233  Real getBoundingRadius(void) const;
235  const VertexData* getVertexData(void) const { return mVertexData; }
237  const IndexData* getIndexData(void) const { return mIndexData; }
239  const MaterialPtr& getMaterial(void) const;
240  Technique* getTechnique(void) const;
241  void getWorldTransforms(Matrix4* xform) const;
242  virtual unsigned short getNumWorldTransforms(void) const ;
243  Real getSquaredViewDepth(const Camera* cam) const;
244  const LightList& getLights(void) const;
245  bool getCastsShadows(void) const;
246  String getFormatString(void) const;
250  bool assign(QueuedGeometry* qsm);
252  void build();
254  void dump(std::ofstream& of) const;
256  AxisAlignedBox & getAABB(void){return mAABB;}
258  void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
259 
260  };
262  {
263  friend class GeometryBucket;
264  public:
266  {
272  TS_WORLD
273  };
276  protected:
278  unsigned short mIndex;
290  unsigned short mNumBoneMatrices;
293  public:
294  InstancedObject(unsigned short index);
295  InstancedObject(unsigned short index,SkeletonInstance *skeleton,AnimationStateSet*animations);
296  ~InstancedObject();
297  void setPosition( Vector3 position);
298  const Vector3& getPosition(void) const;
299  void yaw(const Radian& angle);
300  void pitch(const Radian& angle);
301  void roll(const Radian& angle);
302  void rotate(const Quaternion& q);
303  void setScale(const Vector3& scale);
304  const Vector3& getScale() const;
305  void setOrientation(const Quaternion& q);
306  void setPositionAndOrientation(Vector3 p, const Quaternion& q);
307  Quaternion & getOrientation(void);
308  void addBucketToList(GeometryBucket* bucket);
309  void needUpdate();
310  GeometryBucketList&getGeometryBucketList(void){return mGeometryBucketList;}
311  void translate(const Matrix3& axes, const Vector3& move);
312  void translate(const Vector3& d);
313  Matrix3 getLocalAxes(void) const;
314  void updateAnimation(void);
315  AnimationState* getAnimationState(const String& name) const;
316  SkeletonInstance*getSkeletonInstance(void){return mSkeletonInstance;}
317 
318  };
322  {
323  public:
326  protected:
338  // index to current Geometry Buckets for a given geometry format
342  String getGeometryFormatString(SubMeshLodGeometryLink* geom);
343 
344  public:
345  MaterialBucket(LODBucket* parent, const String& materialName);
346  virtual ~MaterialBucket();
347  LODBucket* getParent(void) { return mParent; }
349  const String& getMaterialName(void) const { return mMaterialName; }
351  void assign(QueuedGeometry* qsm);
353  void build();
355  void addRenderables(RenderQueue* queue, uint8 group,
356  Real lodValue);
358  const MaterialPtr& getMaterial(void) const { return mMaterial; }
362  GeometryIterator getGeometryIterator(void);
364  Technique* getCurrentTechnique(void) const { return mTechnique; }
366  void dump(std::ofstream& of) const;
368  MaterialBucket::CurrentGeometryMap* getMaterialBucketMap(void) const;
370  MaterialBucket::GeometryBucketList*getGeometryBucketList(void) const;
372  void updateContainers(GeometryBucket* bucket, const String &format);
373  void setLastIndex(int index){mLastIndex=index;}
374  int getLastIndex(){return mLastIndex;}
375  void setMaterial(const String & name);
376  void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
377 
378  };
385  {
386  public:
389  protected:
393  unsigned short mLod;
400  public:
401  LODBucket(BatchInstance* parent, unsigned short lod, Real lodValue);
402  virtual ~LODBucket();
403  BatchInstance* getParent(void) { return mParent; }
405  ushort getLod(void) const { return mLod; }
407  Real getLodValue(void) const { return mLodValue; }
409  void assign(QueuedSubMesh* qsm, ushort atLod);
411  void build();
413  void addRenderables(RenderQueue* queue, uint8 group,
414  Real lodValue);
418  MaterialIterator getMaterialIterator(void);
420  void dump(std::ofstream& of) const;
422  void updateContainers(MaterialBucket* bucket, String& name );
423  void visitRenderables(Renderable::Visitor* visitor, bool debugRenderables);
424 
425  };
435  {
436  friend class MaterialBucket;
437  public:
438 
439 
444  protected:
445 
456 
458  public:
473  protected:
478 
479  public:
480  BatchInstance(InstancedGeometry* parent, const String& name, SceneManager* mgr,
481  uint32 BatchInstanceID);
482  virtual ~BatchInstance();
483  // more fields can be added in subclasses
484  InstancedGeometry* getParent(void) const { return mParent;}
486  void assign(QueuedSubMesh* qmesh);
488  void build();
490  uint32 getID(void) const { return mBatchInstanceID; }
492 // const Vector3& getCentre(void) const { return mCentre; }
493  const String& getMovableType(void) const;
494  void _notifyCurrentCamera(Camera* cam);
495  const AxisAlignedBox& getBoundingBox(void) const;
496  void setBoundingBox(AxisAlignedBox& box);
497  Real getBoundingRadius(void) const;
498  void _updateRenderQueue(RenderQueue* queue);
499  bool isVisible(void) const;
501  void visitRenderables(Renderable::Visitor* visitor,
502  bool debugRenderables = false);
503 
504  // uint32 getTypeFlags(void) const;
505 
508  LODIterator getLODIterator(void);
510  const LightList& getLights(void) const;
511 
513  void updateBoundingBox();
514 
516  void dump(std::ofstream& of) const;
518  void updateContainers(LODBucket* bucket );
520  void attachToScene();
521  void addInstancedObject(unsigned short index, InstancedObject* object);
522  InstancedObject* isInstancedObjectPresent(unsigned short index);
523  InstancedObjectIterator getObjectIterator();
524  SceneNode*getSceneNode(void){return mNode;}
525  ObjectsMap& getInstancesMap(void){return mInstancesMap;}
527 
528  };
539  protected:
540  // General state & settings
543  bool mBuilt;
550  bool mVisible;
558  unsigned int mObjectCount;
573 
579 
589  virtual BatchInstance* getBatchInstance(const AxisAlignedBox& bounds, bool autoCreate);
591  virtual BatchInstance* getBatchInstance(const Vector3& point, bool autoCreate);
593  virtual BatchInstance* getBatchInstance(ushort x, ushort y, ushort z, bool autoCreate);
595  virtual BatchInstance* getBatchInstance(uint32 index);
598  virtual void getBatchInstanceIndexes(const Vector3& point,
599  ushort& x, ushort& y, ushort& z);
602  virtual BatchInstance* getInstancedGeometryInstance(void);
605  virtual uint32 packIndex(ushort x, ushort y, ushort z);
608  virtual Real getVolumeIntersection(const AxisAlignedBox& box,
609  ushort x, ushort y, ushort z);
612  virtual AxisAlignedBox getBatchInstanceBounds(ushort x, ushort y, ushort z);
615  virtual Vector3 getBatchInstanceCentre(ushort x, ushort y, ushort z);
617  virtual AxisAlignedBox calculateBounds(VertexData* vertexData,
618  const Vector3& position, const Quaternion& orientation,
619  const Vector3& scale);
621  SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm);
623  void splitGeometry(VertexData* vd, IndexData* id,
624  SubMeshLodGeometryLink* targetGeomLink);
625 
631  template <typename T>
632  void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap)
633  {
634  remap.clear();
635  for (size_t i = 0; i < numIndexes; ++i)
636  {
637  // use insert since duplicates are silently discarded
638  remap.insert(IndexRemap::value_type(*pBuffer++, remap.size()));
639  // this will have mapped oldindex -> new index IF oldindex
640  // wasn't already there
641  }
642  }
644  template <typename T>
645  void remapIndexes(T* src, T* dst, const IndexRemap& remap,
646  size_t numIndexes)
647  {
648  for (size_t i = 0; i < numIndexes; ++i)
649  {
650  // look up original and map to target
651  IndexRemap::const_iterator ix = remap.find(*src++);
652  assert(ix != remap.end());
653  *dst++ = static_cast<T>(ix->second);
654  }
655  }
656 
657  public:
659  InstancedGeometry(SceneManager* owner, const String& name);
661  virtual ~InstancedGeometry();
662 
664  const String& getName(void) const { return mName; }
684  virtual void addEntity(Entity* ent, const Vector3& position,
685  const Quaternion& orientation = Quaternion::IDENTITY,
686  const Vector3& scale = Vector3::UNIT_SCALE);
687 
707  virtual void addSceneNode(const SceneNode* node);
708 
719  virtual void build(void);
728  void addBatchInstance(void);
734  virtual void destroy(void);
735 
739  virtual void reset(void);
740 
750  virtual void setRenderingDistance(Real dist) {
751  mUpperDistance = dist;
752  mSquaredUpperDistance = mUpperDistance * mUpperDistance;
753  }
754 
756  virtual Real getRenderingDistance(void) const { return mUpperDistance; }
757 
759  virtual Real getSquaredRenderingDistance(void) const
760  { return mSquaredUpperDistance; }
761 
763  virtual void setVisible(bool visible);
764 
766  virtual bool isVisible(void) const { return mVisible; }
767 
785  virtual void setCastShadows(bool castShadows);
787  virtual bool getCastShadows(void) { return mCastShadows; }
788 
799  virtual void setBatchInstanceDimensions(const Vector3& size) {
800  mBatchInstanceDimensions = size;
801  mHalfBatchInstanceDimensions = size * 0.5;
802  }
804  virtual const Vector3& getBatchInstanceDimensions(void) const { return mBatchInstanceDimensions; }
816  virtual void setOrigin(const Vector3& origin) { mOrigin = origin; }
818  virtual const Vector3& getOrigin(void) const { return mOrigin; }
819 
831  virtual void setRenderQueueGroup(uint8 queueID);
832 
834  virtual uint8 getRenderQueueGroup(void) const;
838  BatchInstanceIterator getBatchInstanceIterator(void);
842  void visitRenderables(Renderable::Visitor* visitor,
843  bool debugRenderables = false);
844 
848  virtual void dump(const String& filename) const;
853  SkeletonInstance *getBaseSkeletonInstance(void){return mSkeletonInstance;}
858  SkeletonPtr getBaseSkeleton(void){return mBaseSkeleton;}
863  AnimationStateSet* getBaseAnimationState(void){return mAnimationState;}
868  unsigned int getObjectCount(void){return mObjectCount;}
869 
876  virtual void setProvideWorldInverses(bool flag);
877 
883  virtual bool getProvideWorldInverses(void) const { return mProvideWorldInverses; }
884  };
885 
888 }
889 
890 #include "OgreHeaderSuffix.h"
891 
892 #endif
893 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Oct 16 2013 14:35:42