| 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-2006 Torus Knot Software Ltd |
|---|
| 8 | Also see acknowledgements in Readme.html |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | |
|---|
| 24 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | #ifndef __StaticGeometry_H__ |
|---|
| 30 | #define __StaticGeometry_H__ |
|---|
| 31 | |
|---|
| 32 | #include "OgrePrerequisites.h" |
|---|
| 33 | #include "OgreMovableObject.h" |
|---|
| 34 | #include "OgreRenderable.h" |
|---|
| 35 | |
|---|
| 36 | namespace Ogre { |
|---|
| 37 | |
|---|
| 38 | /** Pre-transforms and batches up meshes for efficient use as static |
|---|
| 39 | geometry in a scene. |
|---|
| 40 | @remarks |
|---|
| 41 | Modern graphics cards (GPUs) prefer to receive geometry in large |
|---|
| 42 | batches. It is orders of magnitude faster to render 10 batches |
|---|
| 43 | of 10,000 triangles than it is to render 10,000 batches of 10 |
|---|
| 44 | triangles, even though both result in the same number of on-screen |
|---|
| 45 | triangles. |
|---|
| 46 | @par |
|---|
| 47 | Therefore it is important when you are rendering a lot of geometry to |
|---|
| 48 | batch things up into as few rendering calls as possible. This |
|---|
| 49 | class allows you to build a batched object from a series of entities |
|---|
| 50 | in order to benefit from this behaviour. |
|---|
| 51 | Batching has implications of it's own though: |
|---|
| 52 | @li Batched geometry cannot be subdivided; that means that the whole |
|---|
| 53 | group will be displayed, or none of it will. This obivously has |
|---|
| 54 | culling issues. |
|---|
| 55 | @li A single world transform must apply to the entire batch. Therefore |
|---|
| 56 | once you have batched things, you can't move them around relative to |
|---|
| 57 | each other. That's why this class is most useful when dealing with |
|---|
| 58 | static geometry (hence the name). In addition, geometry is |
|---|
| 59 | effectively duplicated, so if you add 3 entities based on the same |
|---|
| 60 | mesh in different positions, they will use 3 times the geometry |
|---|
| 61 | space than the movable version (which re-uses the same geometry). |
|---|
| 62 | So you trade memory and flexibility of movement for pure speed when |
|---|
| 63 | using this class. |
|---|
| 64 | @li A single material must apply for each batch. In fact this class |
|---|
| 65 | allows you to use multiple materials, but you should be aware that |
|---|
| 66 | internally this means that there is one batch per material. |
|---|
| 67 | Therefore you won't gain as much benefit from the batching if you |
|---|
| 68 | use many different materials; try to keep the number down. |
|---|
| 69 | @par |
|---|
| 70 | In order to retain some sort of culling, this class will batch up |
|---|
| 71 | meshes in localised regions. The size and shape of these blocks is |
|---|
| 72 | controlled by the SceneManager which contructs this object, since it |
|---|
| 73 | makes sense to batch things up in the most appropriate way given the |
|---|
| 74 | existing partitioning of the scene. |
|---|
| 75 | @par |
|---|
| 76 | The LOD settings of both the Mesh and the Materials used in |
|---|
| 77 | constructing this static geometry will be respected. This means that |
|---|
| 78 | if you use meshes/materials which have LOD, batches in the distance |
|---|
| 79 | will have a lower polygon count or material detail to those in the |
|---|
| 80 | foreground. Since each mesh might have different LOD distances, during |
|---|
| 81 | build the furthest distance at each LOD level from all meshes |
|---|
| 82 | in that region is used. This means all the LOD levels change at the |
|---|
| 83 | same time, but at the furthest distance of any of them (so quality is |
|---|
| 84 | not degraded). Be aware that using Mesh LOD in this class will |
|---|
| 85 | further increase the memory required. Only generated LOD |
|---|
| 86 | is supported for meshes. |
|---|
| 87 | @par |
|---|
| 88 | There are 2 ways you can add geometry to this class; you can add |
|---|
| 89 | Entity objects directly with predetermined positions, scales and |
|---|
| 90 | orientations, or you can add an entire SceneNode and it's subtree, |
|---|
| 91 | including all the objects attached to it. Once you've added everthing |
|---|
| 92 | you need to, you have to call build() the fix the geometry in place. |
|---|
| 93 | @note |
|---|
| 94 | This class is not a replacement for world geometry (@see |
|---|
| 95 | SceneManager::setWorldGeometry). The single most efficient way to |
|---|
| 96 | render large amounts of static geometry is to use a SceneManager which |
|---|
| 97 | is specialised for dealing with that particular world structure. |
|---|
| 98 | However, this class does provide you with a good 'halfway house' |
|---|
| 99 | between generalised movable geometry (Entity) which works with all |
|---|
| 100 | SceneManagers but isn't efficient when using very large numbers, and |
|---|
| 101 | highly specialised world geometry which is extremely fast but not |
|---|
| 102 | generic and typically requires custom world editors. |
|---|
| 103 | @par |
|---|
| 104 | You should not construct instances of this class directly; instead, cal |
|---|
| 105 | SceneManager::createStaticGeometry, which gives the SceneManager the |
|---|
| 106 | option of providing you with a specialised version of this class if it |
|---|
| 107 | wishes, and also handles the memory management for you like other |
|---|
| 108 | classes. |
|---|
| 109 | @note |
|---|
| 110 | Warning: this class only works with triangle lists at the moment, |
|---|
| 111 | do not pass it triangle strips, fans or lines / points. |
|---|
| 112 | */ |
|---|
| 113 | class _OgreExport StaticGeometry |
|---|
| 114 | { |
|---|
| 115 | public: |
|---|
| 116 | /** Struct holding geometry optimised per SubMesh / lod level, ready |
|---|
| 117 | for copying to instances. |
|---|
| 118 | @remarks |
|---|
| 119 | Since we're going to be duplicating geometry lots of times, it's |
|---|
| 120 | far more important that we don't have redundant vertex data. If a |
|---|
| 121 | SubMesh uses shared geometry, or we're looking at a lower LOD, not |
|---|
| 122 | all the vertices are being referenced by faces on that submesh. |
|---|
| 123 | Therefore to duplicate them, potentially hundreds or even thousands |
|---|
| 124 | of times, would be extremely wasteful. Therefore, if a SubMesh at |
|---|
| 125 | a given LOD has wastage, we create an optimised version of it's |
|---|
| 126 | geometry which is ready for copying with no wastage. |
|---|
| 127 | */ |
|---|
| 128 | class _OgrePrivate OptimisedSubMeshGeometry |
|---|
| 129 | { |
|---|
| 130 | public: |
|---|
| 131 | OptimisedSubMeshGeometry() :vertexData(0), indexData(0) {} |
|---|
| 132 | ~OptimisedSubMeshGeometry() |
|---|
| 133 | { |
|---|
| 134 | delete vertexData; |
|---|
| 135 | delete indexData; |
|---|
| 136 | } |
|---|
| 137 | VertexData *vertexData; |
|---|
| 138 | IndexData *indexData; |
|---|
| 139 | }; |
|---|
| 140 | typedef std::list<OptimisedSubMeshGeometry*> OptimisedSubMeshGeometryList; |
|---|
| 141 | /// Saved link between SubMesh at a LOD and vertex/index data |
|---|
| 142 | /// May point to original or optimised geometry |
|---|
| 143 | struct SubMeshLodGeometryLink |
|---|
| 144 | { |
|---|
| 145 | VertexData* vertexData; |
|---|
| 146 | IndexData* indexData; |
|---|
| 147 | }; |
|---|
| 148 | typedef std::vector<SubMeshLodGeometryLink> SubMeshLodGeometryLinkList; |
|---|
| 149 | typedef std::map<SubMesh*, SubMeshLodGeometryLinkList*> SubMeshGeometryLookup; |
|---|
| 150 | /// Structure recording a queued submesh for the build |
|---|
| 151 | struct QueuedSubMesh |
|---|
| 152 | { |
|---|
| 153 | SubMesh* submesh; |
|---|
| 154 | /// Link to LOD list of geometry, potentially optimised |
|---|
| 155 | SubMeshLodGeometryLinkList* geometryLodList; |
|---|
| 156 | String materialName; |
|---|
| 157 | Vector3 position; |
|---|
| 158 | Quaternion orientation; |
|---|
| 159 | Vector3 scale; |
|---|
| 160 | /// Pre-transformed world AABB |
|---|
| 161 | AxisAlignedBox worldBounds; |
|---|
| 162 | }; |
|---|
| 163 | typedef std::vector<QueuedSubMesh*> QueuedSubMeshList; |
|---|
| 164 | /// Structure recording a queued geometry for low level builds |
|---|
| 165 | struct QueuedGeometry |
|---|
| 166 | { |
|---|
| 167 | SubMeshLodGeometryLink* geometry; |
|---|
| 168 | Vector3 position; |
|---|
| 169 | Quaternion orientation; |
|---|
| 170 | Vector3 scale; |
|---|
| 171 | }; |
|---|
| 172 | typedef std::vector<QueuedGeometry*> QueuedGeometryList; |
|---|
| 173 | |
|---|
| 174 | // forward declarations |
|---|
| 175 | class LODBucket; |
|---|
| 176 | class MaterialBucket; |
|---|
| 177 | class Region; |
|---|
| 178 | |
|---|
| 179 | /** A GeometryBucket is a the lowest level bucket where geometry with |
|---|
| 180 | the same vertex & index format is stored. It also acts as the |
|---|
| 181 | renderable. |
|---|
| 182 | */ |
|---|
| 183 | class _OgreExport GeometryBucket : public Renderable |
|---|
| 184 | { |
|---|
| 185 | protected: |
|---|
| 186 | /// Geometry which has been queued up pre-build (not for deallocation) |
|---|
| 187 | QueuedGeometryList mQueuedGeometry; |
|---|
| 188 | /// Pointer to parent bucket |
|---|
| 189 | MaterialBucket* mParent; |
|---|
| 190 | /// String identifying the vertex / index format |
|---|
| 191 | String mFormatString; |
|---|
| 192 | /// Vertex information, includes current number of vertices |
|---|
| 193 | /// committed to be a part of this bucket |
|---|
| 194 | VertexData* mVertexData; |
|---|
| 195 | /// Index information, includes index type which limits the max |
|---|
| 196 | /// number of vertices which are allowed in one bucket |
|---|
| 197 | IndexData* mIndexData; |
|---|
| 198 | /// Size of indexes |
|---|
| 199 | HardwareIndexBuffer::IndexType mIndexType; |
|---|
| 200 | /// Maximum vertex indexable |
|---|
| 201 | size_t mMaxVertexIndex; |
|---|
| 202 | |
|---|
| 203 | template<typename T> |
|---|
| 204 | void copyIndexes(const T* src, T* dst, size_t count, size_t indexOffset) |
|---|
| 205 | { |
|---|
| 206 | if (indexOffset == 0) |
|---|
| 207 | { |
|---|
| 208 | memcpy(dst, src, sizeof(T) * count); |
|---|
| 209 | } |
|---|
| 210 | else |
|---|
| 211 | { |
|---|
| 212 | while(count--) |
|---|
| 213 | { |
|---|
| 214 | *dst++ = static_cast<T>(*src++ + indexOffset); |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | public: |
|---|
| 219 | GeometryBucket(MaterialBucket* parent, const String& formatString, |
|---|
| 220 | const VertexData* vData, const IndexData* iData); |
|---|
| 221 | virtual ~GeometryBucket(); |
|---|
| 222 | MaterialBucket* getParent(void) { return mParent; } |
|---|
| 223 | /// Get the vertex data for this geometry |
|---|
| 224 | const VertexData* getVertexData(void) const { return mVertexData; } |
|---|
| 225 | /// Get the index data for this geometry |
|---|
| 226 | const IndexData* getIndexData(void) const { return mIndexData; } |
|---|
| 227 | /// @copydoc Renderable::getMaterial |
|---|
| 228 | const MaterialPtr& getMaterial(void) const; |
|---|
| 229 | Technique* getTechnique(void) const; |
|---|
| 230 | void getRenderOperation(RenderOperation& op); |
|---|
| 231 | void getWorldTransforms(Matrix4* xform) const; |
|---|
| 232 | const Quaternion& getWorldOrientation(void) const; |
|---|
| 233 | const Vector3& getWorldPosition(void) const; |
|---|
| 234 | Real getSquaredViewDepth(const Camera* cam) const; |
|---|
| 235 | const LightList& getLights(void) const; |
|---|
| 236 | bool getCastsShadows(void) const; |
|---|
| 237 | |
|---|
| 238 | /** Try to assign geometry to this bucket. |
|---|
| 239 | @returns false if there is no room left in this bucket |
|---|
| 240 | */ |
|---|
| 241 | bool assign(QueuedGeometry* qsm); |
|---|
| 242 | /// Build |
|---|
| 243 | void build(bool stencilShadows); |
|---|
| 244 | /// Dump contents for diagnostics |
|---|
| 245 | void dump(std::ofstream& of) const; |
|---|
| 246 | }; |
|---|
| 247 | /** A MaterialBucket is a collection of smaller buckets with the same |
|---|
| 248 | Material (and implicitly the same LOD). */ |
|---|
| 249 | class _OgreExport MaterialBucket |
|---|
| 250 | { |
|---|
| 251 | public: |
|---|
| 252 | /// list of Geometry Buckets in this region |
|---|
| 253 | typedef std::vector<GeometryBucket*> GeometryBucketList; |
|---|
| 254 | protected: |
|---|
| 255 | /// Pointer to parent LODBucket |
|---|
| 256 | LODBucket* mParent; |
|---|
| 257 | /// Material being used |
|---|
| 258 | String mMaterialName; |
|---|
| 259 | /// Pointer to material being used |
|---|
| 260 | MaterialPtr mMaterial; |
|---|
| 261 | /// Active technique |
|---|
| 262 | Technique* mTechnique; |
|---|
| 263 | |
|---|
| 264 | /// list of Geometry Buckets in this region |
|---|
| 265 | GeometryBucketList mGeometryBucketList; |
|---|
| 266 | // index to current Geometry Buckets for a given geometry format |
|---|
| 267 | typedef std::map<String, GeometryBucket*> CurrentGeometryMap; |
|---|
| 268 | CurrentGeometryMap mCurrentGeometryMap; |
|---|
| 269 | /// Get a packed string identifying the geometry format |
|---|
| 270 | String getGeometryFormatString(SubMeshLodGeometryLink* geom); |
|---|
| 271 | |
|---|
| 272 | public: |
|---|
| 273 | MaterialBucket(LODBucket* parent, const String& materialName); |
|---|
| 274 | virtual ~MaterialBucket(); |
|---|
| 275 | LODBucket* getParent(void) { return mParent; } |
|---|
| 276 | /// Get the material name |
|---|
| 277 | const String& getMaterialName(void) const { return mMaterialName; } |
|---|
| 278 | /// Assign geometry to this bucket |
|---|
| 279 | void assign(QueuedGeometry* qsm); |
|---|
| 280 | /// Build |
|---|
| 281 | void build(bool stencilShadows); |
|---|
| 282 | /// Add children to the render queue |
|---|
| 283 | void addRenderables(RenderQueue* queue, uint8 group, |
|---|
| 284 | Real camSquaredDist); |
|---|
| 285 | /// Get the material for this bucket |
|---|
| 286 | const MaterialPtr& getMaterial(void) const { return mMaterial; } |
|---|
| 287 | /// Iterator over geometry |
|---|
| 288 | typedef VectorIterator<GeometryBucketList> GeometryIterator; |
|---|
| 289 | /// Get an iterator over the contained geometry |
|---|
| 290 | GeometryIterator getGeometryIterator(void); |
|---|
| 291 | /// Get the current Technique |
|---|
| 292 | Technique* getCurrentTechnique(void) const { return mTechnique; } |
|---|
| 293 | /// Dump contents for diagnostics |
|---|
| 294 | void dump(std::ofstream& of) const; |
|---|
| 295 | }; |
|---|
| 296 | /** A LODBucket is a collection of smaller buckets with the same LOD. |
|---|
| 297 | @remarks |
|---|
| 298 | LOD refers to Mesh LOD here. Material LOD can change separately |
|---|
| 299 | at the next bucket down from this. |
|---|
| 300 | */ |
|---|
| 301 | class _OgreExport LODBucket |
|---|
| 302 | { |
|---|
| 303 | public: |
|---|
| 304 | /// Lookup of Material Buckets in this region |
|---|
| 305 | typedef std::map<String, MaterialBucket*> MaterialBucketMap; |
|---|
| 306 | protected: |
|---|
| 307 | /// Pointer to parent region |
|---|
| 308 | Region* mParent; |
|---|
| 309 | /// LOD level (0 == full LOD) |
|---|
| 310 | unsigned short mLod; |
|---|
| 311 | /// distance at which this LOD starts to apply (squared) |
|---|
| 312 | Real mSquaredDistance; |
|---|
| 313 | /// Lookup of Material Buckets in this region |
|---|
| 314 | MaterialBucketMap mMaterialBucketMap; |
|---|
| 315 | /// Geometry queued for a single LOD (deallocated here) |
|---|
| 316 | QueuedGeometryList mQueuedGeometryList; |
|---|
| 317 | public: |
|---|
| 318 | LODBucket(Region* parent, unsigned short lod, Real lodDist); |
|---|
| 319 | virtual ~LODBucket(); |
|---|
| 320 | Region* getParent(void) { return mParent; } |
|---|
| 321 | /// Get the lod index |
|---|
| 322 | ushort getLod(void) const { return mLod; } |
|---|
| 323 | /// Get the lod squared distance |
|---|
| 324 | Real getSquaredDistance(void) const { return mSquaredDistance; } |
|---|
| 325 | /// Assign a queued submesh to this bucket, using specified mesh LOD |
|---|
| 326 | void assign(QueuedSubMesh* qsm, ushort atLod); |
|---|
| 327 | /// Build |
|---|
| 328 | void build(bool stencilShadows); |
|---|
| 329 | /// Add children to the render queue |
|---|
| 330 | void addRenderables(RenderQueue* queue, uint8 group, |
|---|
| 331 | Real camSquaredDistance); |
|---|
| 332 | /// Iterator over the materials in this LOD |
|---|
| 333 | typedef MapIterator<MaterialBucketMap> MaterialIterator; |
|---|
| 334 | /// Get an iterator over the materials in this LOD |
|---|
| 335 | MaterialIterator getMaterialIterator(void); |
|---|
| 336 | /// Dump contents for diagnostics |
|---|
| 337 | void dump(std::ofstream& of) const; |
|---|
| 338 | |
|---|
| 339 | }; |
|---|
| 340 | /** The details of a topological region which is the highest level of |
|---|
| 341 | partitioning for this class. |
|---|
| 342 | @remarks |
|---|
| 343 | The size & shape of regions entirely depends on the SceneManager |
|---|
| 344 | specific implementation. It is a MovableObject since it will be |
|---|
| 345 | attached to a node based on the local centre - in practice it |
|---|
| 346 | won't actually move (although in theory it could). |
|---|
| 347 | */ |
|---|
| 348 | class _OgreExport Region : public MovableObject |
|---|
| 349 | { |
|---|
| 350 | public: |
|---|
| 351 | /// list of LOD Buckets in this region |
|---|
| 352 | typedef std::vector<LODBucket*> LODBucketList; |
|---|
| 353 | protected: |
|---|
| 354 | /** Nested class to allow region shadows. */ |
|---|
| 355 | class _OgreExport RegionShadowRenderable : public ShadowRenderable |
|---|
| 356 | { |
|---|
| 357 | protected: |
|---|
| 358 | Region* mParent; |
|---|
| 359 | // Shared link to position buffer |
|---|
| 360 | HardwareVertexBufferSharedPtr mPositionBuffer; |
|---|
| 361 | // Shared link to w-coord buffer (optional) |
|---|
| 362 | HardwareVertexBufferSharedPtr mWBuffer; |
|---|
| 363 | |
|---|
| 364 | public: |
|---|
| 365 | RegionShadowRenderable(Region* parent, |
|---|
| 366 | HardwareIndexBufferSharedPtr* indexBuffer, const VertexData* vertexData, |
|---|
| 367 | bool createSeparateLightCap, bool isLightCap = false); |
|---|
| 368 | ~RegionShadowRenderable(); |
|---|
| 369 | /// Overridden from ShadowRenderable |
|---|
| 370 | void getWorldTransforms(Matrix4* xform) const; |
|---|
| 371 | /// Overridden from ShadowRenderable |
|---|
| 372 | const Quaternion& getWorldOrientation(void) const; |
|---|
| 373 | /// Overridden from ShadowRenderable |
|---|
| 374 | const Vector3& getWorldPosition(void) const; |
|---|
| 375 | HardwareVertexBufferSharedPtr getPositionBuffer(void) { return mPositionBuffer; } |
|---|
| 376 | HardwareVertexBufferSharedPtr getWBuffer(void) { return mWBuffer; } |
|---|
| 377 | |
|---|
| 378 | }; |
|---|
| 379 | /// Parent static geometry |
|---|
| 380 | StaticGeometry* mParent; |
|---|
| 381 | /// Scene manager link |
|---|
| 382 | SceneManager* mSceneMgr; |
|---|
| 383 | /// Scene node |
|---|
| 384 | SceneNode* mNode; |
|---|
| 385 | /// Local list of queued meshes (not used for deallocation) |
|---|
| 386 | QueuedSubMeshList mQueuedSubMeshes; |
|---|
| 387 | /// Unique identifier for the region |
|---|
| 388 | uint32 mRegionID; |
|---|
| 389 | /// Center of the region |
|---|
| 390 | Vector3 mCentre; |
|---|
| 391 | /// LOD distances (squared) as built up - use the max at each level |
|---|
| 392 | std::vector<Real> mLodSquaredDistances; |
|---|
| 393 | /// Local AABB relative to region centre |
|---|
| 394 | AxisAlignedBox mAABB; |
|---|
| 395 | /// Local bounding radius |
|---|
| 396 | Real mBoundingRadius; |
|---|
| 397 | /// The current lod level, as determined from the last camera |
|---|
| 398 | ushort mCurrentLod; |
|---|
| 399 | /// Current camera distance, passed on to do material lod later |
|---|
| 400 | Real mCamDistanceSquared; |
|---|
| 401 | /// List of LOD buckets |
|---|
| 402 | LODBucketList mLodBucketList; |
|---|
| 403 | /// List of lights for this region |
|---|
| 404 | mutable LightList mLightList; |
|---|
| 405 | /// The last frame that this light list was updated in |
|---|
| 406 | mutable ulong mLightListUpdated; |
|---|
| 407 | /// Edge list, used if stencil shadow casting is enabled |
|---|
| 408 | EdgeData* mEdgeList; |
|---|
| 409 | /// List of shadow renderables |
|---|
| 410 | ShadowRenderableList mShadowRenderables; |
|---|
| 411 | /// Is a vertex program in use somewhere in this region? |
|---|
| 412 | bool mVertexProgramInUse; |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | public: |
|---|
| 417 | Region(StaticGeometry* parent, const String& name, SceneManager* mgr, |
|---|
| 418 | uint32 regionID, const Vector3& centre); |
|---|
| 419 | virtual ~Region(); |
|---|
| 420 | // more fields can be added in subclasses |
|---|
| 421 | StaticGeometry* getParent(void) const { return mParent;} |
|---|
| 422 | /// Assign a queued mesh to this region, read for final build |
|---|
| 423 | void assign(QueuedSubMesh* qmesh); |
|---|
| 424 | /// Build this region |
|---|
| 425 | void build(bool stencilShadows); |
|---|
| 426 | /// Get the region ID of this region |
|---|
| 427 | uint32 getID(void) const { return mRegionID; } |
|---|
| 428 | /// Get the centre point of the region |
|---|
| 429 | const Vector3& getCentre(void) const { return mCentre; } |
|---|
| 430 | const String& getMovableType(void) const; |
|---|
| 431 | void _notifyCurrentCamera(Camera* cam); |
|---|
| 432 | const AxisAlignedBox& getBoundingBox(void) const; |
|---|
| 433 | Real getBoundingRadius(void) const; |
|---|
| 434 | void _updateRenderQueue(RenderQueue* queue); |
|---|
| 435 | bool isVisible(void) const; |
|---|
| 436 | uint32 getTypeFlags(void) const; |
|---|
| 437 | |
|---|
| 438 | typedef VectorIterator<LODBucketList> LODIterator; |
|---|
| 439 | /// Get an iterator over the LODs in this region |
|---|
| 440 | LODIterator getLODIterator(void); |
|---|
| 441 | /// @copydoc ShadowCaster::getShadowVolumeRenderableIterator |
|---|
| 442 | ShadowRenderableListIterator getShadowVolumeRenderableIterator( |
|---|
| 443 | ShadowTechnique shadowTechnique, const Light* light, |
|---|
| 444 | HardwareIndexBufferSharedPtr* indexBuffer, |
|---|
| 445 | bool extrudeVertices, Real extrusionDistance, unsigned long flags = 0 ); |
|---|
| 446 | /// Overridden from MovableObject |
|---|
| 447 | EdgeData* getEdgeList(void); |
|---|
| 448 | /** Overridden member from ShadowCaster. */ |
|---|
| 449 | bool hasEdgeList(void); |
|---|
| 450 | |
|---|
| 451 | /// Dump contents for diagnostics |
|---|
| 452 | void dump(std::ofstream& of) const; |
|---|
| 453 | |
|---|
| 454 | }; |
|---|
| 455 | /** Indexed region map based on packed x/y/z region index, 10 bits for |
|---|
| 456 | each axis. |
|---|
| 457 | @remarks |
|---|
| 458 | Regions are indexed 0-1023 in all axes, where for example region |
|---|
| 459 | 0 in the x axis begins at mOrigin.x + (mRegionDimensions.x * -512), |
|---|
| 460 | and region 1023 ends at mOrigin + (mRegionDimensions.x * 512). |
|---|
| 461 | */ |
|---|
| 462 | typedef std::map<uint32, Region*> RegionMap; |
|---|
| 463 | protected: |
|---|
| 464 | // General state & settings |
|---|
| 465 | SceneManager* mOwner; |
|---|
| 466 | String mName; |
|---|
| 467 | bool mBuilt; |
|---|
| 468 | Real mUpperDistance; |
|---|
| 469 | Real mSquaredUpperDistance; |
|---|
| 470 | bool mCastShadows; |
|---|
| 471 | Vector3 mRegionDimensions; |
|---|
| 472 | Vector3 mHalfRegionDimensions; |
|---|
| 473 | Vector3 mOrigin; |
|---|
| 474 | bool mVisible; |
|---|
| 475 | /// The render queue to use when rendering this object |
|---|
| 476 | uint8 mRenderQueueID; |
|---|
| 477 | /// Flags whether the RenderQueue's default should be used. |
|---|
| 478 | bool mRenderQueueIDSet; |
|---|
| 479 | |
|---|
| 480 | QueuedSubMeshList mQueuedSubMeshes; |
|---|
| 481 | |
|---|
| 482 | /// List of geometry which has been optimised for SubMesh use |
|---|
| 483 | /// This is the primary storage used for cleaning up later |
|---|
| 484 | OptimisedSubMeshGeometryList mOptimisedSubMeshGeometryList; |
|---|
| 485 | |
|---|
| 486 | /** Cached links from SubMeshes to (potentially optimised) geometry |
|---|
| 487 | This is not used for deletion since the lookup may reference |
|---|
| 488 | original vertex data |
|---|
| 489 | */ |
|---|
| 490 | SubMeshGeometryLookup mSubMeshGeometryLookup; |
|---|
| 491 | |
|---|
| 492 | /// Map of regions |
|---|
| 493 | RegionMap mRegionMap; |
|---|
| 494 | |
|---|
| 495 | /** Virtual method for getting a region most suitable for the |
|---|
| 496 | passed in bounds. Can be overridden by subclasses. |
|---|
| 497 | */ |
|---|
| 498 | virtual Region* getRegion(const AxisAlignedBox& bounds, bool autoCreate); |
|---|
| 499 | /** Get the region within which a point lies */ |
|---|
| 500 | virtual Region* getRegion(const Vector3& point, bool autoCreate); |
|---|
| 501 | /** Get the region using indexes */ |
|---|
| 502 | virtual Region* getRegion(ushort x, ushort y, ushort z, bool autoCreate); |
|---|
| 503 | /** Get the region using a packed index, returns null if it doesn't exist. */ |
|---|
| 504 | virtual Region* getRegion(uint32 index); |
|---|
| 505 | /** Get the region indexes for a point. |
|---|
| 506 | */ |
|---|
| 507 | virtual void getRegionIndexes(const Vector3& point, |
|---|
| 508 | ushort& x, ushort& y, ushort& z); |
|---|
| 509 | /** Pack 3 indexes into a single index value |
|---|
| 510 | */ |
|---|
| 511 | virtual uint32 packIndex(ushort x, ushort y, ushort z); |
|---|
| 512 | /** Get the volume intersection for an indexed region with some bounds. |
|---|
| 513 | */ |
|---|
| 514 | virtual Real getVolumeIntersection(const AxisAlignedBox& box, |
|---|
| 515 | ushort x, ushort y, ushort z); |
|---|
| 516 | /** Get the bounds of an indexed region. |
|---|
| 517 | */ |
|---|
| 518 | virtual AxisAlignedBox getRegionBounds(ushort x, ushort y, ushort z); |
|---|
| 519 | /** Get the centre of an indexed region. |
|---|
| 520 | */ |
|---|
| 521 | virtual Vector3 getRegionCentre(ushort x, ushort y, ushort z); |
|---|
| 522 | /** Calculate world bounds from a set of vertex data. */ |
|---|
| 523 | virtual AxisAlignedBox calculateBounds(VertexData* vertexData, |
|---|
| 524 | const Vector3& position, const Quaternion& orientation, |
|---|
| 525 | const Vector3& scale); |
|---|
| 526 | /** Look up or calculate the geometry data to use for this SubMesh */ |
|---|
| 527 | SubMeshLodGeometryLinkList* determineGeometry(SubMesh* sm); |
|---|
| 528 | /** Split some shared geometry into dedicated geometry. */ |
|---|
| 529 | void splitGeometry(VertexData* vd, IndexData* id, |
|---|
| 530 | SubMeshLodGeometryLink* targetGeomLink); |
|---|
| 531 | |
|---|
| 532 | typedef std::map<size_t, size_t> IndexRemap; |
|---|
| 533 | /** Method for figuring out which vertices are used by an index buffer |
|---|
| 534 | and calculating a remap lookup for a vertex buffer just containing |
|---|
| 535 | those vertices. |
|---|
| 536 | */ |
|---|
| 537 | template <typename T> |
|---|
| 538 | void buildIndexRemap(T* pBuffer, size_t numIndexes, IndexRemap& remap) |
|---|
| 539 | { |
|---|
| 540 | remap.clear(); |
|---|
| 541 | for (size_t i = 0; i < numIndexes; ++i) |
|---|
| 542 | { |
|---|
| 543 | // use insert since duplicates are silently discarded |
|---|
| 544 | remap.insert(IndexRemap::value_type(*pBuffer++, remap.size())); |
|---|
| 545 | // this will have mapped oldindex -> new index IF oldindex |
|---|
| 546 | // wasn't already there |
|---|
| 547 | } |
|---|
| 548 | } |
|---|
| 549 | /** Method for altering indexes based on a remap. */ |
|---|
| 550 | template <typename T> |
|---|
| 551 | void remapIndexes(T* src, T* dst, const IndexRemap& remap, |
|---|
| 552 | size_t numIndexes) |
|---|
| 553 | { |
|---|
| 554 | for (size_t i = 0; i < numIndexes; ++i) |
|---|
| 555 | { |
|---|
| 556 | // look up original and map to target |
|---|
| 557 | IndexRemap::const_iterator ix = remap.find(*src++); |
|---|
| 558 | assert(ix != remap.end()); |
|---|
| 559 | *dst++ = static_cast<T>(ix->second); |
|---|
| 560 | } |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | public: |
|---|
| 564 | /// Constructor; do not use directly (@see SceneManager::createStaticGeometry) |
|---|
| 565 | StaticGeometry(SceneManager* owner, const String& name); |
|---|
| 566 | /// Destructor |
|---|
| 567 | virtual ~StaticGeometry(); |
|---|
| 568 | |
|---|
| 569 | /// Get the name of this object |
|---|
| 570 | const String& getName(void) const { return mName; } |
|---|
| 571 | /** Adds an Entity to the static geometry. |
|---|
| 572 | @remarks |
|---|
| 573 | This method takes an existing Entity and adds its details to the |
|---|
| 574 | list of elements to include when building. Note that the Entity |
|---|
| 575 | itself is not copied or referenced in this method; an Entity is |
|---|
| 576 | passed simply so that you can change the materials of attached |
|---|
| 577 | SubEntity objects if you want. You can add the same Entity |
|---|
| 578 | instance multiple times with different material settings |
|---|
| 579 | completely safely, and destroy the Entity before destroying |
|---|
| 580 | this StaticGeometry if you like. The Entity passed in is simply |
|---|
| 581 | used as a definition. |
|---|
| 582 | @note Must be called before 'build'. |
|---|
| 583 | @param ent The Entity to use as a definition (the Mesh and Materials |
|---|
| 584 | referenced will be recorded for the build call). |
|---|
| 585 | @param position The world position at which to add this Entity |
|---|
| 586 | @param orientation The world orientation at which to add this Entity |
|---|
| 587 | @param scale The scale at which to add this entity |
|---|
| 588 | */ |
|---|
| 589 | virtual void addEntity(Entity* ent, const Vector3& position, |
|---|
| 590 | const Quaternion& orientation = Quaternion::IDENTITY, |
|---|
| 591 | const Vector3& scale = Vector3::UNIT_SCALE); |
|---|
| 592 | |
|---|
| 593 | /** Adds all the Entity objects attached to a SceneNode and all it's |
|---|
| 594 | children to the static geometry. |
|---|
| 595 | @remarks |
|---|
| 596 | This method performs just like addEntity, except it adds all the |
|---|
| 597 | entities attached to an entire sub-tree to the geometry. |
|---|
| 598 | The position / orientation / scale parameters are taken from the |
|---|
| 599 | node structure instead of being specified manually. |
|---|
| 600 | @note |
|---|
| 601 | The SceneNode you pass in will not be automatically detached from |
|---|
| 602 | it's parent, so if you have this node already attached to the scene |
|---|
| 603 | graph, you will need to remove it if you wish to avoid the overhead |
|---|
| 604 | of rendering <i>both</i> the original objects and their new static |
|---|
| 605 | versions! We don't do this for you incase you are preparing this |
|---|
| 606 | in advance and so don't want the originals detached yet. |
|---|
| 607 | @note Must be called before 'build'. |
|---|
| 608 | @param node Pointer to the node to use to provide a set of Entity |
|---|
| 609 | templates |
|---|
| 610 | */ |
|---|
| 611 | virtual void addSceneNode(const SceneNode* node); |
|---|
| 612 | |
|---|
| 613 | /** Build the geometry. |
|---|
| 614 | @remarks |
|---|
| 615 | Based on all the entities which have been added, and the batching |
|---|
| 616 | options which have been set, this method constructs the batched |
|---|
| 617 | geometry structures required. The batches are added to the scene |
|---|
| 618 | and will be rendered unless you specifically hide them. |
|---|
| 619 | @note |
|---|
| 620 | Once you have called this method, you can no longer add any more |
|---|
| 621 | entities. |
|---|
| 622 | */ |
|---|
| 623 | virtual void build(void); |
|---|
| 624 | |
|---|
| 625 | /** Destroys all the built geometry state (reverse of build). |
|---|
| 626 | @remarks |
|---|
| 627 | You can call build() again after this and it will pick up all the |
|---|
| 628 | same entities / nodes you queued last time. |
|---|
| 629 | */ |
|---|
| 630 | virtual void destroy(void); |
|---|
| 631 | |
|---|
| 632 | /** Clears any of the entities / nodes added to this geometry and |
|---|
| 633 | destroys anything which has already been built. |
|---|
| 634 | */ |
|---|
| 635 | virtual void reset(void); |
|---|
| 636 | |
|---|
| 637 | /** Sets the distance at which batches are no longer rendered. |
|---|
| 638 | @remarks |
|---|
| 639 | This lets you turn off batches at a given distance. This can be |
|---|
| 640 | useful for things like detail meshes (grass, foliage etc) and could |
|---|
| 641 | be combined with a shader which fades the geometry out beforehand |
|---|
| 642 | to lessen the effect. |
|---|
| 643 | @param dist Distance beyond which the batches will not be rendered |
|---|
| 644 | (the default is 0, which means batches are always rendered). |
|---|
| 645 | */ |
|---|
| 646 | virtual void setRenderingDistance(Real dist) { |
|---|
| 647 | mUpperDistance = dist; |
|---|
| 648 | mSquaredUpperDistance = mUpperDistance * mUpperDistance; |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | /** Gets the distance at which batches are no longer rendered. */ |
|---|
| 652 | virtual Real getRenderingDistance(void) const { return mUpperDistance; } |
|---|
| 653 | |
|---|
| 654 | /** Gets the squared distance at which batches are no longer rendered. */ |
|---|
| 655 | virtual Real getSquaredRenderingDistance(void) const |
|---|
| 656 | { return mSquaredUpperDistance; } |
|---|
| 657 | |
|---|
| 658 | /** Hides or shows all the batches. */ |
|---|
| 659 | virtual void setVisible(bool visible); |
|---|
| 660 | |
|---|
| 661 | /** Are the batches visible? */ |
|---|
| 662 | virtual bool isVisible(void) const { return mVisible; } |
|---|
| 663 | |
|---|
| 664 | /** Sets whether this geometry should cast shadows. |
|---|
| 665 | @remarks |
|---|
| 666 | No matter what the settings on the original entities, |
|---|
| 667 | the StaticGeometry class defaults to not casting shadows. |
|---|
| 668 | This is because, being static, unless you have moving lights |
|---|
| 669 | you'd be better to use precalculated shadows of some sort. |
|---|
| 670 | However, if you need them, you can enable them using this |
|---|
| 671 | method. If the SceneManager is set up to use stencil shadows, |
|---|
| 672 | edge lists will be copied from the underlying meshes on build. |
|---|
| 673 | It is essential that all meshes support stencil shadows in this |
|---|
| 674 | case. |
|---|
| 675 | @note If you intend to use stencil shadows, you must set this to |
|---|
| 676 | true before calling 'build' as well as making sure you set the |
|---|
| 677 | scene's shadow type (that should always be the first thing you do |
|---|
| 678 | anyway). You can turn shadows off temporarily but they can never |
|---|
| 679 | be turned on if they were not at the time of the build. |
|---|
| 680 | */ |
|---|
| 681 | virtual void setCastShadows(bool castShadows); |
|---|
| 682 | /// Will the geometry from this object cast shadows? |
|---|
| 683 | virtual bool getCastShadows(void) { return mCastShadows; } |
|---|
| 684 | |
|---|
| 685 | /** Sets the size of a single region of geometry. |
|---|
| 686 | @remarks |
|---|
| 687 | This method allows you to configure the physical world size of |
|---|
| 688 | each region, so you can balance culling against batch size. Entities |
|---|
| 689 | will be fitted within the batch they most closely fit, and the |
|---|
| 690 | eventual bounds of each batch may well be slightly larger than this |
|---|
| 691 | if they overlap a little. The default is Vector3(1000, 1000, 1000). |
|---|
| 692 | @note Must be called before 'build'. |
|---|
| 693 | @param size Vector3 expressing the 3D size of each region. |
|---|
| 694 | */ |
|---|
| 695 | virtual void setRegionDimensions(const Vector3& size) { |
|---|
| 696 | mRegionDimensions = size; |
|---|
| 697 | mHalfRegionDimensions = size * 0.5; |
|---|
| 698 | } |
|---|
| 699 | /** Gets the size of a single batch of geometry. */ |
|---|
| 700 | virtual const Vector3& getRegionDimensions(void) const { return mRegionDimensions; } |
|---|
| 701 | /** Sets the origin of the geometry. |
|---|
| 702 | @remarks |
|---|
| 703 | This method allows you to configure the world centre of the geometry, |
|---|
| 704 | thus the place which all regions surround. You probably don't need |
|---|
| 705 | to mess with this unless you have a seriously large world, since the |
|---|
| 706 | default set up can handle an area 1024 * mRegionDimensions, and |
|---|
| 707 | the sparseness of population is no issue when it comes to rendering. |
|---|
| 708 | The default is Vector3(0,0,0). |
|---|
| 709 | @note Must be called before 'build'. |
|---|
| 710 | @param size Vector3 expressing the 3D origin of the geometry. |
|---|
| 711 | */ |
|---|
| 712 | virtual void setOrigin(const Vector3& origin) { mOrigin = origin; } |
|---|
| 713 | /** Gets the origin of this geometry. */ |
|---|
| 714 | virtual const Vector3& getOrigin(void) const { return mOrigin; } |
|---|
| 715 | |
|---|
| 716 | /** Sets the render queue group this object will be rendered through. |
|---|
| 717 | @remarks |
|---|
| 718 | Render queues are grouped to allow you to more tightly control the ordering |
|---|
| 719 | of rendered objects. If you do not call this method, all objects default |
|---|
| 720 | to the default queue (RenderQueue::getDefaultQueueGroup), which is fine for |
|---|
| 721 | most objects. You may want to alter this if you want to perform more complex |
|---|
| 722 | rendering. |
|---|
| 723 | @par |
|---|
| 724 | See RenderQueue for more details. |
|---|
| 725 | @param queueID Enumerated value of the queue group to use. |
|---|
| 726 | */ |
|---|
| 727 | virtual void setRenderQueueGroup(uint8 queueID); |
|---|
| 728 | |
|---|
| 729 | /** Gets the queue group for this entity, see setRenderQueueGroup for full details. */ |
|---|
| 730 | virtual uint8 getRenderQueueGroup(void) const; |
|---|
| 731 | |
|---|
| 732 | /// Iterator for iterating over contained regions |
|---|
| 733 | typedef MapIterator<RegionMap> RegionIterator; |
|---|
| 734 | /// Get an iterator over the regions in this geometry |
|---|
| 735 | RegionIterator getRegionIterator(void); |
|---|
| 736 | |
|---|
| 737 | /** Dump the contents of this StaticGeometry to a file for diagnostic |
|---|
| 738 | purposes. |
|---|
| 739 | */ |
|---|
| 740 | virtual void dump(const String& filename) const; |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | }; |
|---|
| 744 | |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | #endif |
|---|
| 748 | |
|---|