Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/ogreode/OgreOdeGeometry.cpp @ 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: 46.8 KB
Line 
1#include "OgreOdePrecompiledHeaders.h"
2#include "OgreOdeGeometry.h"
3
4#include <OgreSceneNode.h>
5#include <OgreSceneManager.h>
6#include <OgreStringConverter.h>
7
8#include "OgreOdeWorld.h"
9#include "OgreOdeSpace.h"
10#include "OgreOdeBody.h"
11#include "OgreOdeCollision.h"
12#include "OgreOdeDebugObject.h"
13#include "OgreOdeDebugContact.h"
14
15using namespace OgreOde;
16using namespace Ogre;
17
18//------------------------------------------------------------------------------------------------
19int Geometry::_geometry_count = 0;
20const Ogre::String Geometry::MovableType = "OgreOde_Geometry";
21
22//------------------------------------------------------------------------------------------------
23Geometry::Geometry(World *world, Space* space):
24    UserDefinedObject(),
25        _geom (0),
26        _max_contacts (32),
27        _contact_high_water_mark (0),
28    _last_contact_num(0),
29        _contacts (0),
30        _debug_obj (0),
31        _debug_node (0),
32    _debug_contacts (0),
33        _encapsulator (0),
34        _user_data (0),
35        _user_object (0),
36    _world(world)
37{
38        mName = "";
39}
40//------------------------------------------------------------------------------------------------
41Geometry::~Geometry()
42{
43    destroyDebugObject();
44
45    delete[] _contacts;
46    if (_debug_contacts)
47    {         
48        for (unsigned int i = 0; i < _max_contacts; i++)
49            delete _debug_contacts[i];
50        delete [] _debug_contacts;
51    }
52    if (_geom)
53    {
54        _world->getGeometryList().unregisterItem((unsigned long)_geom);
55        dGeomDestroy(_geom); 
56    }
57}
58//------------------------------------------------------------------------------------------------
59dGeomID Geometry::getGeometryID() const
60{
61        return _geom;
62}
63
64//------------------------------------------------------------------------------------------------
65void Geometry::setEncapsulator(Geometry* encapsulator)
66{
67        _encapsulator = encapsulator;
68
69        if (_world->getShowDebugGeometries())
70        {
71                reparentDebugObject(encapsulator->_debug_node);
72        }
73}
74
75//------------------------------------------------------------------------------------------------
76void Geometry::reparentDebugObject(Ogre::Node* node)
77{
78        if ((node)&&(_debug_node)&&(_debug_node->getParent() != node))
79        {
80                Ogre::SceneNode* parent = static_cast<Ogre::SceneNode*>(_debug_node->getParent());
81                parent->removeChild(_debug_node->getName());
82                node->addChild(_debug_node);
83
84                _debug_node->setPosition(getPosition());
85                _debug_node->setOrientation(getOrientation());
86        }
87}
88
89//------------------------------------------------------------------------------------------------
90void Geometry::createDebugObject()
91{
92        if (!_debug_node)
93        {
94                Ogre::SceneNode* parent = _world->_manager->getRootSceneNode();
95                _debug_node = static_cast<Ogre::Node*>(parent->createChildSceneNode(Ogre::String("OgreOde::Geometry_") + Ogre::StringConverter::toString(_geometry_count) + Ogre::String("_Debug"))); 
96
97        if (_debug_obj)
98        {
99            static_cast<Ogre::SceneNode*>(_debug_node)->attachObject(_debug_obj);
100        }
101
102        if (getBody())
103        {
104            static_cast<Ogre::SceneNode*>(_debug_node->getParent())->removeChild(_debug_node->getName());
105            getBody()->addDebugNode(_debug_node);
106            if (_debug_obj) 
107                                _debug_obj->setMode(DebugObject::Mode_Enabled);
108                       
109                        //reposition the debug node if the geometry has an offset
110                        if(isOffset())
111                                _debug_node->setPosition(getOffsetPosition()); 
112        }
113                else if (_encapsulator)
114                {
115                        reparentDebugObject(_encapsulator->_debug_node);
116                }
117                else
118                {
119                        if (_debug_obj) 
120                _debug_obj->setMode(DebugObject::Mode_Static);
121                       
122                        _debug_node->setPosition(getPosition());
123                        _debug_node->setOrientation(getOrientation());
124                }
125                _geometry_count++;
126        }
127}
128
129//------------------------------------------------------------------------------------------------
130void Geometry::destroyDebugObject()
131{
132        if (_debug_node)
133    {
134
135        Ogre::SceneNode* sn = static_cast<Ogre::SceneNode*>(_debug_node);
136        sn->removeAndDestroyAllChildren ();
137        sn = static_cast<Ogre::SceneNode*>(_debug_node->getParent());
138        sn->removeAndDestroyChild(_debug_node->getName());
139                _debug_node = 0;
140        }
141
142        if (_debug_obj)
143        {
144                delete _debug_obj;
145                _debug_obj = 0;
146        }
147}
148
149//------------------------------------------------------------------------------------------------
150void Geometry::registerGeometry()
151{
152        dGeomSetData(_geom, (void*)this);
153        assert(_world);
154    _world->getGeometryList().registerItem(this);
155
156    if (_world->getShowDebugGeometries()) 
157        createDebugObject();
158
159    setMaxContacts (_max_contacts);
160}
161//------------------------------------------------------------------------------------------------
162dSpaceID Geometry::getSpaceID(Space* space) const
163{
164        if (!space) return 0;
165        return space->getSpaceID();
166}
167
168//------------------------------------------------------------------------------------------------
169void Geometry::setBody(Body* body)
170{
171        destroyDebugObject();
172    if (body)
173    {
174        body->addGeometry (this);
175        dGeomSetBody(_geom, body->getBodyID()); 
176    }
177    else
178    {
179        dGeomSetBody(_geom, 0); 
180    }
181        if (_world->getShowDebugGeometries()) 
182        createDebugObject();
183}
184
185//------------------------------------------------------------------------------------------------
186Body* Geometry::getBody()
187{
188        const dBodyID body = dGeomGetBody(_geom);
189        if (!body) return 0;
190        else return (Body*)dBodyGetData(body); 
191}
192
193//------------------------------------------------------------------------------------------------
194void Geometry::notify(Body* body)
195{
196        if (getBody() == body)
197        {
198                destroyDebugObject();
199        }
200}
201
202//------------------------------------------------------------------------------------------------
203void Geometry::setDebug(const bool debug)
204{
205    destroyDebugObject();
206    if (debug) 
207        createDebugObject();   
208}
209//------------------------------------------------------------------------------------------------
210void Geometry::setDebugContact(const bool debug)
211{   
212    if (_debug_contacts)
213    {
214        for (unsigned int i = 0; i < _max_contacts; i++)
215            delete _debug_contacts[i];
216        delete [] _debug_contacts;
217        _debug_contacts = 0;
218    }
219    if (debug) 
220    {
221        _debug_contacts = new DebugContact*[_max_contacts];
222        for (unsigned int i = 0; i < _max_contacts; i++)
223            _debug_contacts[i] = new DebugContact(Ogre::StringConverter::toString((size_t)_geom) + 
224                                                    "_Contact_" + 
225                                                    Ogre::StringConverter::toString(i),
226                                                    _world);
227    }
228}
229//------------------------------------------------------------------------------------------------
230void Geometry::setPosition(const Ogre::Vector3& position)
231{
232    dGeomSetPosition(_geom,(dReal)position.x,(dReal)position.y,(dReal)position.z);
233
234    if ((_debug_node)&& ((!getBody()) || (_encapsulator)))
235        _debug_node->setPosition(position);
236}
237
238//------------------------------------------------------------------------------------------------
239void Geometry::setOrientation(const Ogre::Quaternion& orientation)
240{
241        dQuaternion q;
242        q[0] = (dReal)orientation.w;
243        q[1] = (dReal)orientation.x;
244        q[2] = (dReal)orientation.y;
245        q[3] = (dReal)orientation.z;
246        dGeomSetQuaternion(_geom,q); 
247
248        if ((_debug_node)&& ((!getBody()) || (_encapsulator))) _debug_node->setOrientation(orientation);
249}
250
251//------------------------------------------------------------------------------------------------
252const Ogre::Vector3& Geometry::getPosition()
253{
254        const dReal* position = dGeomGetPosition(_geom);
255        _position.x = (Ogre::Real)position[0];
256        _position.y = (Ogre::Real)position[1];
257        _position.z = (Ogre::Real)position[2];
258        return _position;
259}
260
261//------------------------------------------------------------------------------------------------
262const Ogre::Quaternion& Geometry::getOrientation()
263{
264        dQuaternion orientation;
265        dGeomGetQuaternion(_geom,orientation); 
266        _orientation.w = (Real)orientation[0];
267        _orientation.x = (Real)orientation[1];
268        _orientation.y = (Real)orientation[2];
269        _orientation.z = (Real)orientation[3];
270        return _orientation;
271}
272
273//------------------------------------------------------------------------------------------------
274const AxisAlignedBox& Geometry::getAxisAlignedBox()
275{
276        dReal aabb[6];
277        dGeomGetAABB(_geom,aabb);
278        _bounding_box.setExtents((Real)aabb[0],(Real)aabb[2],(Real)aabb[4],(Real)aabb[1],(Real)aabb[3],(Real)aabb[5]);
279        return _bounding_box;
280}
281
282//------------------------------------------------------------------------------------------------
283Space* Geometry::getSpace()
284{
285        if (dGeomGetSpace(_geom) == 0) 
286        {
287                // not in any space
288                return NULL;
289        }
290
291        return (Space*)_world->getSpaceList().findItem((size_t)dGeomGetSpace(_geom));
292}
293
294
295//------------------------------------------------------------------------------------------------
296void Geometry::enable()
297{
298        dGeomEnable(_geom);
299}
300
301//------------------------------------------------------------------------------------------------
302void Geometry::disable()
303{
304        dGeomDisable(_geom); 
305}
306
307//------------------------------------------------------------------------------------------------
308bool Geometry::isEnabled()
309{
310        return dGeomIsEnabled(_geom) != 0; 
311}
312
313//------------------------------------------------------------------------------------------------
314Geometry::Class Geometry::getClass() const
315{
316        return (Geometry::Class)dGeomGetClass(_geom);
317}
318
319//------------------------------------------------------------------------------------------------
320void Geometry::setCategoryBitfield(unsigned long bits)
321{
322        dGeomSetCategoryBits(_geom,bits); 
323}
324
325//------------------------------------------------------------------------------------------------
326void Geometry::setCollisionBitfield(unsigned long bits)
327{
328        dGeomSetCollideBits(_geom,bits); 
329}
330
331//------------------------------------------------------------------------------------------------
332unsigned long Geometry::getCategoryBitfield()
333{
334        return dGeomGetCategoryBits(_geom); 
335}
336
337//------------------------------------------------------------------------------------------------
338unsigned long Geometry::getCollisionBitfield()
339{
340        return dGeomGetCollideBits(_geom); 
341}
342
343//------------------------------------------------------------------------------------------------
344int Geometry::collide(Geometry* geometry,CollisionListener* listener)
345{
346        const unsigned int num_contacts = (unsigned int) dCollide(_geom,
347                                                                                geometry->getGeometryID(),
348                                                                                _max_contacts,
349                                                                                &(_contacts[0].geom),
350                                                                                sizeof(dContact));
351        if (num_contacts)
352        {
353                _contact_high_water_mark = std::max(_contact_high_water_mark,num_contacts);
354
355                Contact contact;
356               
357                const dWorldID wid = _world->getWorldID();
358                const dJointGroupID cid = _world->getContactGroupID();
359                const dBodyID b1 = dGeomGetBody(_geom);
360                const dBodyID b2 = dGeomGetBody(geometry->getGeometryID());
361
362                if (listener)
363                {
364                        for(unsigned int i = 0;i < num_contacts;i++)
365                        {
366                                contact.setContact (&_contacts[i]);
367
368                                if (listener->collision(&contact))
369                                {
370                                        dJointAttach(dJointCreateContact(wid,cid,&_contacts[i]),b1,b2);
371                } 
372                        }
373                }
374                else
375                {
376                        for(unsigned int i = 0;i < num_contacts;i++)
377                        {
378                                contact.setContact (&_contacts[i]);
379   
380                                dJointAttach(dJointCreateContact(wid,cid,&_contacts[i]), b1, b2);
381                        }
382                } 
383    }
384    _last_contact_num = num_contacts;
385        return num_contacts;
386}
387//------------------------------------------------------------------------------------------------
388void Geometry::updateDebugContact()
389{
390    assert (_world->getShowDebugContact ());
391    assert (_debug_contacts);
392    {
393        unsigned int k = 0;
394        while (k < _max_contacts)
395        {
396            _debug_contacts[k++]->setEnabled(false);
397        }
398
399        if (_last_contact_num)
400        {
401            Contact contact;
402            for(unsigned int i = 0;i < _last_contact_num;i++)
403            {
404                k = 0;
405                while (k < _max_contacts)
406                {
407                    if (_debug_contacts[k]->isEnabled() == false)
408                        break;
409                    k++;
410                }
411                assert (k < _max_contacts);
412
413                _debug_contacts[k]->setEnabled(true);
414                contact.setContact (&_contacts[i]);
415                _debug_contacts[k]->update (&contact);
416            }
417        }
418    }
419    _last_contact_num = 0;
420 }
421//------------------------------------------------------------------------------------------------
422unsigned int Geometry::getMaxContacts() const
423{
424        return _max_contacts;
425}
426
427//------------------------------------------------------------------------------------------------
428void Geometry::setMaxContacts(unsigned int max_contacts)
429{
430        delete[] _contacts;
431        _contacts = new dContact[max_contacts];
432
433    if (_world->getShowDebugContact ())
434    {
435        if (_debug_contacts)
436        {         
437            for (unsigned int i = 0; i < _max_contacts; i++)
438                delete _debug_contacts[i];
439            delete [] _debug_contacts;
440        }
441        _debug_contacts = new DebugContact*[max_contacts];
442        for (unsigned int i = 0; i < max_contacts; i++)
443            _debug_contacts[i] = new DebugContact(Ogre::StringConverter::toString((size_t)_geom) 
444                        + "_Contact_" 
445                        + Ogre::StringConverter::toString(i),
446            _world);
447    }
448    _max_contacts = max_contacts;
449}
450
451//------------------------------------------------------------------------------------------------
452unsigned int Geometry::getContactHighWaterMark() const
453{
454        return _contact_high_water_mark;
455}
456
457//------------------------------------------------------------------------------------------------
458size_t Geometry::getID() const
459{
460        return (size_t)_geom;
461}
462//------------------------------------------------------------------------------------------------
463void Geometry::clearOffset()
464{
465        dGeomClearOffset(_geom);
466}
467//------------------------------------------------------------------------------------------------
468int Geometry::isOffset()
469{
470        return dGeomIsOffset(_geom);
471}
472//------------------------------------------------------------------------------------------------
473void Geometry::setOffsetPosition (const Ogre::Vector3 &pos)
474{
475        dGeomSetOffsetPosition (_geom, pos.x, pos.y, pos.z);
476
477        //move the debug object when setting offset if we have a body only
478        if ((_debug_node)&& ((getBody())/* || (_encapsulator)*/))
479                _debug_node->setPosition(getOffsetPosition()); 
480}
481//------------------------------------------------------------------------------------------------
482void Geometry::setOffsetQuaternion(const Ogre::Quaternion &quat) 
483{
484        dQuaternion q;
485        q[0] = quat.w; 
486        q[1] = quat.x; 
487        q[2] = quat.y; 
488        q[3] = quat.z;
489        dGeomSetOffsetQuaternion (_geom, q);
490
491        if ((_debug_node)&& ((getBody())/* || (_encapsulator)*/))
492                _debug_node->setOrientation(getOffsetQuaternion()); 
493}
494//------------------------------------------------------------------------------------------------
495void Geometry::setOffsetWorldPosition(const Ogre::Vector3 &pos) 
496{
497        dGeomSetOffsetWorldPosition(_geom, pos.x, pos.y, pos.z);
498
499        //move the debug object when setting offset if we have a body only
500        if ((_debug_node)&& ((getBody())/* || (_encapsulator)*/))
501                _debug_node->setPosition(getOffsetPosition()); 
502}
503//------------------------------------------------------------------------------------------------
504void Geometry::setOffsetWorldQuaternion(const Ogre::Quaternion &quat) 
505{
506        dQuaternion q;
507        q[0] = quat.w; 
508        q[1] = quat.x; 
509        q[2] = quat.y; 
510        q[3] = quat.z;
511        dGeomSetOffsetWorldQuaternion (_geom, q);
512
513        if ((_debug_node)&& ((getBody())/* || (_encapsulator)*/))
514                _debug_node->setOrientation(getOffsetQuaternion()); 
515}
516//------------------------------------------------------------------------------------------------
517Vector3 Geometry::getOffsetPosition() 
518{
519        const dReal *p =  dGeomGetOffsetPosition (_geom);
520        return Ogre::Vector3(p[0], p[1], p[2]);
521}
522//------------------------------------------------------------------------------------------------
523Quaternion Geometry::getOffsetQuaternion() 
524{
525        dQuaternion q;
526        dGeomGetOffsetQuaternion (_geom, q);
527        return Ogre::Quaternion(q[0], q[1], q[2], q[3]);
528}
529//-----------------------------------------------------------------------
530void Geometry::_notifyAttached(Node* parent, bool isTagPoint)
531{
532        MovableObject::_notifyAttached(parent, isTagPoint);
533        reparentDebugObject(parent);
534}
535//-----------------------------------------------------------------------
536void Geometry::_notifyMoved()
537{
538        MovableObject::_notifyMoved();
539               
540        if (!getBody())
541        {
542                setPosition(mParentNode->_getDerivedPosition());
543                setOrientation(mParentNode->_getDerivedOrientation());
544        }
545}
546//------------------------------------------------------------------------------------------------
547SphereGeometry::SphereGeometry(Real radius, World *world, Space* space):Geometry(world, space)
548{
549        _geom = dCreateSphere(getSpaceID(space),(dReal)radius); 
550        registerGeometry();
551}
552
553//------------------------------------------------------------------------------------------------
554void SphereGeometry::setRadius(Real radius)
555{
556        dGeomSphereSetRadius(_geom,(dReal)radius);
557}
558
559//------------------------------------------------------------------------------------------------
560Real SphereGeometry::getRadius()
561{
562        return (Real)dGeomSphereGetRadius(_geom); 
563}
564
565//------------------------------------------------------------------------------------------------
566Real SphereGeometry::getPointDepth(const Ogre::Vector3& point)
567{
568        return (Real)dGeomSpherePointDepth(_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z); 
569}
570
571//------------------------------------------------------------------------------------------------
572void SphereGeometry::createDebugObject()
573{
574        _debug_obj = new SphereDebugObject(getRadius());
575        Geometry::createDebugObject();
576}
577
578//------------------------------------------------------------------------------------------------
579SphereGeometry::~SphereGeometry()
580{
581}
582
583//------------------------------------------------------------------------------------------------
584BoxGeometry::BoxGeometry(const Ogre::Vector3& size, World *world, Space* space):Geometry(world, space)
585{
586        _geom = dCreateBox(getSpaceID(space),(dReal)size.x,(dReal)size.y,(dReal)size.z); 
587        registerGeometry();
588}
589
590//------------------------------------------------------------------------------------------------
591void BoxGeometry::setSize(const Ogre::Vector3& size)
592{
593        dGeomBoxSetLengths(_geom,(dReal)size.x,(dReal)size.y,(dReal)size.z); 
594}
595
596//------------------------------------------------------------------------------------------------
597const Ogre::Vector3& BoxGeometry::getSize()
598{
599        dVector3 result;
600        dGeomBoxGetLengths(_geom,result); 
601        _size.x = (Real)result[0];
602        _size.y = (Real)result[1];
603        _size.z = (Real)result[2];
604        return _size;
605}
606
607//------------------------------------------------------------------------------------------------
608Real BoxGeometry::getPointDepth(const Ogre::Vector3& point)
609{
610        return (Real)dGeomBoxPointDepth(_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z); 
611}
612
613//------------------------------------------------------------------------------------------------
614void BoxGeometry::createDebugObject()
615{
616        _debug_obj = new BoxDebugObject(getSize());
617        Geometry::createDebugObject(); 
618}
619
620//------------------------------------------------------------------------------------------------
621BoxGeometry::~BoxGeometry()
622{
623}
624
625//------------------------------------------------------------------------------------------------
626InfinitePlaneGeometry::InfinitePlaneGeometry(const Plane& plane,World *world, Space* space):Geometry(world, space)
627{
628        _geom = dCreatePlane(getSpaceID(space),(dReal)plane.normal.x,(dReal)plane.normal.y,(dReal)plane.normal.z,(dReal)-plane.d); 
629        registerGeometry();
630}
631
632//------------------------------------------------------------------------------------------------
633void InfinitePlaneGeometry::setDefinition(const Plane& plane)
634{
635        dGeomPlaneSetParams(_geom,(dReal)plane.normal.x,(dReal)plane.normal.y,(dReal)plane.normal.z,(dReal)-plane.d); 
636}
637
638//------------------------------------------------------------------------------------------------
639const Plane& InfinitePlaneGeometry::getDefinition()
640{
641        dVector4 result;
642        dGeomPlaneGetParams(_geom,result); 
643        _plane.normal.x = result[0];
644        _plane.normal.y = result[1];
645        _plane.normal.z = result[2];
646        _plane.d = -result[3];
647        return _plane;
648}
649
650//------------------------------------------------------------------------------------------------
651Real InfinitePlaneGeometry::getPointDepth(const Ogre::Vector3& point)
652{
653        return (Real)dGeomPlanePointDepth(_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z); 
654}
655
656//------------------------------------------------------------------------------------------------
657void InfinitePlaneGeometry::setPosition(const Ogre::Vector3& position)
658{
659}
660
661//------------------------------------------------------------------------------------------------
662void InfinitePlaneGeometry::setOrientation(const Ogre::Quaternion& orientation)
663{
664}
665
666//------------------------------------------------------------------------------------------------
667const Ogre::Vector3& InfinitePlaneGeometry::getPosition()
668{
669        return Ogre::Vector3::ZERO;
670}
671
672//------------------------------------------------------------------------------------------------
673const Ogre::Quaternion& InfinitePlaneGeometry::getOrientation()
674{
675        return Ogre::Quaternion::ZERO;
676}
677
678//------------------------------------------------------------------------------------------------
679const AxisAlignedBox& InfinitePlaneGeometry::getAxisAlignedBox()
680{
681        return _bounding_box;
682}
683
684//------------------------------------------------------------------------------------------------
685InfinitePlaneGeometry::~InfinitePlaneGeometry()
686{
687}
688
689//------------------------------------------------------------------------------------------------
690CapsuleGeometry::CapsuleGeometry(Real radius,Real length,World *world, Space* space):Geometry(world, space)
691{
692        _geom = dCreateCapsule(getSpaceID(space),(dReal)radius,(dReal)length);
693        registerGeometry();
694}
695
696//------------------------------------------------------------------------------------------------
697void CapsuleGeometry::setDefinition(Real radius,Real length)
698{
699        dGeomCapsuleSetParams(_geom,(dReal)radius,(dReal)length);
700}
701
702//------------------------------------------------------------------------------------------------
703Real CapsuleGeometry::getRadius()
704{
705        dReal radius,length;
706        dGeomCapsuleGetParams(_geom,&radius,&length); 
707        return radius;
708}
709
710//------------------------------------------------------------------------------------------------
711Real CapsuleGeometry::getLength()
712{
713        dReal radius,length;
714        dGeomCapsuleGetParams(_geom,&radius,&length); 
715        return length;
716}
717
718//------------------------------------------------------------------------------------------------
719Real CapsuleGeometry::getPointDepth(const Ogre::Vector3& point)
720{
721        return (Real)dGeomCapsulePointDepth(_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z); 
722}
723
724//------------------------------------------------------------------------------------------------
725void CapsuleGeometry::createDebugObject()
726{
727        _debug_obj = new CapsuleDebugObject(getRadius(),getLength());
728        Geometry::createDebugObject();
729}
730
731//------------------------------------------------------------------------------------------------
732CapsuleGeometry::~CapsuleGeometry()
733{
734}
735
736//------------------------------------------------------------------------------------------------
737CylinderGeometry::CylinderGeometry(Real radius,Real length,World *world, Space* space):Geometry(world, space)
738{
739        _geom = dCreateCCylinder(getSpaceID(space),(dReal)radius,(dReal)length);
740        registerGeometry();
741}
742
743//------------------------------------------------------------------------------------------------
744void CylinderGeometry::setDefinition(Real radius,Real length)
745{
746        dGeomCCylinderSetParams(_geom,(dReal)radius,(dReal)length);
747}
748
749//------------------------------------------------------------------------------------------------
750Real CylinderGeometry::getRadius()
751{
752        dReal radius,length;
753        dGeomCCylinderGetParams(_geom,&radius,&length); 
754        return radius;
755}
756
757//------------------------------------------------------------------------------------------------
758Real CylinderGeometry::getLength()
759{
760        dReal radius,length;
761        dGeomCCylinderGetParams(_geom,&radius,&length); 
762        return length;
763}
764
765//------------------------------------------------------------------------------------------------
766Real CylinderGeometry::getPointDepth(const Ogre::Vector3& point)
767{
768        return (Real)dGeomCCylinderPointDepth(_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z); 
769}
770
771//------------------------------------------------------------------------------------------------
772void CylinderGeometry::createDebugObject()
773{
774        _debug_obj = new CylinderDebugObject(getRadius(),getLength());
775        Geometry::createDebugObject();
776}
777
778//------------------------------------------------------------------------------------------------
779CylinderGeometry::~CylinderGeometry()
780{
781}
782//------------------------------------------------------------------------------------------------
783RayGeometry::RayGeometry(Real length,World *world, Space* space) : Geometry(world, space)
784{
785        _geom = dCreateRay(getSpaceID(space),(dReal)length);
786        registerGeometry();
787}
788
789//------------------------------------------------------------------------------------------------
790void RayGeometry::createDebugObject()
791{
792        _debug_obj = new RayDebugObject(getStart(),getDirection(),getLength());
793        Geometry::createDebugObject();
794        if (_debug_node)
795        {
796                _debug_node->setPosition(Vector3::ZERO);
797                _debug_node->setOrientation(Quaternion::IDENTITY);
798        }
799}
800
801//------------------------------------------------------------------------------------------------
802void RayGeometry::setLength(Real length)
803{
804        dGeomRaySetLength(_geom,(dReal)length); 
805}
806
807//------------------------------------------------------------------------------------------------
808Real RayGeometry::getLength()
809{
810        return (Real)dGeomRayGetLength(_geom); 
811}
812
813//------------------------------------------------------------------------------------------------
814void RayGeometry::setDefinition(const Ogre::Vector3& start,const Ogre::Vector3& direction)
815{
816        dGeomRaySet(_geom,
817                                        (dReal)start.x,(dReal)start.y,(dReal)start.z,
818                                        (dReal)direction.x,(dReal)direction.y,(dReal)direction.z);
819
820        if ((_debug_node)&& ((!getBody()) || (_encapsulator))) 
821                (static_cast<RayDebugObject *>(_debug_obj))->setDefinition(start, direction, (Real)dGeomRayGetLength(_geom));
822
823}
824
825//------------------------------------------------------------------------------------------------
826const Ogre::Vector3& RayGeometry::getStart()
827{
828        dVector3 start,direction;
829        dGeomRayGet(_geom,start,direction); 
830        _start.x = (Real)start[0];
831        _start.y = (Real)start[1];
832        _start.z = (Real)start[2];
833        return _start;
834}
835
836//------------------------------------------------------------------------------------------------
837const Ogre::Vector3& RayGeometry::getDirection()
838{
839        dVector3 start,direction;
840        dGeomRayGet(_geom,start,direction); 
841        _direction.x = (Real)direction[0];
842        _direction.y = (Real)direction[1];
843        _direction.z = (Real)direction[2];
844        return _direction;
845}
846
847//------------------------------------------------------------------------------------------------
848RayGeometry::~RayGeometry()
849{
850}
851
852//------------------------------------------------------------------------------------------------
853TransformGeometry::TransformGeometry(World *world, Space* space) : Geometry(world, space)
854{
855        _geom = dCreateGeomTransform(getSpaceID(space)); 
856        dGeomTransformSetCleanup(_geom,0);
857        dGeomTransformSetInfo(_geom,1);
858        registerGeometry();
859}
860
861//------------------------------------------------------------------------------------------------
862void TransformGeometry::setEncapsulatedGeometry(Geometry* geometry)
863{
864        dGeomTransformSetGeom(_geom,geometry->getGeometryID()); 
865        destroyDebugObject();
866        if (_world->getShowDebugGeometries()) 
867                createDebugObject();   
868
869        geometry->setEncapsulator(this);
870}
871
872//------------------------------------------------------------------------------------------------
873void TransformGeometry::createDebugObject()
874{
875        Geometry::createDebugObject();
876        if (getEncapsulatedGeometry())
877        {
878                getEncapsulatedGeometry()->destroyDebugObject();
879                getEncapsulatedGeometry()->createDebugObject();
880        }
881}
882
883//------------------------------------------------------------------------------------------------
884void TransformGeometry::destroyDebugObject()
885{
886        if (getEncapsulatedGeometry()) getEncapsulatedGeometry()->destroyDebugObject();
887        Geometry::destroyDebugObject();
888}
889
890//------------------------------------------------------------------------------------------------
891Geometry* TransformGeometry::getEncapsulatedGeometry() const
892{
893        dGeomID id = dGeomTransformGetGeom(_geom);
894        if (id == 0) return 0;
895        else return (Geometry*)dGeomGetData(id); 
896}
897
898//------------------------------------------------------------------------------------------------
899TransformGeometry::~TransformGeometry()
900{
901}
902
903
904//------------------------------------------------------------------------------------------------
905TriangleMeshGeometry::TriangleMeshGeometry(const Ogre::Vector3* vertices,
906                                           unsigned int vertex_count,
907                                           const TriangleIndex* indices,
908                                           unsigned int index_count,
909                                           World *world, Space* space) : 
910    Geometry(world, space),
911    _vertex_count (vertex_count),
912    _index_count (index_count)
913{
914        _vertex_count = vertex_count;
915        _index_count = index_count;
916        _vertices = new dVector3[vertex_count];
917        _indices = new unsigned int[index_count];
918
919        for(unsigned int i = 0;i < vertex_count;i++)
920        {
921                _vertices[i][0] = (dReal)vertices[i].x;
922                _vertices[i][1] = (dReal)vertices[i].y;
923                _vertices[i][2] = (dReal)vertices[i].z;
924        }
925
926        memcpy(_indices,indices,sizeof(unsigned int) * index_count);
927
928        _data = dGeomTriMeshDataCreate(); 
929
930        //dGeomTriMeshDataBuildSimple(_data,(const dReal*)_vertices, (int)vertex_count, _indices, (int) index_count);
931    // modified version
932        dGeomTriMeshDataBuildSimple(_data,(const dReal*)_vertices, (int)vertex_count, (dTriIndex*)_indices, (int) index_count); 
933
934        _geom = dCreateTriMesh(getSpaceID(space),_data,0,0,0);
935        registerGeometry();
936
937        _collision_listener = 0;
938}
939//------------------------------------------------------------------------------------------------
940TriangleMeshGeometry::TriangleMeshGeometry(TriangleMeshDataPtr dataPtr, World *world, Space* space) :
941        Geometry(world, space)
942{
943        // increases the referance count
944        _dataPtr = dataPtr;     
945       
946        //printf ("dataPtr counter %d\n", dataPtr.useCount() );
947        //printf ("_dataPtr counter %d\n", _dataPtr.useCount() );
948       
949        _data                   = dataPtr.getPointer()->getData();
950        _vertex_count   = dataPtr.getPointer()->getVertexCount();
951        _index_count    = dataPtr.getPointer()->getIndexCount();
952        _vertices               = dataPtr.getPointer()->getVertices();
953        _indices                = (unsigned int*) dataPtr.getPointer()->getIndices();
954       
955        _geom = dCreateTriMesh(getSpaceID(space), _data,0,0,0);
956        registerGeometry();
957       
958        _collision_listener = 0;
959}
960//------------------------------------------------------------------------------------------------
961void TriangleMeshGeometry::changeTriangleMeshData(TriangleMeshDataPtr dataPtr)
962{
963        if(dataPtr.isNull() == false)
964        {
965                _dataPtr = dataPtr;
966               
967                _data                   = dataPtr.getPointer()->getData();
968                _vertex_count   = dataPtr.getPointer()->getVertexCount();
969                _index_count    = dataPtr.getPointer()->getIndexCount();
970                _vertices               = dataPtr.getPointer()->getVertices();
971                _indices                = (unsigned int*) dataPtr.getPointer()->getIndices();
972       
973                dGeomTriMeshSetData(_geom, _data);
974       
975                if ( _world->getShowDebugGeometries() )
976                {
977                        destroyDebugObject();
978                        createDebugObject();
979                }
980        }
981        else // setting it to a NULL
982        {
983       
984                _dataPtr = dataPtr;
985               
986                _data                   = NULL;
987                _vertex_count   = 0;
988                _index_count    = 0;
989                _vertices               = NULL;
990                _indices                = NULL;
991               
992                dGeomTriMeshSetData(_geom, _data);
993               
994                if ( _world->getShowDebugGeometries() )
995                {
996                        destroyDebugObject();
997                        createDebugObject();
998                }
999                //printf ("TriangleMeshGeometry::changeTriangleMeshData error : The TriangleMeshDataPtr passed in isNull\n");
1000        }
1001}
1002
1003//------------------------------------------------------------------------------------------------
1004Vector3 TriangleMeshGeometry::getPoint(unsigned int index,const Ogre::Vector3& uv)
1005{
1006        dVector3 out;
1007        dGeomTriMeshGetPoint(_geom, (int) index, (dReal)uv.x, (dReal)uv.y, out); 
1008        return Ogre::Vector3((Real)out[0],(Real)out[1],(Real)out[2]);
1009}
1010
1011//------------------------------------------------------------------------------------------------
1012TriangleMeshTriangle TriangleMeshGeometry::getTriangle(int index)
1013{
1014        dVector3 v0,v1,v2;
1015        dGeomTriMeshGetTriangle(_geom,(int)index, &v0, &v1, &v2); 
1016       
1017        TriangleMeshTriangle tri;
1018
1019        tri.v0.x = v0[0]; tri.v0.y = v0[1]; tri.v0.z = v0[2];
1020        tri.v1.x = v1[0]; tri.v1.y = v1[1]; tri.v1.z = v1[2];
1021        tri.v2.x = v2[0]; tri.v2.y = v2[1]; tri.v2.z = v2[2];
1022       
1023        return tri;
1024}
1025
1026//------------------------------------------------------------------------------------------------
1027void TriangleMeshGeometry::clearTemporalCoherenceCache()
1028{
1029        dGeomTriMeshClearTCCache(_geom); 
1030}
1031
1032//------------------------------------------------------------------------------------------------
1033void TriangleMeshGeometry::enableTemporalCoherence(Geometry::Class geometry_class, bool enable)
1034{
1035        assert((geometry_class == Geometry::Class_Sphere)||(geometry_class == Geometry::Class_Box));
1036        dGeomTriMeshEnableTC(_geom,(int)geometry_class,(enable)?1:0); 
1037}
1038
1039//------------------------------------------------------------------------------------------------
1040bool TriangleMeshGeometry::isTemporalCoherenceEnabled(Geometry::Class geometry_class)
1041{
1042        return ((dGeomTriMeshIsTCEnabled(_geom,(int)geometry_class))?true:false);
1043}
1044
1045//------------------------------------------------------------------------------------------------
1046int TriangleMeshGeometry::_collisionCallback(dGeomID mesh,dGeomID object,int triangle)
1047{
1048        TriangleMeshGeometry* trimesh = (TriangleMeshGeometry*)dGeomGetData(mesh);
1049        if (trimesh->_collision_listener)
1050        {
1051                Geometry* geometry = (object)?((Geometry*)dGeomGetData(object)):0;
1052                return ((trimesh->_collision_listener->collide(trimesh,geometry,triangle))?1:0);
1053        }
1054        return 1;
1055}
1056
1057//------------------------------------------------------------------------------------------------
1058void TriangleMeshGeometry::setCollisionListener(TriangleMeshCollisionListener* collision_listener)
1059{
1060        _collision_listener = collision_listener;
1061        dGeomTriMeshSetCallback(_geom,(_collision_listener)?TriangleMeshGeometry::_collisionCallback:0); 
1062}
1063
1064//------------------------------------------------------------------------------------------------
1065void TriangleMeshGeometry::_intersectionCallback(dGeomID mesh,dGeomID object,const int* triangles,int triangle_count)
1066{
1067        TriangleMeshGeometry* trimesh = (TriangleMeshGeometry*)dGeomGetData(mesh);
1068        if (trimesh->_intersection_listener)
1069        {
1070                Geometry* geometry = (object)?((Geometry*)dGeomGetData(object)):0;
1071                trimesh->_intersection_listener->intersect(trimesh,geometry,triangles,triangle_count);
1072        }
1073}
1074
1075//------------------------------------------------------------------------------------------------
1076void TriangleMeshGeometry::setIntersectionListener(TriangleMeshIntersectionListener* intersection_listener)
1077{
1078        _intersection_listener = intersection_listener;
1079        dGeomTriMeshSetArrayCallback(_geom,(_intersection_listener)?TriangleMeshGeometry::_intersectionCallback:0); 
1080}
1081
1082//------------------------------------------------------------------------------------------------
1083int TriangleMeshGeometry::_rayCallback(dGeomID mesh,dGeomID ray,int triangle,dReal u,dReal v)
1084{
1085        TriangleMeshGeometry* trimesh = (TriangleMeshGeometry*)dGeomGetData(mesh);
1086        if (trimesh->_ray_listener)
1087        {
1088                RayGeometry* ray_geometry = (ray)?((RayGeometry*)dGeomGetData(ray)):0;
1089                return ((trimesh->_ray_listener->collide(trimesh,ray_geometry,triangle,Vector3((Real)u,(Real)v,0.0)))?1:0);
1090        }
1091        return 1;
1092}
1093
1094//------------------------------------------------------------------------------------------------
1095void TriangleMeshGeometry::setRayListener(TriangleMeshRayListener* ray_listener)
1096{
1097        _ray_listener = ray_listener;
1098        dGeomTriMeshSetRayCallback(_geom,(_ray_listener)?TriangleMeshGeometry::_rayCallback:0); 
1099}
1100
1101//------------------------------------------------------------------------------------------------
1102void TriangleMeshGeometry::createDebugObject()
1103{
1104        if (_index_count != 0)
1105        {
1106                _debug_obj = new TriangleMeshDebugObject((_index_count / 3) * 6);
1107                TriangleMeshDebugObject* obj = static_cast<TriangleMeshDebugObject*>(_debug_obj);
1108
1109                obj->beginDefinition();
1110                for(unsigned int i = 0,j = 0;i < _index_count;i+=3,j+=6)
1111                {
1112                        obj->setVertex(j,Vector3((Real)_vertices[_indices[i]][0],(Real)_vertices[_indices[i]][1],(Real)_vertices[_indices[i]][2]));
1113                        obj->setVertex(j+1,Vector3((Real)_vertices[_indices[i+1]][0],(Real)_vertices[_indices[i+1]][1],(Real)_vertices[_indices[i+1]][2]));
1114
1115                        obj->setVertex(j+2,Vector3((Real)_vertices[_indices[i+1]][0],(Real)_vertices[_indices[i+1]][1],(Real)_vertices[_indices[i+1]][2]));
1116                        obj->setVertex(j+3,Vector3((Real)_vertices[_indices[i+2]][0],(Real)_vertices[_indices[i+2]][1],(Real)_vertices[_indices[i+2]][2]));
1117
1118                        obj->setVertex(j+4,Vector3((Real)_vertices[_indices[i+2]][0],(Real)_vertices[_indices[i+2]][1],(Real)_vertices[_indices[i+2]][2]));
1119                        obj->setVertex(j+5,Vector3((Real)_vertices[_indices[i]][0],(Real)_vertices[_indices[i]][1],(Real)_vertices[_indices[i]][2]));
1120                }
1121                obj->endDefinition();
1122
1123                Geometry::createDebugObject();
1124        }
1125}
1126
1127//------------------------------------------------------------------------------------------------
1128TriangleMeshGeometry::~TriangleMeshGeometry()
1129{
1130        if (_dataPtr.isNull())
1131        {
1132                dGeomTriMeshDataDestroy(_data); 
1133                delete[] _vertices;
1134                delete[] _indices;
1135        }
1136}
1137
1138
1139//------------------------------------------------------------------------------------------------
1140ConvexGeometry::ConvexGeometry(const Ogre::Vector3* vertices,
1141                                                           unsigned int vertex_count,
1142                                                           const unsigned int* indices,
1143                                                           unsigned int index_count,
1144                               World *world, Space* space) : 
1145    Geometry(world, space),
1146    _vertex_count (vertex_count),
1147    _index_count (index_count)
1148{
1149        _vertices = new dReal[vertex_count*3];
1150        _indices = new unsigned int[index_count];
1151
1152        for(unsigned int i = 0;i < vertex_count;i++)
1153        {
1154                _vertices[i*3 + 0] = (dReal)vertices[i].x;
1155                _vertices[i*3 + 1] = (dReal)vertices[i].y;
1156                _vertices[i*3 + 2] = (dReal)vertices[i].z;
1157        }
1158
1159        memcpy(_indices,indices,sizeof(unsigned int) * index_count);
1160
1161
1162        _geom = dCreateConvex (getSpaceID(space),
1163                0,//dReal *_planes,
1164                0,//unsigned int _planecount,
1165                _vertices,
1166                vertex_count,
1167                0);//unsigned int *_polygons)
1168
1169        //      dGeomSetConvex (dGeomID g,
1170        //              dReal *_planes,
1171        //              unsigned int _count,
1172        //              dReal *_points,
1173        //              unsigned int _pointcount,unsigned int *_polygons);
1174
1175        registerGeometry();
1176}
1177//------------------------------------------------------------------------------------------------
1178void ConvexGeometry::createDebugObject()
1179{
1180        _debug_obj = new TriangleMeshDebugObject((_index_count / 3) * 6);
1181        TriangleMeshDebugObject* obj = static_cast<TriangleMeshDebugObject*>(_debug_obj);
1182
1183        obj->beginDefinition();
1184        for(unsigned int i = 0,j = 0;i < _index_count;i+=3,j+=6)
1185        {
1186                obj->setVertex(j,Vector3((Real)_vertices[_indices[i]*3 + 0],(Real)_vertices[_indices[i]*3 + 1],(Real)_vertices[_indices[i]*3 + 2]));
1187                obj->setVertex(j+1,Vector3((Real)_vertices[_indices[i+1]*3 + 0],(Real)_vertices[_indices[i+1]*3 + 1],(Real)_vertices[_indices[i+1]*3 + 2]));
1188
1189                obj->setVertex(j+2,Vector3((Real)_vertices[_indices[i+1]*3 + 0],(Real)_vertices[_indices[i+1]*3 + 1],(Real)_vertices[_indices[i+1]*3 + 2]));
1190                obj->setVertex(j+3,Vector3((Real)_vertices[_indices[i+2]*3 + 0],(Real)_vertices[_indices[i+2]*3 + 1],(Real)_vertices[_indices[i+2]*3 + 2]));
1191
1192                obj->setVertex(j+4,Vector3((Real)_vertices[_indices[i+2]*3 + 0],(Real)_vertices[_indices[i+2]*3 + 1],(Real)_vertices[_indices[i+2]*3 + 2]));
1193                obj->setVertex(j+5,Vector3((Real)_vertices[_indices[i]*3 + 0],(Real)_vertices[_indices[i]*3 + 1],(Real)_vertices[_indices[i]*3 + 2]));
1194        }
1195        obj->endDefinition();
1196
1197        Geometry::createDebugObject();
1198}
1199//------------------------------------------------------------------------------------------------
1200ConvexGeometry::~ConvexGeometry()
1201{
1202        delete[] _vertices;
1203        delete[] _indices;
1204}
1205
1206//------------------------------------------------------------------------------------------------
1207TerrainGeometry::TerrainGeometry(World *world, Space* space,
1208                                                                const Ogre::Vector3 &scale,
1209                                int nodes_per_sideX,
1210                                int nodes_per_sideY,
1211                                Ogre::Real worldSizeX,
1212                                Ogre::Real worldSizeZ,
1213                                                                bool centered,
1214                                Ogre::Real thickness) :
1215                        Geometry(world, space),
1216                        _max_height (scale.y),
1217                        _sample_width(scale.x),
1218                        _sample_height(scale.z),
1219                        _centered(centered),
1220                        _halfWorldSizeX(worldSizeX * 0.5),
1221                        _halfWorldSizeZ(worldSizeZ * 0.5)
1222{
1223        dHeightfieldDataID heightid = dGeomHeightfieldDataCreate();
1224        dGeomHeightfieldDataBuildCallback(      heightid, //getSpaceID(space),
1225                                                                                this, // pUserData ?
1226                                                                                TerrainGeometry::_heightCallback, 
1227                                                                                (dReal) (worldSizeX), //X
1228                                                                                (dReal) (worldSizeZ), //Z
1229                                                                                nodes_per_sideX, // w // Vertex count along edge >= 2
1230                                                                                nodes_per_sideY, // h // Vertex count along edge >= 2
1231                                                                                REAL( 1.0 ),     //scale
1232                                                                                REAL( 0.0 ),    // vOffset
1233                                                                                thickness,      // vThickness
1234                                                                                 0); // nWrapMode
1235
1236
1237        // Give some very bounds which, while conservative,
1238        // makes AABB computation more accurate than +/-INF.
1239        dGeomHeightfieldDataSetBounds( heightid, REAL( 0.0 ),  _max_height );
1240        _geom = dCreateHeightfield( getSpaceID(space), heightid, 1 );
1241
1242        _listener = 0;
1243        _ray = Ogre::Ray (Ogre::Vector3::ZERO, Ogre::Vector3::NEGATIVE_UNIT_Y);
1244        _ray_query = _world->getSceneManager()->createRayQuery(_ray);
1245
1246        _ray_query->setQueryTypeMask(Ogre::SceneManager::WORLD_GEOMETRY_TYPE_MASK );
1247        _ray_query->setWorldFragmentType(Ogre::SceneQuery::WFT_SINGLE_INTERSECTION); 
1248
1249        registerGeometry();
1250
1251        if (_centered)
1252        {
1253                // PLSM2 is centered by default.       
1254        //setPosition (Ogre::Vector3::ZERO);
1255        ;
1256        }
1257        else
1258        {
1259        // TSM is not centered by default.     
1260                setPosition (Ogre::Vector3(_halfWorldSizeX, 
1261                                    0, 
1262                                    _halfWorldSizeZ));
1263        }
1264        setOrientation(Ogre::Quaternion::ZERO);
1265}
1266
1267//------------------------------------------------------------------------------------------------
1268dReal TerrainGeometry::_heightCallback(void* data,int x,int z)
1269{
1270        TerrainGeometry * const terrain = (TerrainGeometry*)data;   
1271        if (terrain->_listener)
1272        {
1273                return static_cast <dReal> (terrain->_listener->heightAt(Vector3((Ogre::Real)x,
1274                                                                terrain->_max_height,
1275                                                                (Ogre::Real)z)));
1276        }
1277    else
1278    {
1279        return static_cast <dReal> (terrain->getHeightAt(Vector3((Ogre::Real)x, 
1280                                                        terrain->_max_height, 
1281                                                        (Ogre::Real)z)));
1282    }
1283}
1284
1285//------------------------------------------------------------------------------------------------
1286Ogre::Real TerrainGeometry::getPointDepth(const Ogre::Vector3& point)
1287{
1288        //return (Real)dGeomTerrainCallbackPointDepth(_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z);
1289        //return (Real) dGeomHeightfieldPointDepth (_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z);
1290        //dGetDepthFn(_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z);
1291        return (Real) 0.0;
1292}
1293
1294//------------------------------------------------------------------------------------------------
1295void TerrainGeometry::setHeightListener(TerrainGeometryHeightListener* listener)
1296{
1297        _listener = listener;
1298}
1299//------------------------------------------------------------------------------------------------
1300bool TerrainGeometry::queryResult(Ogre::MovableObject *obj, Ogre::Real distance)
1301{
1302        return false;
1303}
1304
1305//------------------------------------------------------------------------------------------------
1306bool TerrainGeometry::queryResult(Ogre::SceneQuery::WorldFragment *fragment,Ogre::Real distance)
1307{
1308        _distance_to_terrain = distance;
1309        return false;
1310}
1311//------------------------------------------------------------------------------------------------
1312const Ogre::Vector3& TerrainGeometry::getPosition()
1313{
1314        return Ogre::Vector3::ZERO;
1315}
1316
1317//------------------------------------------------------------------------------------------------
1318const Ogre::Quaternion& TerrainGeometry::getOrientation()
1319{
1320        return Ogre::Quaternion::ZERO;
1321}
1322
1323//------------------------------------------------------------------------------------------------
1324TerrainGeometry::~TerrainGeometry()
1325{
1326        _world->getSceneManager()->destroyQuery(_ray_query);
1327}
1328
1329//------------------------------------------------------------------------------------------------
1330std::list<Ogre::Plane>* PlaneBoundedRegionGeometry::_planeCallback(void* data,int x,int z)
1331{
1332    PlaneBoundedRegionGeometry* terrain = (PlaneBoundedRegionGeometry*)data;
1333
1334    if (terrain->_listener)
1335    {
1336        return terrain->_listener->planesAt(Vector3((Ogre::Real)x,
1337            terrain->_max_height,
1338            (Ogre::Real)z));
1339    }
1340    else
1341    {
1342        return  terrain->planesAt(Vector3((Ogre::Real)x, 
1343                terrain->_max_height, 
1344                (Ogre::Real)z));
1345    }
1346    return 0;
1347}
1348//------------------------------------------------------------------------------------------------
1349PlaneBoundedRegionGeometry::PlaneBoundedRegionGeometry (World *world, Space* space,
1350                                                        const Ogre::AxisAlignedBox &Size) : 
1351    Geometry(world,space)
1352{
1353
1354    _ray = Ogre::Ray (Ogre::Vector3::ZERO, Ogre::Vector3::NEGATIVE_UNIT_Y);
1355    _ray_query = _world->getSceneManager()->createRayQuery(_ray);
1356
1357    _ray_query->setQueryTypeMask(Ogre::SceneManager::WORLD_GEOMETRY_TYPE_MASK );
1358    _ray_query->setWorldFragmentType(Ogre::SceneQuery::WFT_SINGLE_INTERSECTION); 
1359
1360}
1361
1362//------------------------------------------------------------------------------------------------
1363Ogre::Real PlaneBoundedRegionGeometry::getPointDepth(const Ogre::Vector3& point)
1364{
1365    //dGetDepthFn(_geom,(dReal)point.x,(dReal)point.y,(dReal)point.z);
1366    return (Real) 0.0;
1367}
1368
1369//------------------------------------------------------------------------------------------------
1370void PlaneBoundedRegionGeometry::setPlaneListener(PlaneBoundedRegionGeometryPlaneListener* listener)
1371{
1372    _listener = listener;
1373}
1374//------------------------------------------------------------------------------------------------
1375bool PlaneBoundedRegionGeometry::queryResult(Ogre::MovableObject *obj, Ogre::Real distance)
1376{
1377    return false;
1378}
1379
1380//------------------------------------------------------------------------------------------------
1381bool PlaneBoundedRegionGeometry::queryResult(Ogre::SceneQuery::WorldFragment *fragment,Ogre::Real distance)
1382{
1383    _distance_to_terrain = distance;
1384    return false;
1385}
1386//------------------------------------------------------------------------------------------------
1387const Ogre::Vector3& PlaneBoundedRegionGeometry::getPosition()
1388{
1389    return Ogre::Vector3::ZERO;
1390}
1391
1392//------------------------------------------------------------------------------------------------
1393const Ogre::Quaternion& PlaneBoundedRegionGeometry::getOrientation()
1394{
1395    return Ogre::Quaternion::ZERO;
1396}
1397
1398//------------------------------------------------------------------------------------------------
1399PlaneBoundedRegionGeometry::~PlaneBoundedRegionGeometry()
1400{
1401    _world->getSceneManager()->destroyQuery(_ray_query);
1402}
1403
1404
Note: See TracBrowser for help on using the repository browser.