source: GTP/trunk/App/Demos/Geom/include/opt/OgreBspSceneManager.h @ 1030

Revision 1030, 11.2 KB checked in by gumbau, 18 years ago (diff)

Ogre Stuff initial import

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://ogre.sourceforge.net/
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 __BspSceneManager_H__
26#define __BspSceneManager_H__
27
28
29#include "OgreBspPrerequisites.h"
30#include "OgreSceneManager.h"
31#include "OgreStaticFaceGroup.h"
32#include "OgreRenderOperation.h"
33#include "OgreBspLevel.h"
34#include <set>
35
36
37namespace Ogre {
38
39
40    /** Specialisation of the SceneManager class to deal with indoor scenes
41        based on a BSP tree.
42        This class refines the behaviour of the default SceneManager to manage
43        a scene whose bulk of geometry is made up of an indoor environment which
44        is organised by a Binary Space Partition (BSP) tree. </p>
45        A BSP tree progressively subdivides the space using planes which are the nodes of the tree.
46        At some point we stop subdividing and everything in the remaining space is part of a 'leaf' which
47        contains a number of polygons. Typically we traverse the tree to locate the leaf in which a
48        point in space is (say the camera origin) and work from there. A second structure, the
49        Potentially Visible Set, tells us which other leaves can been seen from this
50        leaf, and we test their bounding boxes against the camera frustum to see which
51        we need to draw. Leaves are also a good place to start for collision detection since
52        they divide the level into discrete areas for testing.</p>
53        This BSP and PVS technique has been made famous by engines such as Quake and Unreal. Ogre
54        provides support for loading Quake3 level files to populate your world through this class,
55        by calling the BspSceneManager::setWorldGeometry. Note that this interface is made
56        available at the top level of the SceneManager class so you don't have to write your code
57        specifically for this class - just call Root::getSceneManager passing a SceneType of ST_INTERIOR
58        and in the current implementation you will get a BspSceneManager silently disguised as a
59        standard SceneManager.
60    */
61    class BspSceneManager : public SceneManager
62    {
63    protected:
64
65        // Pointer to resource manager just for singleton management
66        BspResourceManager* mBspResMgr;
67
68        // World geometry
69        BspLevelPtr mLevel;
70
71        // State variables for rendering WIP
72        // Set of face groups (by index) already included
73        typedef std::set<int> FaceGroupSet;
74        FaceGroupSet mFaceGroupSet;
75        // Material -> face group hashmap
76        typedef std::map<Material*, std::vector<StaticFaceGroup*>, materialLess > MaterialFaceGroupMap;
77        MaterialFaceGroupMap mMatFaceGroupMap;
78
79        RenderOperation mRenderOp;
80
81        // Debugging features
82        bool mShowNodeAABs;
83        RenderOperation mAABGeometry;
84
85        /** Walks the BSP tree looking for the node which the camera
86            is in, and tags any geometry which is in a visible leaf for
87            later processing.
88            @param camera Pointer to the viewpoint.
89            @returns The BSP node the camera was found in, for info.
90        */
91        BspNode* walkTree(Camera* camera, bool onlyShadowCasters);
92        /** Tags geometry in the leaf specified for later rendering. */
93        void processVisibleLeaf(BspNode* leaf, Camera* cam, bool onlyShadowCasters);
94
95        /** Caches a face group for imminent rendering. */
96        unsigned int cacheGeometry(unsigned int* pIndexes, const StaticFaceGroup* faceGroup);
97
98        /** Frees up allocated memory for geometry caches. */
99        void freeMemory(void);
100
101        /** Adds a bounding box to draw if turned on. */
102        void addBoundingBox(const AxisAlignedBox& aab, bool visible);
103
104        /** Renders the static level geometry tagged in walkTree. */
105        void renderStaticGeometry(void);
106
107                /** @copydoc SceneManager::clearScene */
108                void clearScene(void);
109
110
111        typedef std::set<const MovableObject*> MovablesForRendering;
112        MovablesForRendering mMovablesForRendering;
113
114    public:
115        BspSceneManager();
116        ~BspSceneManager();
117
118        /** Specialised from SceneManager to support Quake3 bsp files. */
119        void setWorldGeometry(const String& filename);
120
121        /** Specialised from SceneManager to support Quake3 bsp files. */
122        size_t estimateWorldGeometry(const String& filename);
123       
124        /** Tells the manager whether to draw the axis-aligned boxes that surround
125            nodes in the Bsp tree. For debugging purposes.
126        */
127        void showNodeBoxes(bool show);
128
129        /** Specialised to suggest viewpoints. */
130        ViewPoint getSuggestedViewpoint(bool random = false);
131
132        const BspLevelPtr& getLevel(void) {return mLevel; }
133
134        /** Overriden from SceneManager. */
135        void _findVisibleObjects(Camera* cam, bool onlyShadowCasters);
136
137        /** Overriden from SceneManager. */
138        void _renderVisibleObjects(void);
139
140        /** Creates a specialized BspSceneNode */
141        SceneNode * createSceneNode ( void );
142        /** Creates a specialized BspSceneNode */
143        SceneNode * createSceneNode ( const String &name );
144
145        /** Internal method for tagging BspNodes with objects which intersect them. */
146        void _notifyObjectMoved(const MovableObject* mov, const Vector3& pos);
147                /** Internal method for notifying the level that an object has been detached from a node */
148                void _notifyObjectDetached(const MovableObject* mov);
149
150        /** Creates an AxisAlignedBoxSceneQuery for this scene manager.
151        @remarks
152            This method creates a new instance of a query object for this scene manager,
153            for an axis aligned box region. See SceneQuery and AxisAlignedBoxSceneQuery
154            for full details.
155        @par
156            The instance returned from this method must be destroyed by calling
157            SceneManager::destroyQuery when it is no longer required.
158        @param box Details of the box which describes the region for this query.
159        @param mask The query mask to apply to this query; can be used to filter out
160            certain objects; see SceneQuery for details.
161        */
162        /*
163        virtual AxisAlignedBoxSceneQuery*
164            createAABBQuery(const AxisAlignedBox& box, unsigned long mask = 0xFFFFFFFF);
165        */
166        /** Creates a SphereSceneQuery for this scene manager.
167        @remarks
168            This method creates a new instance of a query object for this scene manager,
169            for a spherical region. See SceneQuery and SphereSceneQuery
170            for full details.
171        @par
172            The instance returned from this method must be destroyed by calling
173            SceneManager::destroyQuery when it is no longer required.
174        @param sphere Details of the sphere which describes the region for this query.
175        @param mask The query mask to apply to this query; can be used to filter out
176            certain objects; see SceneQuery for details.
177        */
178        /*
179        virtual SphereSceneQuery*
180            createSphereQuery(const Sphere& sphere, unsigned long mask = 0xFFFFFFFF);
181        */
182        /** Creates a RaySceneQuery for this scene manager.
183        @remarks
184            This method creates a new instance of a query object for this scene manager,
185            looking for objects which fall along a ray. See SceneQuery and RaySceneQuery
186            for full details.
187        @par
188            The instance returned from this method must be destroyed by calling
189            SceneManager::destroyQuery when it is no longer required.
190        @param ray Details of the ray which describes the region for this query.
191        @param mask The query mask to apply to this query; can be used to filter out
192            certain objects; see SceneQuery for details.
193        */
194        virtual RaySceneQuery*
195            createRayQuery(const Ray& ray, unsigned long mask = 0xFFFFFFFF);
196        /** Creates an IntersectionSceneQuery for this scene manager.
197        @remarks
198            This method creates a new instance of a query object for locating
199            intersecting objects. See SceneQuery and IntersectionSceneQuery
200            for full details.
201        @par
202            The instance returned from this method must be destroyed by calling
203            SceneManager::destroyQuery when it is no longer required.
204        @param mask The query mask to apply to this query; can be used to filter out
205            certain objects; see SceneQuery for details.
206        */
207        virtual IntersectionSceneQuery*
208            createIntersectionQuery(unsigned long mask = 0xFFFFFFFF);
209
210    };
211
212    /** BSP specialisation of IntersectionSceneQuery */
213    class BspIntersectionSceneQuery : public DefaultIntersectionSceneQuery
214    {
215    public:
216        BspIntersectionSceneQuery(SceneManager* creator);
217
218        /** See IntersectionSceneQuery. */
219        void execute(IntersectionSceneQueryListener* listener);
220
221    };
222
223    /** BSP specialisation of RaySceneQuery */
224    class BspRaySceneQuery : public DefaultRaySceneQuery
225    {
226    public:
227        BspRaySceneQuery(SceneManager* creator);
228        ~BspRaySceneQuery();
229
230        /** See RaySceneQuery. */
231        void execute(RaySceneQueryListener* listener);
232    protected:
233        /// Set for eliminating duplicates since objects can be in > 1 node
234        std::set<MovableObject*> mObjsThisQuery;
235        /// list of the last single intersection world fragments (derived)
236        std::vector<SceneQuery::WorldFragment*> mSingleIntersections;
237
238        void clearTemporaries(void);
239        /** Internal processing of a single node.
240        @returns true if we should continue tracing, false otherwise
241        */
242        bool processNode(const BspNode* node, const Ray& tracingRay, RaySceneQueryListener* listener,
243            Real maxDistance = Math::POS_INFINITY, Real traceDistance = 0.0f);
244        /** Internal processing of a single leaf.
245        @returns true if we should continue tracing, false otherwise
246        */
247        bool processLeaf(const BspNode* node, const Ray& tracingRay, RaySceneQueryListener* listener,
248            Real maxDistance = Math::POS_INFINITY, Real traceDistance = 0.0f);
249
250    };
251}
252
253#endif
Note: See TracBrowser for help on using the repository browser.