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