Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/ogreode/OgreOdeGeometry.h @ 1923

Last change on this file since 1923 was 1923, checked in by rgrieder, 16 years ago

Cleaned up the heavy mess with header file includes in OgreOde. It should now compile a lot faster.

  • Property svn:eol-style set to native
File size: 21.9 KB
Line 
1#ifndef _OGREODEGEOMETRY_H_
2#define _OGREODEGEOMETRY_H_
3
4#include "OgreOdePreReqs.h"
5
6#include <OgreUserDefinedObject.h>
7#include <OgreMovableObject.h>
8#include <OgreSceneQuery.h>
9#include "OgreOdeTriangleMeshData.h"
10
11namespace OgreOde
12{
13    /**
14    This object is the base class for all Geometry objects in OgreOde.
15    @remarks
16        This object is tied either to a Body (acting as collision proxy for a physic object)
17        or to a Entity Object (which then is physically "static")
18    @remarks
19        It extends the OGRE UserDefinedObject to allow reverse links from Ogre::Entity.
20        Note that this class does not override the UserDefinedObject's getTypeId method
21        because this class is abstract.
22    */ 
23    class _OgreOdeExport Geometry : public Ogre::UserDefinedObject, public Ogre::MovableObject
24        {
25                friend class Space;
26        friend class World;
27        friend class Body;
28                friend class TransformGeometry;
29
30        public:
31                enum Class
32                {
33                        Class_Sphere            = dSphereClass,
34                        Class_Box                       = dBoxClass,
35                        Class_Capsule           = dCapsuleClass,
36                        Class_Cylinder          = dCCylinderClass,
37                        Class_InfinitePlane = dPlaneClass,
38                        Class_Ray                       = dRayClass,
39                        Class_TriangleMesh      = dTriMeshClass,
40                        Class_Terrain           = dHeightfieldClass,
41                        Class_Convex            = dConvexClass,
42                        Class_Transform         = dGeomTransformClass,
43                        Class_NoGeometry
44                };
45
46        public:
47        Geometry(World *world,  Space* space = 0);
48                virtual ~Geometry();
49
50                virtual void                        setBody(Body* body);
51                Body*                               getBody(); 
52
53
54                virtual void                        setPosition(const Ogre::Vector3& position);
55                virtual void                        setOrientation(const Ogre::Quaternion& orientation);       
56                virtual const Ogre::Vector3&        getPosition();
57                virtual const Ogre::Quaternion&     getOrientation();
58                virtual const Ogre::AxisAlignedBox& getAxisAlignedBox(); 
59
60
61                Space*                              getSpace(); 
62
63                void                                 enable();
64                void                                 disable();
65                bool                                 isEnabled(); 
66
67                Geometry::Class                     getClass() const; 
68
69        void                                 setCategoryBitfield(unsigned long bits); 
70        void                                 setCollisionBitfield(unsigned long bits); 
71                unsigned long                                            getCategoryBitfield(); 
72                unsigned long                                            getCollisionBitfield(); 
73
74                int                                 collide(Geometry* geometry, CollisionListener* listener = 0);
75
76                void                                setMaxContacts(unsigned int max_contacts);
77                unsigned int                        getMaxContacts() const;
78                unsigned int                        getContactHighWaterMark() const;
79
80                void                                setEncapsulator(Geometry* encapsulator);
81                Geometry*                           getEncapsulator(){return _encapsulator;}
82
83                void                                setUserData(size_t user_data){_user_data = user_data;}
84                size_t                                                          getUserData() const {return _user_data;}
85
86                void                                setUserObject(UserDefinedObject* object){_user_object = object;}
87                UserDefinedObject*                  getUserObject(){return _user_object;}
88
89                virtual size_t                                           getID() const ;
90                virtual void                        notify(Body* body);
91
92
93        virtual void                        setDebug(const bool debug);
94        virtual void                        createDebugObject();
95        virtual void                        destroyDebugObject();
96        void                                reparentDebugObject(Ogre::Node* node);
97
98        virtual void                        setDebugContact(const bool debug);
99        void                                updateDebugContact();
100
101                void                                clearOffset();
102                int                                 isOffset();
103
104                void                                setOffsetPosition (const Ogre::Vector3 &position);
105                void                                setOffsetQuaternion(const Ogre::Quaternion &orientation);
106
107                 void                               setOffsetWorldPosition(const Ogre::Vector3 &position);             
108                 void                               setOffsetWorldQuaternion(const Ogre::Quaternion &orientation) ;
109
110                 Ogre::Vector3                      getOffsetPosition() ;
111                 Ogre::Quaternion                   getOffsetQuaternion() ;
112
113
114                /**     returns mGeometry's AABB
115                */
116                virtual const Ogre::AxisAlignedBox& getBoundingBox(void) const  {return _bounding_box; };
117
118                /**
119                */
120                static const Ogre::String MovableType;
121                virtual const Ogre::String& getMovableType() const { return MovableType; }; 
122
123                /**
124                */             
125                virtual Ogre::Real getBoundingRadius(void) const { return Ogre::Real(0.0); }
126               
127                /**
128                */
129                virtual void _updateRenderQueue(Ogre::RenderQueue* queue){}
130               
131                /** added for Ogre 1.5
132                */
133#if OGRE_VERSION >= ((1 << 16) | (5 << 8))
134                virtual void visitRenderables(Ogre::Renderable::Visitor* visitor, bool debugRenderables = false){}
135#endif
136
137                /** set mParentNode and sets mGeometrys position and orientation to match mParentNode's
138                */
139                virtual void _notifyAttached(Ogre::Node* parent, bool isTagPoint = false);
140               
141                /** sets mGeometry position and orientation to match mParentNode's
142                */
143                virtual void _notifyMoved();
144               
145               
146                virtual const Ogre::String& getName (void) const { return mName; }
147               
148                /** setting mName is not important - its not actually being used
149                it just helps debugging code
150                */
151                void setName (Ogre::String name){ mName = name; }
152
153
154
155        protected:
156                dGeomID                             getGeometryID() const;
157                void                                registerGeometry();
158                dSpaceID                            getSpaceID(Space* space) const;
159
160        protected:
161                Ogre::String                                            mName;
162                dGeomID                             _geom;
163                unsigned int                        _max_contacts;
164        unsigned int                        _contact_high_water_mark;
165        unsigned int                        _last_contact_num;
166                dContact*                           _contacts;
167
168                DebugObject*                        _debug_obj;
169        Ogre::Node*                         _debug_node;
170        DebugContact**                      _debug_contacts;
171
172                Geometry*                           _encapsulator;
173                Ogre::AxisAlignedBox                _bounding_box;
174                Ogre::Quaternion                    _orientation;
175                Ogre::Vector3                       _position;
176
177                static int                          _geometry_count;
178                size_t                                                          _user_data;
179                UserDefinedObject*                  _user_object;
180        World                               *_world;
181
182        };
183
184        //------------------------------------------------------------------------------------------------
185    class _OgreOdeExport SphereGeometry:public Geometry
186        {
187        public:
188                SphereGeometry(Ogre::Real radius, World *world, Space* space = 0);
189                ~SphereGeometry();
190
191                virtual void createDebugObject();
192
193                void setRadius(Ogre::Real radius);
194                Ogre::Real getRadius();
195        Ogre::Real getPointDepth(const Ogre::Vector3& point); 
196        /** Return a string identifying the type of user defined object.
197           @remarks
198           Used to differentiate between different Bodies, Geometries and prefab_object
199        */
200        const Ogre::String& getTypeName(void) const
201        {static Ogre::String sName("Geometry Sphere");return sName;};
202
203        };
204
205        //------------------------------------------------------------------------------------------------
206        class _OgreOdeExport BoxGeometry:public Geometry
207        {
208        public:
209                BoxGeometry(const Ogre::Vector3& size,  World *world, Space* space = 0);
210                ~BoxGeometry();
211
212                virtual void createDebugObject();
213
214                void setSize(const Ogre::Vector3& size);
215                const Ogre::Vector3& getSize();
216                Ogre::Real getPointDepth(const Ogre::Vector3& point);
217
218        /** Return a string identifying the type of user defined object.
219        @remarks
220        Used to differentiate between different Bodies, Geometries and prefab_object
221        */
222        const Ogre::String& getTypeName(void) const
223        {static Ogre::String sName("Geometry Box");return sName;};
224
225        protected:
226                Ogre::Vector3 _size;
227        };
228
229        //------------------------------------------------------------------------------------------------
230        class _OgreOdeExport InfinitePlaneGeometry:public Geometry
231        {
232        public:
233                InfinitePlaneGeometry(const Ogre::Plane& plane, World *world, Space* space = 0);
234                ~InfinitePlaneGeometry();
235
236                void setDefinition(const Ogre::Plane& plane);
237                const Ogre::Plane& getDefinition();
238                Ogre::Real getPointDepth(const Ogre::Vector3& point);
239
240                void setPosition(const Ogre::Vector3& position);
241                void setOrientation(const Ogre::Quaternion& orientation);       
242                const Ogre::Vector3& getPosition();
243                const Ogre::Quaternion& getOrientation();
244                const Ogre::AxisAlignedBox& getAxisAlignedBox(); 
245
246        /** Return a string identifying the type of user defined object.
247        @remarks
248        Used to differentiate between different Bodies, Geometries and prefab_object
249        */
250        const Ogre::String& getTypeName(void) const
251        {static Ogre::String sName("Geometry Infinity Plane");return sName;};
252
253        protected:
254                Ogre::Plane _plane;
255    };
256    //------------------------------------------------------------------------------------------------
257        class _OgreOdeExport CapsuleGeometry:public Geometry
258        {
259        public:
260                CapsuleGeometry(Ogre::Real radius, Ogre::Real length, World *world, Space* space = 0);
261                ~CapsuleGeometry();
262
263                virtual void createDebugObject();
264 
265                void setDefinition(Ogre::Real radius, Ogre::Real length);
266                Ogre::Real getRadius();
267                Ogre::Real getLength();
268                Ogre::Real getPointDepth(const Ogre::Vector3& point);
269
270
271        /** Return a string identifying the type of user defined object.
272        @remarks
273        Used to differentiate between different Bodies, Geometries and prefab_object
274        */
275        const Ogre::String& getTypeName(void) const
276        {static Ogre::String sName("Geometry Capsule");return sName;};
277
278        };
279        //------------------------------------------------------------------------------------------------
280        class _OgreOdeExport CylinderGeometry:public Geometry
281        {
282        public:
283                CylinderGeometry(Ogre::Real radius, Ogre::Real length, World *world, Space* space = 0);
284                ~CylinderGeometry();
285
286                virtual void createDebugObject();
287
288                void setDefinition(Ogre::Real radius, Ogre::Real length);
289                Ogre::Real getRadius();
290                Ogre::Real getLength();
291                Ogre::Real getPointDepth(const Ogre::Vector3& point);
292
293        /** Return a string identifying the type of user defined object.
294        @remarks
295        Used to differentiate between different Bodies, Geometries and prefab_object
296        */
297        const Ogre::String& getTypeName(void) const
298        {static Ogre::String sName("Geometry Cylinder");return sName;};
299
300        };
301        //------------------------------------------------------------------------------------------------
302        class _OgreOdeExport RayGeometry:public Geometry
303        {
304        public:
305                RayGeometry(Ogre::Real length, World *world, Space* space = 0);
306                ~RayGeometry();
307
308                virtual void createDebugObject();
309               
310                void setLength(Ogre::Real length);
311                Ogre::Real getLength();
312                void setDefinition(const Ogre::Vector3& start, const Ogre::Vector3& direction);
313                const Ogre::Vector3& getStart();
314                const Ogre::Vector3& getDirection();
315
316        /** Return a string identifying the type of user defined object.
317        @remarks
318        Used to differentiate between different Bodies, Geometries and prefab_object
319        */
320        const Ogre::String& getTypeName(void) const
321        {static Ogre::String sName("Geometry Ray");return sName;};
322
323        protected:
324                Ogre::Vector3 _direction,_start;
325        };
326
327        //------------------------------------------------------------------------------------------------
328        class _OgreOdeExport TransformGeometry:public Geometry
329        {
330        public:
331                TransformGeometry(World *world, Space* space);
332                ~TransformGeometry();
333
334                virtual void createDebugObject();
335                virtual void destroyDebugObject();
336
337                void setEncapsulatedGeometry(Geometry* geometry);
338                Geometry* getEncapsulatedGeometry() const;
339
340        /** Return a string identifying the type of user defined object.
341        @remarks
342        Used to differentiate between different Bodies, Geometries and prefab_object
343        */
344        const Ogre::String& getTypeName(void) const
345        {static Ogre::String sName("Geometry Transform");return sName;};
346
347        };
348        //------------------------------------------------------------------------------------------------
349        class _OgreOdeExport TriangleMeshCollisionListener
350        {
351        public:
352                TriangleMeshCollisionListener(){}
353                virtual ~TriangleMeshCollisionListener(){}
354
355                virtual bool collide(TriangleMeshGeometry* triangle_mesh, Geometry* geometry, int triangle) {return true;}
356        };
357
358        //------------------------------------------------------------------------------------------------
359        class _OgreOdeExport TriangleMeshIntersectionListener
360        {
361        public:
362                TriangleMeshIntersectionListener(){}
363                virtual ~TriangleMeshIntersectionListener(){}
364
365                virtual void intersect(TriangleMeshGeometry* triangle_mesh, Geometry* geometry, const int* triangles, int triangle_count){}
366        };
367
368        //------------------------------------------------------------------------------------------------
369        class _OgreOdeExport TriangleMeshRayListener
370        {
371        public:
372                TriangleMeshRayListener(){}
373                virtual ~TriangleMeshRayListener(){}
374
375                virtual bool collide(TriangleMeshGeometry* triangle_mesh, RayGeometry* ray,int triangle, const Ogre::Vector3& uv){return true;}
376        };
377
378        //------------------------------------------------------------------------------------------------
379        struct _OgreOdeExport TriangleMeshTriangle
380        {
381                Ogre::Vector3 v0,v1,v2;
382        };
383
384        //------------------------------------------------------------------------------------------------
385        class _OgreOdeExport TriangleMeshGeometry:public Geometry
386        {
387        public:
388                TriangleMeshGeometry(const Ogre::Vector3* vertices, 
389                             unsigned int vertex_count, 
390                             const TriangleIndex* indices,
391                             unsigned int index_count,
392                             World *world, 
393                             Space* space = 0);
394               
395                TriangleMeshGeometry(TriangleMeshDataPtr dataPtr, World *world, Space* space = 0);
396               
397                void changeTriangleMeshData(TriangleMeshDataPtr dataPtr);
398               
399                TriangleMeshDataPtr getTriangleMeshData(){ return _dataPtr; }
400               
401                ~TriangleMeshGeometry();
402
403                Ogre::Vector3 getPoint(unsigned int index, const Ogre::Vector3& uv);
404                TriangleMeshTriangle getTriangle(int index);
405
406                void clearTemporalCoherenceCache(); 
407                void enableTemporalCoherence(Geometry::Class geometry_class,bool enable); 
408                bool isTemporalCoherenceEnabled(Geometry::Class geometry_class);
409
410                static int _collisionCallback(dGeomID mesh,dGeomID object,int triangle);
411                void setCollisionListener(TriangleMeshCollisionListener* collision_listener);
412
413                static void _intersectionCallback(dGeomID mesh,dGeomID object,const int* triangles,int triangle_count);
414                void setIntersectionListener(TriangleMeshIntersectionListener* intersection_listener); 
415
416                static int _rayCallback(dGeomID mesh,dGeomID ray,int triangle,dReal u,dReal v);
417                void setRayListener(TriangleMeshRayListener* ray_listener); 
418
419                virtual void createDebugObject();
420
421        /** Return a string identifying the type of user defined object.
422        @remarks
423        Used to differentiate between different Bodies, Geometries and prefab_object
424        */
425        const Ogre::String& getTypeName(void) const
426        {static Ogre::String sName("Geometry Trimesh");return sName;};
427
428        protected:
429                dTriMeshDataID _data;
430                dVector3* _vertices;
431                unsigned int* _indices;
432                unsigned int _vertex_count;
433        unsigned int _index_count;
434
435                TriangleMeshDataPtr _dataPtr;
436               
437                TriangleMeshCollisionListener* _collision_listener;
438                TriangleMeshIntersectionListener* _intersection_listener;
439                TriangleMeshRayListener* _ray_listener;
440        };
441
442        //------------------------------------------------------------------------------------------------
443        class _OgreOdeExport ConvexGeometry:public Geometry
444        {
445        public:
446                ConvexGeometry(const Ogre::Vector3* vertices, 
447                                                unsigned int vertex_count, 
448                                                const unsigned int* indices,
449                                                unsigned int index_count,
450                                                World *world, 
451                        Space* space = 0);
452                ~ConvexGeometry();
453
454
455                virtual void createDebugObject();
456
457        /** Return a string identifying the type of user defined object.
458        @remarks
459        Used to differentiate between different Bodies, Geometries and prefab_object
460        */
461        const Ogre::String& getTypeName(void) const
462        {static Ogre::String sName("Geometry Convex");return sName;};
463
464        protected:
465                dReal   * _vertices;
466                unsigned int* _indices;
467                unsigned int _vertex_count;
468                unsigned int _index_count;
469        };
470
471        //------------------------------------------------------------------------------------------------
472        class _OgreOdeExport TerrainGeometryHeightListener
473        {
474        public:
475                TerrainGeometryHeightListener(){}
476                virtual ~TerrainGeometryHeightListener(){}
477
478                virtual Ogre::Real heightAt(const Ogre::Vector3& position) = 0;
479        };
480
481        //------------------------------------------------------------------------------------------------
482        class _OgreOdeExport TerrainGeometry:public Geometry, public Ogre::RaySceneQueryListener
483        {
484        public:
485        TerrainGeometry(World *world, 
486                        Space* space,
487                        const Ogre::Vector3 &scale,
488                        int nodes_per_sideX,
489                        int nodes_per_sideY,
490                        Ogre::Real worldSizeX,
491                        Ogre::Real worldSizeZ,
492                        bool centered,
493                        Ogre::Real thickness = 10.0f);
494
495                ~TerrainGeometry();
496
497                Ogre::Real getPointDepth(const Ogre::Vector3& point);
498                const Ogre::Vector3& getPosition();
499                const Ogre::Quaternion& getOrientation();
500               
501                static dReal _heightCallback(void* data, dReal x, dReal z);
502                static dReal _heightCallback(void* data, int x, int z);
503                void setHeightListener(TerrainGeometryHeightListener* listener);
504
505        inline Ogre::Real getHeightAt(const Ogre::Vector3& position)
506        {
507            _num_query++;
508            const Ogre::Vector3 pos (position.x * _sample_width - (_centered?_halfWorldSizeX:0),
509                position.y,
510                position.z * _sample_height - (_centered?_halfWorldSizeZ:0)); 
511            _ray.setOrigin(pos);
512            _ray_query->setRay(_ray);
513            _distance_to_terrain = static_cast<Ogre::Real>(0.0);
514            _ray_query->execute(this);
515            return position.y - _distance_to_terrain;
516        }
517
518                bool queryResult(Ogre::MovableObject *obj, Ogre::Real distance);
519                bool queryResult(Ogre::SceneQuery::WorldFragment *fragment, Ogre::Real distance);
520
521        size_t getNumQueries() const {return _num_query;};
522        void resetNumQueries(){_num_query = 0;};
523
524        /** Return a string identifying the type of user defined object.
525        @remarks
526        Used to differentiate between different Bodies, Geometries and prefab_object
527        */
528        const Ogre::String& getTypeName(void) const
529        {static Ogre::String sName("Geometry Terrain");return sName;};
530
531        protected:
532                Ogre::Real _max_height;
533                Ogre::Real _distance_to_terrain;
534                Ogre::Real _sample_width, _sample_height;
535                TerrainGeometryHeightListener* _listener;
536                Ogre::Ray _ray;
537                Ogre::RaySceneQuery* _ray_query;
538        bool _centered;
539        Ogre::Real _halfWorldSizeX; 
540        Ogre::Real _halfWorldSizeZ;
541        size_t _num_query;
542    };
543    //------------------------------------------------------------------------------------------------
544    class _OgreOdeExport PlaneBoundedRegionGeometryPlaneListener
545    {
546    public:
547        PlaneBoundedRegionGeometryPlaneListener(){}
548        virtual ~PlaneBoundedRegionGeometryPlaneListener(){}
549
550        virtual std::list<Ogre::Plane>* planesAt(const Ogre::Vector3& position) = 0;
551    };
552
553    //------------------------------------------------------------------------------------------------
554    class _OgreOdeExport PlaneBoundedRegionGeometry:public Geometry, public Ogre::RaySceneQueryListener
555    {
556    public:
557        PlaneBoundedRegionGeometry(World *world, 
558                                    Space* space,
559                                    const Ogre::AxisAlignedBox &scale);
560
561        ~PlaneBoundedRegionGeometry();
562
563        Ogre::Real getPointDepth(const Ogre::Vector3& point);
564        const Ogre::Vector3& getPosition();
565        const Ogre::Quaternion& getOrientation();
566
567        static std::list<Ogre::Plane> * _planeCallback(void* data, dReal x, dReal z);
568        static std::list<Ogre::Plane> * _planeCallback(void* data, int x, int z);
569        void setPlaneListener(PlaneBoundedRegionGeometryPlaneListener* listener);
570
571        inline std::list<Ogre::Plane> *planesAt(const Ogre::Vector3& position)
572        {
573            _num_query++;
574//             const Ogre::Vector3 pos (position.x * _sample_width - (_centered?_halfWorldSizeX:0),
575//                                     position.y,
576//                                     position.z * _sample_height - (_centered?_halfWorldSizeZ:0));
577//             _ray.setOrigin(pos);
578//             _ray_query->setRay(_ray);
579//             _distance_to_terrain = static_cast<Ogre::Real>(0.0);
580//             _ray_query->execute(this);
581
582            return _last_query_results;
583
584        }
585
586        bool queryResult(Ogre::MovableObject *obj, Ogre::Real distance);
587        bool queryResult(Ogre::SceneQuery::WorldFragment *fragment, Ogre::Real distance);
588
589        size_t getNumQueries() const {return _num_query;};
590        void resetNumQueries(){_num_query = 0;};
591
592        /** Return a string identifying the type of user defined object.
593        @remarks
594        Used to differentiate between different Bodies, Geometries and prefab_object
595        */
596        const Ogre::String& getTypeName(void) const
597        {static Ogre::String sName("Geometry Plane Bounded Region");return sName;};
598
599    protected:
600        Ogre::Real _max_height;
601        Ogre::Real _distance_to_terrain;
602        Ogre::Real _sample_width, _sample_height;
603        PlaneBoundedRegionGeometryPlaneListener* _listener;
604        Ogre::Ray _ray;
605        Ogre::RaySceneQuery* _ray_query;
606        bool _centered;
607        Ogre::Real _halfWorldSizeX; 
608        Ogre::Real _halfWorldSizeZ;
609        size_t _num_query;
610
611        std::list<Ogre::Plane> *_last_query_results;
612    };
613}
614
615#endif
Note: See TracBrowser for help on using the repository browser.