Serialization failure? (kind of strange bug)

HexiDave

02-03-2006 00:58:01

Let me preface this by saying that I am still quite new to Ogre programming. I can get the terrain to function as a TreeCollision just fine, but something tells me I misread the PLSM2 wiki guide on integrating the TreeCollisionSerializer mechanism. Here's the code (appologies for the poor coding style - I've been trying about everything I could think of for a few hours now and my head is about to explode...):
void PhysicsManager::loadTerrainGeometry(Ogre::PagingLandScapeTile* tile, Ogre::Vector3* vertices, int numVertices, Ogre::IndexData* indexData)
{
PagingLandScapeTileInfo *blah = tile->getInfo();
log->logMessage("Loading: " + StringConverter::toString(blah->tileX) + "-" + StringConverter::toString(blah->tileZ));
if (tilesBodies[tile]==NULL)
{
log->logMessage("Got to Stage 1");
TreeCollisionSerializer* serializer=new TreeCollisionSerializer();
CollisionPrimitives::TreeCollision* collision;

String tileData = "coldata//tile"+StringConverter::toString(blah->tileX)+"-"+StringConverter::toString(blah->tileZ)+".collision";
std::ifstream file;
file.open(tileData.c_str());

if(!file.is_open()){
log->logMessage("Not Found: " + tileData);
collision = new CollisionPrimitives::TreeCollision(mWorld,numVertices,vertices,indexData,false);
serializer->exportTreeCollision(collision,"coldata//tile"+StringConverter::toString(blah->tileX)+"-"+StringConverter::toString(blah->tileZ)+".collision");
////////////////////////Problem starts here\\\\\\\\\\\\
}else{
log->logMessage("Found: " + tileData);
collision=new CollisionPrimitives::TreeCollision(mWorld);
Ogre::DataStreamPtr ptr(new FileStreamDataStream(&file));
serializer->importTreeCollision(ptr,collision);
log->logMessage("Serialized Data");
}
log->logMessage("Got to Stage 2");
Body* body=new Body(mWorld,collision);
delete collision;
body->attachToNode(tile->getSceneNode());
tilesBodies[tile]=body;
log->logMessage("Added to Map");
}/////////////////// End of loading block \\\\\\\\\\\
log->logMessage("Tile done!");
}


If I were to tear it back down to just exporting the serialized data, it would go off without a hitch. What I can only guess is that I'm exporting the data wrong and it crashes the importer later. It exports properly in that it goes to the right place and places data in the file as I tell it to (434kb per file, 64 tfiles in all.) When reading the files in my Hex editor, they ARE different, so they are saving properly (I guess.) This is a call-back function for each time PLSM2's tileLoad'ing mechanism fires.

The kicker for me is that (if you see the markers in the Log output) when I run it after the crash, I get this:

19:32:41: Loading: 2-3
19:32:41: Got to Stage 1
19:32:41: Found: coldata//tile2-3.collision
19:32:41: Serialized Data


How the hell is it finishing the serialization and not making it to the next log marker? What's worse is sometimes it actually gets a second tile loaded!

So, I'm at your mercy here. I can't go any further until I get this crazy thing to work. I just updated my CVS this morning, so I'm about as up-to-date as I can be. Any help you can provide would be most appriciated. It's probably something very simple, but I'm just not seeing it.

Thanks again.

Edit: If the code is too difficult to read, I can go back and edit/comment it. Sorry bout that :(

HexiDave

02-03-2006 14:13:17

Just for posterity - the error was file-loading. It loaded the file, but not in a format that Ogre liked, so it just exploded.

This is basically what I did:

There is a sub-folder called "coldata" and I just added it to the resources.cfg file under the "General" section as "./coldata"
PagingLandScapeTileInfo *blah = tile->getInfo();
String tileData = "tile"+StringConverter::toString(blah->tileX)+"-"+StringConverter::toString(blah->tileZ)+".collision";

TreeCollisionSerializer* serializer=new TreeCollisionSerializer();
CollisionPrimitives::TreeCollision* collision;


Then I check through a map<String,bool> since checking bodies in the old map failed a lot (*shrug*) and if it wasn't loaded before, I load it and check to see if the file exists:

if(!ResourceGroupManager::getSingleton().resourceExists("General",tileData)){
collision = new CollisionPrimitives::TreeCollision(mWorld,numVertices,vertices,indexData,false);
serializer->exportTreeCollision(collision,"coldata//tile"+StringConverter::toString(blah->tileX)+"-"+StringConverter::toString(blah->tileZ)+".collision");

That will check if the tile's serial data exists, and if not it will create it, otherwise:
}else{

collision=new CollisionPrimitives::TreeCollision(mWorld);
Ogre::DataStreamPtr ptr = ResourceGroupManager::getSingleton().openResource(tileData);


serializer->importTreeCollision(ptr,collision);

}

It loads the data using the ResourceGroupManager (much cleaner!) and imports it correctly - then you just do the standard:
Body* body=new Body(mWorld,collision);
body->attachToNode(tile->getSceneNode());
delete collision;


And it works just fine. If anyone needs sourcecode, I can probably clean up what I'm working on. I'm not the best programmer out there, so there's some ugly code in some parts, but it gets the job done.

This is all to integrate OgreNewt into PLSM2. The Wiki was a pretty straight-forward tutorial, but some things didn't work how I expected (the map<> for one thing - just never picked up on the data for some reason.)

walaber

02-03-2006 23:04:21

thanks for the info, I'm sure other users will find it useful.