Project

General

Profile

SO3Engine
OgreNewt_CollisionSerializer.cpp
Go to the documentation of this file.
5
6namespace OgreNewt
7{
11
12
16
17
18 void CollisionSerializer::exportCollision(const CollisionPtr& collision, const Ogre::String& filename)
19 {
20 if( !collision )
21 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Argument collision is NULL","CollisionSerializer::exportCollision");
22
23// If our ogre is anterior to v1.8
24#if OGRE_VERSION < ((1 << 16) | (8 << 8) | 0)
25 mpfFile=fopen(filename.c_str(),"wb");
26 if (!mpfFile)
27 {
28 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to open file " + filename + " for writing","CollisionSerializer::exportCollision");
29 }
30
31 NewtonCollisionSerialize(collision->getWorld()->getNewtonWorld(), collision->m_col, &CollisionSerializer::_newtonSerializeCallback, this);
32 fclose(mpfFile);
33#else
34 try
35 {
36 mStream = Ogre::Root::getSingleton().createFileStream(filename, Ogre::RGN_DEFAULT, true);
37 NewtonCollisionSerialize(collision->getWorld()->getNewtonWorld(), collision->m_col, &CollisionSerializer::_newtonSerializeCallback, this);
38 }
39 catch (Ogre::Exception&)
40 {
41 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to open file " + filename + " for writing","CollisionSerializer::exportCollision");
42 }
43
44 mStream.get()->close();
45#endif
46 }
47
48
49 //CollisionPtr CollisionSerializer::importCollision(Ogre::DataStreamPtr& stream, OgreNewt::World* world)
51 {
52 CollisionPtr dest;
53
54 NewtonCollision* col = NewtonCreateCollisionFromSerialization(world->getNewtonWorld(), &CollisionSerializer::_newtonDeserializeCallback, &stream);
55
56 // the type doesn't really matter... but lets do it correctly
58 {
60 dest = CollisionPtr(new CollisionPrimitives::Box(world));
61 break;
63 dest = CollisionPtr(new CollisionPrimitives::Cone(world));
64 break;
67 break;
70 break;
73 break;
76 break;
79 break;
82 break;
85 break;
87 dest = CollisionPtr(new CollisionPrimitives::Null(world));
88 break;
91 default:
92 dest = CollisionPtr(new Collision(world));
93 }
94
95 dest->m_col = col;
96
97 return dest;
98 }
99
100
101 void CollisionSerializer::_newtonSerializeCallback(void* serializeHandle, const void* buffer, int size)
102 {
103 CollisionSerializer* me = (static_cast<CollisionSerializer*>(serializeHandle));
104 me->writeData(buffer, 1, size);
105 }
106
107
108 void CollisionSerializer::_newtonDeserializeCallback(void* deserializeHandle, void* buffer, int size)
109 {
110// Ogre::DataStreamPtr ptr =* (static_cast<Ogre::DataStreamPtr*>(deserializeHandle));
111 Ogre::DataStream& ptr = * (static_cast<Ogre::DataStream*>(deserializeHandle));
112
113 ptr.read(buffer, size);
114 }
115} // end namespace OgreNewt
116
117
represents a shape for collision detection
NewtonCollision * m_col
CollisionPrimitiveType getCollisionPrimitiveType() const
Returns the Collisiontype for this Collision.
const World * getWorld() const
create a compound from several collision pieces.
null collision (results in no collision)
TreeCollision - complex polygonal collision.
OgreNewt::CollisionPtr importCollision(Ogre::DataStream &stream, OgreNewt::World *world)
void exportCollision(const OgreNewt::CollisionPtr &collision, const Ogre::String &filename)
represents a physics world.
NewtonWorld * getNewtonWorld() const
retrieves a pointer to the NewtonWorld
Collision * CollisionPtr
@ CompoundCollisionPrimitiveType
@ ChamferCylinderPrimitiveType