source: GTP/trunk/App/Demos/Geom/OgreStuff/include/opt/OgreBspLevel.h @ 1092

Revision 1092, 11.1 KB checked in by gumbau, 18 years ago (diff)

LodStrips? and LODTrees demos

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#ifndef _BspLevel_H__
26#define _BspLevel_H__
27
28#include "OgreBspPrerequisites.h"
29#include "OgreResource.h"
30#include "OgreStaticFaceGroup.h"
31#include "OgreSceneManager.h"
32#include "OgreBspNode.h"
33#include "OgreHardwareBufferManager.h"
34#include "OgreDefaultHardwareBufferManager.h"
35#include "OgreQuake3Level.h"
36
37
38namespace Ogre {
39
40    /** Holds all the data associated with a Binary Space Parition
41        (BSP) based indoor level.
42        The data used here is populated by loading level files via
43        the BspLevelManager::load method, although application users
44        are more likely to call SceneManager::setWorldGeometry which will
45        automatically arrange the loading of the level. Note that this assumes
46        that you have asked for an indoor-specialised SceneManager (specify
47        ST_INDOOR when calling Root::getSceneManager).</p>
48        Ogre currently only supports loading from Quake3 Arena level files,
49        although any source that can be converted into this classes structure
50        could also be used. The Quake3 level load process is in a different
51        class called Quake3Level to keep the specifics separate.</p>
52    */
53    class BspLevel : public Resource
54    {
55        friend class BspSceneManager;
56    public:
57        /** Default constructor - used by BspResourceManager (do not call directly) */
58        BspLevel(ResourceManager* creator, const String& name, ResourceHandle handle,
59            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
60        ~BspLevel();
61
62        /** Determines if one leaf node is visible from another. */
63        bool isLeafVisible(const BspNode* from, const BspNode* to) const;
64
65        /** Returns a pointer to the root node (BspNode) of the BSP tree. */
66        const BspNode* getRootNode(void);
67
68        /** Walks the entire BSP tree and returns the leaf
69            which contains the given point.
70        */
71        BspNode* findLeaf(const Vector3& point) const;
72
73        /** Ensures that the MovableObject is attached to the right leaves of the
74            BSP tree.
75        */
76        void _notifyObjectMoved(const MovableObject* mov,
77            const Vector3& pos);
78                /** Internal method, makes sure an object is removed from the leaves when detached from a node. */
79                void _notifyObjectDetached(const MovableObject* mov);
80        /** Gets a pointer to the start of the leaf nodes. */
81        BspNode* getLeafStart(void) {return &mRootNode[mLeafStart]; }
82        /** Gets the number of leaf nodes */
83        int getNumLeaves(void) const { return mNumLeaves; }
84
85        /** Calculate the number of loading stages required for a given level */
86        static size_t calculateLoadingStages(const String& levelName);
87
88        /** Utility class just to enable queueing of patches */
89    protected:
90        /** @copydoc Resource::loadImpl. */
91        void loadImpl(void);
92        /** @copydoc Resource::unloadImpl. */
93        void unloadImpl(void);
94        /** @copydoc Resource::calculateSize. */
95        size_t calculateSize(void) const;
96        /** Pointer to the root node of the BSP tree;
97            This pointer actually has a dual purpose; to avoid allocating lots of small chunks of
98            memory, the BspLevel actually allocates all nodes required through this pointer. So this
99            pointer is the handle for the allocation of memory for all nodes. It also happens to point
100            to the root node, since the first one in the memory chunk is the root node.
101        */
102        BspNode* mRootNode;
103        int mNumNodes;
104        int mNumLeaves;
105        int mLeafStart; // the index at which leaf nodes begin
106
107        /** Vertex format for fixed geometry.
108            Note that in this case vertex components (position, normal, texture coords etc)
109            are held interleaved in the same buffer. However, the format here is different from
110            the format used by Quake because older Direct3d drivers like the vertex elements
111            to be in a particular order within the buffer. See VertexDeclaration for full
112            details of this marvellous(not) feature.
113        */
114        struct BspVertex
115        {
116            float position[3];
117            float normal[3];
118            int colour;
119            float texcoords[2];
120            float lightmap[2];
121        };
122        /*
123        /// Array of vertices for whole level.
124        BspVertex* mVertices;
125        int mNumVertices;
126        */
127        /// Vertex data holding all the data for the level, but able to render parts of it
128        VertexData* mVertexData;
129
130        /** Array of indexes into the mFaceGroups array. This buffer is organised
131            by leaf node so leaves can just use contiguous chunks of it and
132            get repointed to the actual entries in mFaceGroups. */
133        int* mLeafFaceGroups;
134        int mNumLeafFaceGroups;
135
136        /** Array of face groups, indexed into by contents of mLeafFaceGroups. */
137        StaticFaceGroup* mFaceGroups;
138        int mNumFaceGroups;
139
140
141        /*
142        /// Array of elements i.e. vertex indexes as used by face groups.
143        int* mElements;
144        int mNumElements;
145        */
146
147        /// indexes for the whole level, will be copied to the real indexdata per frame
148        size_t mNumIndexes;
149        // system-memory buffer
150        HardwareIndexBufferSharedPtr mIndexes;
151
152        /// Brushes as used for collision, main memory is here
153        BspNode::Brush *mBrushes;
154
155        /** Vector of player start points */
156        std::vector<ViewPoint> mPlayerStarts;
157
158        /** Internal utility function for loading data from Quake3. */
159        void loadQuake3Level(const Quake3Level& q3lvl);
160        /** Internal lookup table to determine visibility between leaves.
161            Leaf nodes are assigned to 'clusters' of nodes, which are used to group nodes together for
162            visibility testing. This data holds a lookup table which is used to determine if one cluster of leaves
163            is visible from another cluster. Whilst it would be possible to expand all this out so that
164            each node had a list of pointers to other visible nodes, this would be very expensive in terms
165            of storage (using the cluster method there is a table which is 1-bit squared per cluster, rounded
166            up to the nearest byte obviously, which uses far less space than 4-bytes per linked node per source
167            node). Of course the limitation here is that you have to each leaf in turn to determine if it is visible
168            rather than just following a list, but since this is only done once per frame this is not such a big
169            overhead.</p>
170            Each row in the table is a 'from' cluster, with each bit in the row corresponding to a 'to' cluster,
171            both ordered based on cluster index. A 0 in the bit indicates the 'to' cluster is not visible from the
172            'from' cluster, whilst a 1 indicates it is.</p>
173            As many will notice, this is lifted directly from the Quake implementation of PVS.
174        */
175        struct VisData
176        {
177            unsigned char *tableData;
178            int numClusters;            // Number of clusters, therefore number of rows
179            int rowLength;                // Length in bytes of each row (num clusters / 8 rounded up)
180        };
181
182        VisData mVisData;
183
184
185        /** Internal method for parsing chosen entities. */
186        void loadEntities(const Quake3Level& q3lvl);
187
188        typedef std::map<const MovableObject*, std::list<BspNode*> > MovableToNodeMap;
189        /// Map for locating the nodes a movable is currently a member of
190        MovableToNodeMap mMovableToNodeMap;
191
192        void tagNodesWithMovable(BspNode* node, const MovableObject* mov, const Vector3& pos);
193
194        // Storage of patches
195        typedef std::map<int, PatchSurface*> PatchMap;
196        PatchMap mPatches;
197        // Total number of vertices required for all patches
198        size_t mPatchVertexCount;
199        // Total number of indexes required for all patches
200        size_t mPatchIndexCount;
201
202        void initQuake3Patches(const Quake3Level & q3lvl, VertexDeclaration* decl);
203        void buildQuake3Patches(size_t vertOffset, size_t indexOffset);
204
205        void quakeVertexToBspVertex(const bsp_vertex_t* src, BspVertex* dest);
206
207
208    };
209    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to BspLevelPtr
210    @note Has to be a subclass since we need operator=.
211    We could templatise this instead of repeating per Resource subclass,
212    except to do so requires a form VC6 does not support i.e.
213    ResourceSubclassPtr<T> : public SharedPtr<T>
214    */
215    class BspLevelPtr : public SharedPtr<BspLevel>
216    {
217    public:
218        BspLevelPtr() : SharedPtr<BspLevel>() {}
219        explicit BspLevelPtr(BspLevel* rep) : SharedPtr<BspLevel>(rep) {}
220        BspLevelPtr(const BspLevelPtr& r) : SharedPtr<BspLevel>(r) {}
221        BspLevelPtr(const ResourcePtr& r) : SharedPtr<BspLevel>()
222        {
223                        // lock & copy other mutex pointer
224                        OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
225                        OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
226            pRep = static_cast<BspLevel*>(r.getPointer());
227            pUseCount = r.useCountPointer();
228            if (pUseCount)
229            {
230                ++(*pUseCount);
231            }
232        }
233
234        /// Operator used to convert a ResourcePtr to a BspLevelPtr
235        BspLevelPtr& operator=(const ResourcePtr& r)
236        {
237            if (pRep == static_cast<BspLevel*>(r.getPointer()))
238                return *this;
239            release();
240                        // lock & copy other mutex pointer
241                        OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
242                        OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
243            pRep = static_cast<BspLevel*>(r.getPointer());
244            pUseCount = r.useCountPointer();
245            if (pUseCount)
246            {
247                ++(*pUseCount);
248            }
249            return *this;
250        }
251    };
252
253}
254
255#endif
Note: See TracBrowser for help on using the repository browser.