PLSM2 & OgreNewt Help!

_Archic

08-07-2006 20:31:36

Hi!

Anybody has experience of PLSM2 and OgreNewt integration as it is described in Wiki? Probably PLSM2/OgreNewt has changed and this example already doesn't work..

I have looked on a forum and have found an example (http://www.ogre3d.org/phpBB2addons/view ... pedelegate), but it make unhandled exception on call PagingLandScapePageManager:: getPage.
Can you give me please an _working_ example (source code) of integration PLSM2 and OgreNewt?

Thanks

HexiDave

08-07-2006 21:51:47

I had this problem crop up recently - if you'd like to try to finish it on your own, then just when you first get the PagingLandScapeSceneManager passed into it (or in some cases, the SceneManager - depends on how you coded it I suppose...) do this:

PagingLandScapePageManager* mPageMgr = mSceneMgr->getPageManager();

Now instead of calling mSceneMgr->getPageManager()->getPage(), use mPageMgr->getPage(). I still don't know why mPageManager (the internal PagingLandScapePageManager) goes out of scope, but it happens right around the time a delegate is created/called.

Edit: just noticed you were linking to what I posted in :D

_Archic

08-07-2006 23:19:31

Unfortunately, it does not solve a problem. :cry:

HexiDave, whether it is impossible to take somewhere source codes of a "small" working example that I could learn it?

HexiDave

08-07-2006 23:57:43

I'll see what I can do - I modified a lot of the code to suit my needs.

Are you using the standard SceneManager or using PagingLandScapeSceneManager like I did in the thread you linked?

Also, what version of PLSM2 are you using (CVS? SDK? Is it fairly up to date?)

EDIT: I should also note that I still had problems with the PageManager disapearing while in Debug mode, so try compiling in Release mode.

_Archic

09-07-2006 13:37:27

I use PLSM2, which I download from a site (http://tuan.kuranes.free.fr/Ogre.html) last week, Newton 1.53, OgreNewt 0.08.

Below I have resulted my source codes.. They can be copied and inserted in MS VC ++ 8 and check this error.. May be I forget some important details? Help me please! :roll:


OgreNewtonApplication.h

#pragma once
#include <ExampleApplication.h>
//Ogre
#include "Ogre.h"
#include "OgreConfigFile.h"
#include "OgreKeyEvent.h"
#include "OgreEventListeners.h"
#include "OgreStringConverter.h"
#include "OgreException.h"
#include "OgrePrerequisites.h"
//PLSM2
#include "OgrePagingLandScapeOptions.h"
#include "OgrePagingLandScapeCamera.h"
#include "OgrePagingLandScapeData2DManager.h"
#include "OgrePagingLandScapeRenderableManager.h"
#include "OgrePagingLandScapeTextureCoordinatesManager.h"
#include "OgrePagingLandScapeRenderable.h"
#include "OgrePagingLandScapeTextureManager.h"
#include "OgrePagingLandScapeTexture.h"
#include "OgrePagingLandScapePageManager.h"
#include "OgrePagingLandScapeTile.h"
#include "OgrePagingLandScapeTileInfo.h"
#include "OgrePagingLandScapeTileManager.h"
#include "OgrePagingLandScapeRenderable.h"
#include "OgrePagingLandScapeRenderableManager.h"
#include "OgrePagingLandScapeRaySceneQuery.h"
#include "OgrePagingLandScapeCamera.h"

#include "OgrePagingLandScapeListenerManager.h"

#include "OgrePagingLandScapeSceneManager.h"

#include "OgrePagingLandScapeHorizon.h"


using namespace Ogre;
using namespace std;
#include <OgreNewt.h>

class PLSM2Manager
{
SceneManager* m_sceneManager;
OgreNewt::World *m_World;
map<String,OgreNewt::Body*> tilesBodies;
public:

PLSM2Manager(SceneManager* sceneManager,OgreNewt::World* World, Camera * mCamera):m_sceneManager(sceneManager),m_World(World)
{
PagingLandscapeDelegate * loadTileDelegate = new PagingLandscapeDelegate();
loadTileDelegate->bind(this,&PLSM2Manager::tileLoaded);
m_sceneManager->setOption("addLoadTileListener",loadTileDelegate);

PagingLandscapeDelegate * unloadTileDelegate = new PagingLandscapeDelegate();
unloadTileDelegate->bind(this,&PLSM2Manager::tileUnloaded);
m_sceneManager->setOption("addUnloadTileListener",unloadTileDelegate);

m_sceneManager->setOption("LoadNow", mCamera);
}


void tileUnloaded(PagingLandscapeEvent* e)
{
//recover PLSM2's tile object
int pageX=e->mPagex;
int pageZ=e->mPagez;

PagingLandScapePage* page = ((PagingLandScapeSceneManager*)m_sceneManager)->getPageManager()->getPage(pageX, pageZ); // UNHANDLED EXCEPTION HERE!!!

int tileX = e->mTilex;
int tileZ = e->mTilez;
PagingLandScapeTile* tile = page->getTile(tileX, tileZ);

String tileData = "page" + StringConverter::toString(pageX) + "-" + StringConverter::toString(pageZ) + "-tile" + StringConverter::toString(tileX) + "-" + StringConverter::toString(tileZ);

tile->getSceneNode()->removeAndDestroyAllChildren();
OgreNewt::Body *body = tilesBodies[tileData];

delete body;
tilesBodies[tileData]=NULL;
}


void tileLoaded(PagingLandscapeEvent* e)
{
//recover PLSM2's tile object
int pageX=e->mPagex;
int pageZ=e->mPagez;
int tileX=e->mTilex;
int tileZ=e->mTilez;


PagingLandScapeSceneManager* mgr = (PagingLandScapeSceneManager*)m_sceneManager;
PagingLandScapePageManager * pmgr =mgr->getPageManager();

PagingLandScapePage* page = pmgr->getPage(pageX, pageZ,false); // UNHANDLED EXCEPTION HERE!!!

PagingLandScapeTile* tile=page->getTile(tileX, tileZ);
//ask reference to geometry to PLSM2
std::vector<void*> params;
int renderLevel=0;
params.push_back(&pageX);
params.push_back(&pageZ);
params.push_back(&tileX);
params.push_back(&tileZ);
params.push_back(&renderLevel);
m_sceneManager->getOption("PageGetTileVertexData_2",&params);

//recover data at the end of the vector and send it to our physics class for the next step
int* numVtx=((int*)params[5]);
Vector3* vertices=((Vector3*)params[6]);
IndexData* indexData=((IndexData*)params[7]);

loadTerrainGeometry(tile,vertices,*numVtx,indexData);

//cleanup
delete[] vertices;
delete numVtx;
}



void loadTerrainGeometry(PagingLandScapeTile* tile, Vector3* vertices, int numVertices, IndexData* indexData)
{
PagingLandScapeTileInfo *blah = tile->getInfo();
String tileData = "page"+StringConverter::toString(blah->pageX) +"-" + StringConverter::toString(blah->pageZ) +"-tile"+StringConverter::toString(blah->tileX)+"-"+StringConverter::toString(blah->tileZ);

if(tilesBodies[tileData]==NULL)
{

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

if(!ResourceGroupManager::getSingleton().resourceExists("General",tileData + ".collision"))
{
collision = new OgreNewt::CollisionPrimitives::TreeCollision(m_World,numVertices,vertices,indexData,false);
serializer->exportTreeCollision(collision,"coldata//" + tileData + ".collision");
}
else
{
collision=new OgreNewt::CollisionPrimitives::TreeCollision(m_World);
DataStreamPtr ptr = ResourceGroupManager::getSingleton().openResource(tileData + ".collision");
serializer->importTreeCollision(ptr,collision);
}

OgreNewt::Body* body = new OgreNewt::Body(m_World,collision);
SceneNode* newBody = tile->getSceneNode()->createChildSceneNode(tileData);
body->attachToNode(newBody);

tilesBodies[tileData]=body;

delete collision;
}
}
};



class OgreNewtonApplication : public ExampleApplication
{
public:
OgreNewtonApplication(void);
~OgreNewtonApplication(void);

protected:
void createFrameListener();
void createScene();
void chooseSceneManager();


private:
OgreNewt::World* m_World;

Ogre::FrameListener* mNewtonListener;

PLSM2Manager *plsm2Manager;
};





OgreNewtonApplication.cpp

#include ".\OgreNewtonApplication.h"
#include <OgreNewt.h>


OgreNewtonApplication::OgreNewtonApplication(void)
{
m_World = new OgreNewt::World();
m_World->setWorldSize(AxisAlignedBox(-100000,-100000,-100000,100000,100000,100000));
}

OgreNewtonApplication::~OgreNewtonApplication(void)
{
delete m_World;
OgreNewt::Debugger::getSingleton().deInit();
}

void OgreNewtonApplication::chooseSceneManager()
{
mSceneMgr = mRoot->createSceneManager("PagingLandScapeSceneManager", "PagingLandScapeDemo" );
mSceneMgr->setWorldGeometry( Ogre::String("paginglandscape2.cfg") );
}

void OgreNewtonApplication::createScene()
{
// sky box.
mSceneMgr->setSkyBox(true, "Examples/CloudyNoonSkyBox");

Ogre::Vector3 size(10.0,1.0,10.0);
Ogre::SceneNode* node ;
Ogre::Entity* ent;
OgreNewt::Collision* col ;

//delete col;
// BOX BODY
// standard 1x1x1 cube.
size = Ogre::Vector3( 1, 1, 1 );
node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
ent = mSceneMgr->createEntity("box_body", "box.mesh" );
node->attachObject( ent );
node->setScale( size );

// rigid body.
col = new OgreNewt::CollisionPrimitives::Box( m_World, size );
OgreNewt::Body * bod = new OgreNewt::Body( m_World, col );
bod->attachToNode( node );

// initial position
bod->setPositionOrientation( Ogre::Vector3(1000,350,-2),Ogre::Quaternion::IDENTITY );
delete col;

Ogre::Real mass = 10.0;
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcBoxSolid( mass, size );
bod->setMassMatrix( mass, inertia );
bod->setStandardForceCallback();
mCamera->setPosition( Ogre::Vector3( 1000, 350, 100) );
}


void OgreNewtonApplication::createFrameListener()
{
plsm2Manager = new PLSM2Manager(mSceneMgr,m_World,mCamera);

mFrameListener = new ExampleFrameListener( mWindow, mCamera );
mRoot->addFrameListener(mFrameListener);

mNewtonListener = new OgreNewt::BasicFrameListener( mWindow, mSceneMgr, m_World, 120 );
mRoot->addFrameListener(mNewtonListener);
}

HexiDave

09-07-2006 14:54:30

I don't have time to test it right now, but try replacing all your SceneManager* to PagingLandScapeSceneManager* and initialize it like this:

mSceneMgr = (PagingLandScapeSceneManager*)mRoot->createSceneManager("PagingLandScapeSceneManager", "PagingLandScapeDemo" );

It might complain about linker errors, so if it does just link the Plugin_PagingLandScapeSceneManager2.lib which should be in the ogrenew\PlugIns\PagingLandScape2\bin\Release folder.

And again, make sure you're trying this in Release mode - I think one of the settings in Debug mode is causing a problem.

_Archic

09-07-2006 20:25:58

Ок, I understand.. :( Replacement SceneManager* to PagingLandScapeSceneManager* and rebuilding in release mode also does not give any changes.. I think a problem somewhere more deeply and consequently I search though any working source code for research.. :?:

HexiDave

10-07-2006 00:50:08

You've pretty much got exactly what I use - I have seperate SceneManagers for loading processes and things, so my code is all over the place.

Have you tried this:
class OgreNewtonApplication : public ExampleApplication
{
public:
OgreNewtonApplication(void);
~OgreNewtonApplication(void);

protected:
void chooseSceneManager(); // switched
void createScene();
void createFrameListener(); // switched


private:
OgreNewt::World* m_World;

Ogre::FrameListener* mNewtonListener;

PLSM2Manager *plsm2Manager;
};


You might be getting some funny memory problems there otherwise.

Other things to try would be not using m_sceneManager->setOption("LoadNow", mCamera); but instead setting the starting camera settings manually and not using setOption at all - set the camera up before you load your delegates in PLSM2Manager's constructor.

Also try commenting out the m_sceneManager->setOption("addLoadTileListener",loadTileDelegate); code out along with the unloading code to see if you still get a crash. Then uncomment the Loader to see if it keeps crashing.

Other than that I'm stumped at the moment. If I can clean my code up I'll post it.

_Archic

10-07-2006 16:02:44

You've pretty much got exactly what I use - I have seperate SceneManagers for loading processes and things, so my code is all over the place.

Have you tried this:

class OgreNewtonApplication : public ExampleApplication
{
public:
OgreNewtonApplication(void);
~OgreNewtonApplication(void);

protected:
void chooseSceneManager(); // switched
void createScene();
void createFrameListener(); // switched


private:
OgreNewt::World* m_World;

Ogre::FrameListener* mNewtonListener;

PLSM2Manager *plsm2Manager;
};



Sorry, And in what difference of the offered decision? :oops:

Other things to try would be not using m_sceneManager->setOption("LoadNow", mCamera); but instead setting the starting camera settings manually and not using setOption at all - set the camera up before you load your delegates in PLSM2Manager's constructor.


I do not know, how starting camera settings manually but if I moving upward "LoadNow" setOption, nothing varies..


Also try commenting out the m_sceneManager->setOption("addLoadTileListener",loadTileDelegate); code out along with the unloading code to see if you still get a crash. Then uncomment the Loader to see if it keeps crashing.


Certainly, if I make comments on this line, tileLoaded it is not caused also exception does not occur :)


Other than that I'm stumped at the moment. If I can clean my code up I'll post it.


I have come to conclusion, that it is not connected in any way with OgreNewt, and connected only with processing tileLoaded.. Therefore I reduce the code also have started up it in " new topic ".. Can be to me "bad" version PLSM2 Plugin has got.. :-) The Publication of your source codes will help to avoid many problems at many people in the future.. As people which use PLSM2 at all so much ;-)

tuan kuranes

24-07-2006 21:08:31

Did this topic resolved the problem:
http://www.ogre3d.org/phpBB2addons/view ... 0611#10611