Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogre_src_v1-9-0/OgreMain/include/OgreSceneQuery.h @ 148

Last change on this file since 148 was 148, checked in by patricwi, 6 years ago

Added new dependencies for ogre1.9 and cegui0.8

File size: 25.3 KB
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-2013 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26-----------------------------------------------------------------------------
27*/
28#ifndef __SceneQuery_H__
29#define __SceneQuery_H__
30
31#include "OgrePrerequisites.h"
32#include "OgreAxisAlignedBox.h"
33#include "OgreSphere.h"
34#include "OgreRay.h"
35#include "OgreRenderOperation.h"
36#include "OgrePlaneBoundedVolume.h"
37#include "OgreHeaderPrefix.h"
38
39namespace Ogre {
40
41    // forward declaration
42    class SceneQueryListener;
43        /** \addtogroup Core
44        *  @{
45        */
46        /** \addtogroup Scene
47        *  @{
48        */
49        /** A class for performing queries on a scene.
50    @remarks
51        This is an abstract class for performing a query on a scene, i.e. to retrieve
52        a list of objects and/or world geometry sections which are potentially intersecting a
53        given region. Note the use of the word 'potentially': the results of a scene query
54        are generated based on bounding volumes, and as such are not correct at a triangle
55        level; the user of the SceneQuery is expected to filter the results further if
56        greater accuracy is required.
57    @par
58        Different SceneManagers will implement these queries in different ways to
59        exploit their particular scene organisation, and thus will provide their own
60        concrete subclasses. In fact, these subclasses will be derived from subclasses
61        of this class rather than directly because there will be region-type classes
62        in between.
63    @par
64        These queries could have just been implemented as methods on the SceneManager,
65        however, they are wrapped up as objects to allow 'compilation' of queries
66        if deemed appropriate by the implementation; i.e. each concrete subclass may
67        precalculate information (such as fixed scene partitions involved in the query)
68        to speed up the repeated use of the query.
69    @par
70        You should never try to create a SceneQuery object yourself, they should be created
71        using the SceneManager interfaces for the type of query required, e.g.
72        SceneManager::createSphereSceneQuery.
73    */
74        class _OgreExport SceneQuery : public SceneMgtAlloc
75    {
76    public:
77        /** This type can be used by collaborating applications & SceneManagers to
78            agree on the type of world geometry to be returned from queries. Not all
79            these types will be supported by all SceneManagers; once the application
80            has decided which SceneManager specialisation to use, it is expected that
81            it will know which type of world geometry abstraction is available to it.
82        */
83        enum WorldFragmentType {
84            /// Return no world geometry hits at all
85            WFT_NONE,
86            /// Return pointers to convex plane-bounded regions
87            WFT_PLANE_BOUNDED_REGION,
88            /// Return a single intersection point (typically RaySceneQuery only)
89            WFT_SINGLE_INTERSECTION,
90            /// Custom geometry as defined by the SceneManager
91            WFT_CUSTOM_GEOMETRY,
92            /// General RenderOperation structure
93            WFT_RENDER_OPERATION
94        };
95
96        /** Represents part of the world geometry that is a result of a SceneQuery.
97        @remarks
98            Since world geometry is normally vast and sprawling, we need a way of
99            retrieving parts of it based on a query. That is what this struct is for;
100            note there are potentially as many data structures for world geometry as there
101            are SceneManagers, however this structure includes a few common abstractions as
102            well as a more general format.
103        @par
104            The type of world fragment that is returned from a query depends on the
105            SceneManager, and the option set using SceneQuery::setWorldFragmentType.
106            You can see what fragment types are supported on the query in question by
107            calling SceneQuery::getSupportedWorldFragmentTypes().
108        */
109        struct WorldFragment {
110            /// The type of this world fragment
111            WorldFragmentType fragmentType;
112            /// Single intersection point, only applicable for WFT_SINGLE_INTERSECTION
113            Vector3 singleIntersection;
114            /// Planes bounding a convex region, only applicable for WFT_PLANE_BOUNDED_REGION
115            list<Plane>::type* planes;
116            /// Custom geometry block, only applicable for WFT_CUSTOM_GEOMETRY
117            void* geometry;
118            /// General render operation structure, fallback if nothing else is available
119            RenderOperation* renderOp;
120           
121        };
122    protected:
123        SceneManager* mParentSceneMgr;
124        uint32 mQueryMask;
125                uint32 mQueryTypeMask;
126        set<WorldFragmentType>::type mSupportedWorldFragments;
127        WorldFragmentType mWorldFragmentType;
128   
129    public:
130        /** Standard constructor, should be called by SceneManager. */
131        SceneQuery(SceneManager* mgr);
132        virtual ~SceneQuery();
133
134        /** Sets the mask for results of this query.
135        @remarks
136            This method allows you to set a 'mask' to limit the results of this
137            query to certain types of result. The actual meaning of this value is
138            up to the application; basically MovableObject instances will only be returned
139            from this query if a bitwise AND operation between this mask value and the
140            MovableObject::getQueryFlags value is non-zero. The application will
141            have to decide what each of the bits means.
142        */
143        virtual void setQueryMask(uint32 mask);
144        /** Returns the current mask for this query. */
145        virtual uint32 getQueryMask(void) const;
146
147        /** Sets the type mask for results of this query.
148        @remarks
149            This method allows you to set a 'type mask' to limit the results of this
150            query to certain types of objects. Whilst setQueryMask deals with flags
151                        set per instance of object, this method deals with setting a mask on
152                        flags set per type of object. Both may exclude an object from query
153                        results.
154        */
155        virtual void setQueryTypeMask(uint32 mask);
156        /** Returns the current mask for this query. */
157        virtual uint32 getQueryTypeMask(void) const;
158
159                /** Tells the query what kind of world geometry to return from queries;
160            often the full renderable geometry is not what is needed.
161        @remarks
162            The application receiving the world geometry is expected to know
163            what to do with it; inevitably this means that the application must
164            have knowledge of at least some of the structures
165            used by the custom SceneManager.
166        @par
167            The default setting is WFT_NONE.
168        */
169        virtual void setWorldFragmentType(enum WorldFragmentType wft);
170
171        /** Gets the current world fragment types to be returned from the query. */
172        virtual WorldFragmentType getWorldFragmentType(void) const;
173
174        /** Returns the types of world fragments this query supports. */
175        virtual const set<WorldFragmentType>::type* getSupportedWorldFragmentTypes(void) const
176            {return &mSupportedWorldFragments;}
177
178       
179    };
180
181    /** This optional class allows you to receive per-result callbacks from
182        SceneQuery executions instead of a single set of consolidated results.
183    @remarks
184        You should override this with your own subclass. Note that certain query
185        classes may refine this listener interface.
186    */
187    class _OgreExport SceneQueryListener
188    {
189    public:
190        virtual ~SceneQueryListener() { }
191        /** Called when a MovableObject is returned by a query.
192        @remarks
193            The implementor should return 'true' to continue returning objects,
194            or 'false' to abandon any further results from this query.
195        */
196        virtual bool queryResult(MovableObject* object) = 0;
197        /** Called when a WorldFragment is returned by a query.
198        @remarks
199            The implementor should return 'true' to continue returning objects,
200            or 'false' to abandon any further results from this query.
201        */
202        virtual bool queryResult(SceneQuery::WorldFragment* fragment) = 0;
203
204    };
205
206    typedef list<MovableObject*>::type SceneQueryResultMovableList;
207    typedef list<SceneQuery::WorldFragment*>::type SceneQueryResultWorldFragmentList;
208    /** Holds the results of a scene query. */
209        struct _OgreExport SceneQueryResult : public SceneMgtAlloc
210    {
211        /// List of movable objects in the query (entities, particle systems etc)
212        SceneQueryResultMovableList movables;
213        /// List of world fragments
214                SceneQueryResultWorldFragmentList worldFragments;
215    };
216
217    /** Abstract class defining a query which returns single results from a region.
218    @remarks
219        This class is simply a generalisation of the subtypes of query that return
220        a set of individual results in a region. See the SceneQuery class for abstract
221        information, and subclasses for the detail of each query type.
222    */
223    class _OgreExport RegionSceneQuery
224        : public SceneQuery, public SceneQueryListener
225    {
226    protected:
227        SceneQueryResult* mLastResult;
228    public:
229        /** Standard constructor, should be called by SceneManager. */
230        RegionSceneQuery(SceneManager* mgr);
231        virtual ~RegionSceneQuery();
232        /** Executes the query, returning the results back in one list.
233        @remarks
234            This method executes the scene query as configured, gathers the results
235            into one structure and returns a reference to that structure. These
236            results will also persist in this query object until the next query is
237            executed, or clearResults() is called. An more lightweight version of
238            this method that returns results through a listener is also available.
239        */
240        virtual SceneQueryResult& execute(void);
241
242        /** Executes the query and returns each match through a listener interface.
243        @remarks
244            Note that this method does not store the results of the query internally
245            so does not update the 'last result' value. This means that this version of
246            execute is more lightweight and therefore more efficient than the version
247            which returns the results as a collection.
248        */
249        virtual void execute(SceneQueryListener* listener) = 0;
250       
251        /** Gets the results of the last query that was run using this object, provided
252            the query was executed using the collection-returning version of execute.
253        */
254        virtual SceneQueryResult& getLastResults(void) const;
255        /** Clears the results of the last query execution.
256        @remarks
257            You only need to call this if you specifically want to free up the memory
258            used by this object to hold the last query results. This object clears the
259            results itself when executing and when destroying itself.
260        */
261        virtual void clearResults(void);
262
263        /** Self-callback in order to deal with execute which returns collection. */
264        bool queryResult(MovableObject* first);
265        /** Self-callback in order to deal with execute which returns collection. */
266        bool queryResult(SceneQuery::WorldFragment* fragment);
267    };
268
269    /** Specialises the SceneQuery class for querying within an axis aligned box. */
270    class _OgreExport AxisAlignedBoxSceneQuery : public RegionSceneQuery
271    {
272    protected:
273        AxisAlignedBox mAABB;
274    public:
275        AxisAlignedBoxSceneQuery(SceneManager* mgr);
276        virtual ~AxisAlignedBoxSceneQuery();
277
278        /** Sets the size of the box you wish to query. */
279        void setBox(const AxisAlignedBox& box);
280
281        /** Gets the box which is being used for this query. */
282        const AxisAlignedBox& getBox(void) const;
283
284    };
285
286    /** Specialises the SceneQuery class for querying within a sphere. */
287    class _OgreExport SphereSceneQuery : public RegionSceneQuery
288    {
289    protected:
290        Sphere mSphere;
291    public:
292        SphereSceneQuery(SceneManager* mgr);
293        virtual ~SphereSceneQuery();
294        /** Sets the sphere which is to be used for this query. */
295        void setSphere(const Sphere& sphere);
296
297        /** Gets the sphere which is being used for this query. */
298        const Sphere& getSphere() const;
299
300    };
301
302    /** Specialises the SceneQuery class for querying within a plane-bounded volume.
303    */
304    class _OgreExport PlaneBoundedVolumeListSceneQuery : public RegionSceneQuery
305    {
306    protected:
307        PlaneBoundedVolumeList mVolumes;
308    public:
309        PlaneBoundedVolumeListSceneQuery(SceneManager* mgr);
310        virtual ~PlaneBoundedVolumeListSceneQuery();
311        /** Sets the volume which is to be used for this query. */
312        void setVolumes(const PlaneBoundedVolumeList& volumes);
313
314        /** Gets the volume which is being used for this query. */
315        const PlaneBoundedVolumeList& getVolumes() const;
316
317    };
318
319
320    /*
321    /// Specialises the SceneQuery class for querying within a pyramid.
322    class _OgreExport PyramidSceneQuery : public RegionSceneQuery
323    {
324    public:
325        PyramidSceneQuery(SceneManager* mgr);
326        virtual ~PyramidSceneQuery();
327    };
328    */
329
330    /** Alternative listener class for dealing with RaySceneQuery.
331    @remarks
332        Because the RaySceneQuery returns results in an extra bit of information, namely
333        distance, the listener interface must be customised from the standard SceneQueryListener.
334    */
335    class _OgreExport RaySceneQueryListener
336    {
337    public:
338        virtual ~RaySceneQueryListener() { }
339        /** Called when a movable objects intersects the ray.
340        @remarks
341            As with SceneQueryListener, the implementor of this method should return 'true'
342            if further results are required, or 'false' to abandon any further results from
343            the current query.
344        */
345        virtual bool queryResult(MovableObject* obj, Real distance) = 0;
346
347        /** Called when a world fragment is intersected by the ray.
348        @remarks
349            As with SceneQueryListener, the implementor of this method should return 'true'
350            if further results are required, or 'false' to abandon any further results from
351            the current query.
352        */
353        virtual bool queryResult(SceneQuery::WorldFragment* fragment, Real distance) = 0;
354
355    };
356     
357    /** This struct allows a single comparison of result data no matter what the type */
358    struct _OgreExport RaySceneQueryResultEntry
359    {
360        /// Distance along the ray
361        Real distance;
362        /// The movable, or NULL if this is not a movable result
363        MovableObject* movable;
364        /// The world fragment, or NULL if this is not a fragment result
365        SceneQuery::WorldFragment* worldFragment;
366        /// Comparison operator for sorting
367        bool operator < (const RaySceneQueryResultEntry& rhs) const
368        {
369            return this->distance < rhs.distance;
370        }
371
372    };
373    typedef vector<RaySceneQueryResultEntry>::type RaySceneQueryResult;
374
375    /** Specialises the SceneQuery class for querying along a ray. */
376    class _OgreExport RaySceneQuery : public SceneQuery, public RaySceneQueryListener
377    {
378    protected:
379        Ray mRay;
380        bool mSortByDistance;
381        ushort mMaxResults;
382        RaySceneQueryResult mResult;
383
384    public:
385        RaySceneQuery(SceneManager* mgr);
386        virtual ~RaySceneQuery();
387        /** Sets the ray which is to be used for this query. */
388        virtual void setRay(const Ray& ray);
389        /** Gets the ray which is to be used for this query. */
390        virtual const Ray& getRay(void) const;
391        /** Sets whether the results of this query will be sorted by distance along the ray.
392        @remarks
393            Often you want to know what was the first object a ray intersected with, and this
394            method allows you to ask the query to sort the results so that the nearest results
395            are listed first.
396        @par
397            Note that because the query returns results based on bounding volumes, the ray may not
398            actually intersect the detail of the objects returned from the query, just their
399            bounding volumes. For this reason the caller is advised to use more detailed
400            intersection tests on the results if a more accurate result is required; OGRE uses
401            bounds checking in order to give the most speedy results since not all applications
402            need extreme accuracy.
403        @param sort If true, results will be sorted.
404        @param maxresults If sorting is enabled, this value can be used to constrain the maximum number
405            of results that are returned. Please note (as above) that the use of bounding volumes mean that
406            accuracy is not guaranteed; if in doubt, allow more results and filter them in more detail.
407            0 means unlimited results.
408        */
409        virtual void setSortByDistance(bool sort, ushort maxresults = 0);
410        /** Gets whether the results are sorted by distance. */
411        virtual bool getSortByDistance(void) const;
412        /** Gets the maximum number of results returned from the query (only relevant if
413        results are being sorted) */
414        virtual ushort getMaxResults(void) const;
415        /** Executes the query, returning the results back in one list.
416        @remarks
417            This method executes the scene query as configured, gathers the results
418            into one structure and returns a reference to that structure. These
419            results will also persist in this query object until the next query is
420            executed, or clearResults() is called. An more lightweight version of
421            this method that returns results through a listener is also available.
422        */
423        virtual RaySceneQueryResult& execute(void);
424
425        /** Executes the query and returns each match through a listener interface.
426        @remarks
427            Note that this method does not store the results of the query internally
428            so does not update the 'last result' value. This means that this version of
429            execute is more lightweight and therefore more efficient than the version
430            which returns the results as a collection.
431        */
432        virtual void execute(RaySceneQueryListener* listener) = 0;
433
434        /** Gets the results of the last query that was run using this object, provided
435            the query was executed using the collection-returning version of execute.
436        */
437        virtual RaySceneQueryResult& getLastResults(void);
438        /** Clears the results of the last query execution.
439        @remarks
440            You only need to call this if you specifically want to free up the memory
441            used by this object to hold the last query results. This object clears the
442            results itself when executing and when destroying itself.
443        */
444        virtual void clearResults(void);
445
446        /** Self-callback in order to deal with execute which returns collection. */
447        bool queryResult(MovableObject* obj, Real distance);
448        /** Self-callback in order to deal with execute which returns collection. */
449        bool queryResult(SceneQuery::WorldFragment* fragment, Real distance);
450
451
452
453
454    };
455
456    /** Alternative listener class for dealing with IntersectionSceneQuery.
457    @remarks
458        Because the IntersectionSceneQuery returns results in pairs, rather than singularly,
459        the listener interface must be customised from the standard SceneQueryListener.
460    */
461    class _OgreExport IntersectionSceneQueryListener
462    {
463    public:
464        virtual ~IntersectionSceneQueryListener() { }
465        /** Called when 2 movable objects intersect one another.
466        @remarks
467            As with SceneQueryListener, the implementor of this method should return 'true'
468            if further results are required, or 'false' to abandon any further results from
469            the current query.
470        */
471        virtual bool queryResult(MovableObject* first, MovableObject* second) = 0;
472
473        /** Called when a movable intersects a world fragment.
474        @remarks
475            As with SceneQueryListener, the implementor of this method should return 'true'
476            if further results are required, or 'false' to abandon any further results from
477            the current query.
478        */
479        virtual bool queryResult(MovableObject* movable, SceneQuery::WorldFragment* fragment) = 0;
480
481        /* NB there are no results for world fragments intersecting other world fragments;
482           it is assumed that world geometry is either static or at least that self-intersections
483           are irrelevant or dealt with elsewhere (such as the custom scene manager) */
484       
485   
486    };
487       
488    typedef std::pair<MovableObject*, MovableObject*> SceneQueryMovableObjectPair;
489    typedef std::pair<MovableObject*, SceneQuery::WorldFragment*> SceneQueryMovableObjectWorldFragmentPair;
490    typedef list<SceneQueryMovableObjectPair>::type SceneQueryMovableIntersectionList;
491    typedef list<SceneQueryMovableObjectWorldFragmentPair>::type SceneQueryMovableWorldFragmentIntersectionList;
492    /** Holds the results of an intersection scene query (pair values). */
493        struct _OgreExport IntersectionSceneQueryResult : public SceneMgtAlloc
494    {
495        /// List of movable / movable intersections (entities, particle systems etc)
496        SceneQueryMovableIntersectionList movables2movables;
497        /// List of movable / world intersections
498        SceneQueryMovableWorldFragmentIntersectionList movables2world;
499       
500       
501
502    };
503
504    /** Separate SceneQuery class to query for pairs of objects which are
505        possibly intersecting one another.
506    @remarks
507        This SceneQuery subclass considers the whole world and returns pairs of objects
508        which are close enough to each other that they may be intersecting. Because of
509        this slightly different focus, the return types and listener interface are
510        different for this class.
511    */
512    class _OgreExport IntersectionSceneQuery
513        : public SceneQuery, public IntersectionSceneQueryListener
514    {
515    protected:
516        IntersectionSceneQueryResult* mLastResult;
517    public:
518        IntersectionSceneQuery(SceneManager* mgr);
519        virtual ~IntersectionSceneQuery();
520
521        /** Executes the query, returning the results back in one list.
522        @remarks
523            This method executes the scene query as configured, gathers the results
524            into one structure and returns a reference to that structure. These
525            results will also persist in this query object until the next query is
526            executed, or clearResults() is called. An more lightweight version of
527            this method that returns results through a listener is also available.
528        */
529        virtual IntersectionSceneQueryResult& execute(void);
530
531        /** Executes the query and returns each match through a listener interface.
532        @remarks
533            Note that this method does not store the results of the query internally
534            so does not update the 'last result' value. This means that this version of
535            execute is more lightweight and therefore more efficient than the version
536            which returns the results as a collection.
537        */
538        virtual void execute(IntersectionSceneQueryListener* listener) = 0;
539
540        /** Gets the results of the last query that was run using this object, provided
541            the query was executed using the collection-returning version of execute.
542        */
543        virtual IntersectionSceneQueryResult& getLastResults(void) const;
544        /** Clears the results of the last query execution.
545        @remarks
546            You only need to call this if you specifically want to free up the memory
547            used by this object to hold the last query results. This object clears the
548            results itself when executing and when destroying itself.
549        */
550        virtual void clearResults(void);
551
552        /** Self-callback in order to deal with execute which returns collection. */
553        bool queryResult(MovableObject* first, MovableObject* second);
554        /** Self-callback in order to deal with execute which returns collection. */
555        bool queryResult(MovableObject* movable, SceneQuery::WorldFragment* fragment);
556    };
557   
558        /** @} */
559        /** @} */
560
561}
562   
563#include "OgreHeaderSuffix.h"
564
565#endif
Note: See TracBrowser for help on using the repository browser.