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