[Solved]Using OSM and newtonBodyes

lonwolf

27-06-2006 12:54:53

yep the same question:

Is there anyway of scaling bodyes??

im using oFusion for exporting objects and the osmLoader creates the node and its attributes.. but when i create the TreeCollisionScQry make a body out of that and attach it to the node .. well those propertyes are not inherited.. so how can i scale a body to make it look like the entity




any sugestions?? :? :cry:

HexiDave

27-06-2006 13:29:07

As far as I know, you cannot move or scale TreeCollisions. If you want to move or scale TreeCollisions (not a good idea) then you'll probably have to recreate it after each change to the base object.

lonwolf

27-06-2006 13:32:49

i did it <:-p i have been using osm loader and i found a way to make bodyes look exactly like entityes and in the exact place :D :D

man it took me long (about 2 weeks :lol: )

so for all of those who use OSM and want to make bodyes when entities are created just use this

OgreNewt::Collision* colDotScen = new OgreNewt::CollisionPrimitives::TreeCollision( mWorld, pObjNode, true );
bodDotScen = new OgreNewt::Body( mWorld, colDotScen );
delete colDotScen;
bodDotScen->attachToNode( pObjNode );
bodDotScen->setPositionOrientation(pObjNode->getPosition(), pObjNode->getOrientation());


just put it into the createEntity function after the entity has been attached yo the node :wink:

Operator

03-09-2006 13:24:04

lonwolf --> Could you tell me how do you put the mWorld variable to the OgreOSMScene.cpp file? Is it an in-class var?

lonwolf

03-09-2006 13:29:27

sure!.
in OgreOSMScene.h:

at the private members add this
OgreNewt::World* mWorld;
and then to initialise the mWorld change th initialise() function with this one:
bool initialise(const char *pszXMLFile, OSMSceneCallbacks* pCallbacks = NULL, OgreNewt::World* pWorld = NULL);
and after that go in the cpp and change it there too:
bool OSMScene::initialise(const char* pszXMLFile, OSMSceneCallbacks* pCallbacks, OgreNewt::World* pWorld)
{
//Init Newton Wolrd

mWorld=pWorld;

and ur done :D

Operator

03-09-2006 13:51:28

Hmm I've got these errors... :/


ogreosmscene.cpp(54) : error C2511: 'bool OSMScene::initialise(const char *,OSMSceneCallbacks *,OgreNewt::World *)' : overloaded member function not found in 'OSMScene'
ogreosmscene.h(39) : see declaration of 'OSMScene'
ogreosmscene.cpp(737) : error C2065: 'mWorld' : undeclared identifier


My code only with changes:

OgreOSMScene.h

class OSMScene
{
public:
// Initialise
bool initialise(const char *pszXMLFile, OSMSceneCallbacks* pCallbacks = NULL, OgreNewt::World* pWorld = NULL);

private:
OgreNewt::World* mWorld;
}


OgreOSMScene.cpp

bool OSMScene::initialise(const char* pszXMLFile, OSMSceneCallbacks* pCallbacks, OgreNewt::World* pWorld)
{
mWorld = pWorld;
}

// Create all entities in scene
void OSMScene::createEntities(TiXmlElement* pEntityNode, Ogre::SceneNode* pSceneRoot)
{
// Iterate all meshes, creating them.
for (TiXmlElement* pMeshElem = pEntityNode->FirstChildElement();
pMeshElem != 0; pMeshElem = pMeshElem->NextSiblingElement())
{
// Ogre could cast an exception, in which case we just try to
// continue reading the other meshes
try
{
const char *pszName = pMeshElem->Attribute("name");
const char *pszFileName = pMeshElem->Attribute("filename");

// try to create the mesh
Entity *pEntity = mSceneMgr->createEntity(pszName, pszFileName);
if(pEntity==0) continue;

// Check if the object should cast shadows
const char *pszCastShadows=pMeshElem->Attribute("CastShadows");
if(pszCastShadows && stricmp(pszCastShadows, "no")==0)
pEntity->setCastShadows(false);
else
pEntity->setCastShadows(true);

// Create node with full information
SceneNode *pObjNode=createNode(pMeshElem, pSceneRoot);

// Attach the mesh entity to node
pObjNode->attachObject(pEntity);

OgreNewt::Collision* colDotScen = new OgreNewt::CollisionPrimitives::TreeCollision( mWorld, pObjNode, true );
OgreNewt::Body* bodDotScen = new OgreNewt::Body( mWorld, colDotScen );
delete colDotScen;
bodDotScen->attachToNode( pObjNode );
bodDotScen->setPositionOrientation(pObjNode->getPosition(), pObjNode->getOrientation());

// Notify
if(mCallbacks)
mCallbacks->OnEntityCreate(pEntity, pMeshElem);

// Add to entity list
mEntities.push_back(pEntity);
} catch(...)
{
continue;
}
}
}


oSceneLibDemo.h

class oSceneLibApplication : public ExampleApplication {
public:

oSceneLibApplication()
{
// create OgreNewt world.
mWorld = new OgreNewt::World();
}
~oSceneLibApplication()
{
delete mWorld;
OgreNewt::Debugger::getSingleton().deInit();
}
}

void createScene(void)
{
// Initialises with the scene to be loaded and the callback if requiered
oScene.initialise("scene.osm", &oe_Callback, mWorld);
}

lonwolf

03-09-2006 14:10:55


class OSMScene{
//etc
// Initialise
bool initialise(const char *pszXMLFile, OSMSceneCallbacks* pCallbacks = NULL, OgreNewt::World* pWorld = NULL);
//etc other functions
private:
OgreNewt::World* mWorld;
}


and in cpp

bool OSMScene::initialise(const char* pszXMLFile, OSMSceneCallbacks* pCallbacks, OgreNewt::World* pWorld)
{
//Init Newton Wolrd

mWorld=pWorld;

// Hook up callback interface
mCallbacks = pCallbacks;

LogManager::getSingleton().logMessage("********************************");
LogManager::getSingleton().logMessage("** oScene Loader Lib **");
LogManager::getSingleton().logMessage("********************************");

String msg("oSceneLoader: Loading '");
msg += pszXMLFile;
msg += "' file";
LogManager::getSingleton().logMessage(msg);

// Create new XML document
mXMLDoc = TiXmlDocumentPtr(new TiXmlDocument());

DataStreamPtr pStream = ResourceGroupManager::getSingleton().openResource(pszXMLFile);
if(!pStream->size())
{
mXMLDoc.setNull();

OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
"oSceneLoader: Empty scene file",
"OSMScene::initialise");

}

size_t iSize = pStream->size();
char *pBuf = new char[iSize+1];
memset(pBuf, 0, iSize+1);
pStream->read(pBuf, iSize);
pStream.setNull();
mXMLDoc->Parse(pBuf);
delete[] pBuf;

// check for errors
if(mXMLDoc->Error())
{
mXMLDoc.setNull();

String errDesc = "oSceneLoader: Failed to load scene file, ";
msg += mXMLDoc->ErrorDesc();

OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
errDesc.c_str(),
"OSMScene::initialise");
}

/*
mXMLDoc = TiXmlDocumentPtr(new TiXmlDocument(pszXMLFile));
if(!mXMLDoc->LoadFile())
{
mXMLDoc->ClearError();
try
{
{
DataStreamPtr pStream=ResourceGroupManager::getSingleton().openResource(pszXMLFile);
if(!pStream->size())
{
mXMLDoc.setNull();

OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
"Empty oScene file",
"OSMScene::initialise");

}

size_t iSize=pStream->size();
char *pBuf=new char[iSize+1];
memset(pBuf, 0, iSize+1);
pStream->read(pBuf, iSize);
pStream.setNull();
mXMLDoc->Parse(pBuf);
delete[] pBuf;
}

// check for errors
if(mXMLDoc->Error())
{
mXMLDoc.setNull();

String errDesc = mXMLDoc->ErrorDesc();

OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
errDesc.c_str(),
"OSMScene::initialise");

return false;
}
} catch(...)
{
mXMLDoc.setNull();

// LOGERROR "Failed to load scene file");
return false;
}
}
else {

mXMLDoc.setNull();

OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND, "oScene file not found", "OSMScene::initialise")

return false;
}
*/

return true;
}


and using it:
OSMScene oScene;
oSceneCallback oe_Callback;
oScene.initialise("scene.osm", &oe_Callback, mWorld);


it should work perfectly ;) if it doesnt post all the errors

Operator

03-09-2006 14:18:21

Nope, it doesn't change anything, I have still the same errors... :(
Maybe that's something about project settings?

lonwolf

03-09-2006 14:24:53

ok heres my entire cpp and h . Note : it has been changed a bit more since last time i posted..

osmscene.h
#ifndef __OGRE_XML_SCENE__
#define __OGRE_XML_SCENE__


#include "Ogre.h"
#include "OgreSharedPtr.h"
#include "tinyxml.h"
#include "tutorial.h"


#include "IOSMSceneCallbacks.h"

class TiXmlDocumentPtr : public Ogre::SharedPtr<TiXmlDocument> {
public:
TiXmlDocumentPtr() : Ogre::SharedPtr<TiXmlDocument>() {}
explicit TiXmlDocumentPtr(TiXmlDocument* rep) : Ogre::SharedPtr<TiXmlDocument>(rep) {}

};

class OSMScene
{
public:

typedef std::vector<Ogre::Camera*> CameraList;
typedef std::vector<Ogre::Light*> LightList;
typedef std::vector<Ogre::Entity*> EntityList;
typedef std::vector<OgreNewt::Body*> BodyList;
typedef std::vector<Ogre::SceneNode*> NodeList;

OSMScene(Ogre::SceneManager* pSceneMgr = 0, Ogre::RenderWindow* win = 0);
~OSMScene(void);

// Initialise
bool initialise(const char *pszXMLFile, OSMSceneCallbacks* pCallbacks = NULL, OgreNewt::World* pWorld = NULL);

// Declare all resources used in the scene
void declareResources(void);

// Create scene, optionally attaching it to a parent node
bool createOgreScene(Ogre::SceneNode *pParent = NULL);
bool createOgreNewtScene(Ogre::SceneNode* pParent = NULL, Ogre::Vector3 scale = Vector3(1,1,1), Ogre::Vector3 trans = Vector3(0,0,0));

// Get list of cameras in this scene
CameraList& getCameraList(void);

// Get list of lights in this scene
LightList& getLightList(void);

// Get list of lights in this scene
EntityList& getEntityList(void);

EntityList& getLastCreatedEntityesList(void);

OgreNewt::Body* getLastBody(void){return bodDotScen;}

NodeList getLastNodes(void){return mLastNodes;}

BodyList& getBodyList(void){ return mBodies; }

// Get created scene manager
Ogre::SceneManager* getSceneManager(void) { return mSceneMgr; }

//Get OgreNewt World
OgreNewt::World* getOgreNewtWorld(void) { return mWorld; }

private:
// Create node from information
Ogre::SceneNode* createNode(TiXmlElement* pElem, Ogre::SceneNode *pSceneRoot);
Ogre::SceneNode* createNormalNode(TiXmlElement* pElem, Ogre::SceneNode *pSceneRoot);

// Creation helpers
void setSceneProperties(TiXmlElement* sceneProp);
void createEntities(TiXmlElement* pEntityNode, Ogre::SceneNode *pSceneRoot);
void createNormalEntities(TiXmlElement* pEntityNode, Ogre::SceneNode *pSceneRoot);
void createLights(TiXmlElement* pLightNode, Ogre::SceneNode *pSceneRoot);
void createCameras(TiXmlElement* pCameraNode, Ogre::SceneNode *pSceneRoot);

// Created objects
CameraList mCameras;
LightList mLights;
EntityList mEntities;
EntityList mLastCreatedEntityList;
BodyList mBodies;
NodeList mLastNodes;

OgreNewt::World* mWorld;
OgreNewt::Body* bodDotScen;
Ogre::Vector3 scale_vector;

// Callback interface
OSMSceneCallbacks* mCallbacks;

// Scene manager
Ogre::SceneManager* mSceneMgr;
Ogre::RenderWindow* mWindow;

// Scene XML document
TiXmlDocumentPtr mXMLDoc;
};

#endif


and the cpp
#include "OgreOSMScene.h"
#include "tinyxml.h"

using namespace Ogre;

enum {
SCENE_SKYPLANE = 1,
SCENE_SKYBOX,
SCENE_SKYDOME,
};

#define ENABLE_LOGMANAGER LogManager::getSingleton().setLogDetail(LL_NORMAL);
#define DISABLE_LOGMANAGER LogManager::getSingleton().setLogDetail(static_cast<LoggingLevel>(0));


OSMScene::OSMScene(SceneManager* pSceneMgr, RenderWindow* win)
{
mSceneMgr = pSceneMgr;
mWindow = win;
//mXMLDoc = 0;
mCallbacks = 0;
}

OSMScene::~OSMScene(void)
{
/*
if(mXMLDoc)
delete mXMLDoc;
*/
}

// Init overloads - use either of them
bool OSMScene::initialise(const char* pszXMLFile, OSMSceneCallbacks* pCallbacks, OgreNewt::World* pWorld)
{
//Init Newton Wolrd

mWorld=pWorld;

// Hook up callback interface
mCallbacks = pCallbacks;

LogManager::getSingleton().logMessage("********************************");
LogManager::getSingleton().logMessage("** oScene Loader Lib **");
LogManager::getSingleton().logMessage("********************************");

String msg("oSceneLoader: Loading '");
msg += pszXMLFile;
msg += "' file";
LogManager::getSingleton().logMessage(msg);

// Create new XML document
mXMLDoc = TiXmlDocumentPtr(new TiXmlDocument());

DataStreamPtr pStream = ResourceGroupManager::getSingleton().openResource(pszXMLFile);
if(!pStream->size())
{
mXMLDoc.setNull();

OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
"oSceneLoader: Empty scene file",
"OSMScene::initialise");

}

size_t iSize = pStream->size();
char *pBuf = new char[iSize+1];
memset(pBuf, 0, iSize+1);
pStream->read(pBuf, iSize);
pStream.setNull();
mXMLDoc->Parse(pBuf);
delete[] pBuf;

// check for errors
if(mXMLDoc->Error())
{
mXMLDoc.setNull();

String errDesc = "oSceneLoader: Failed to load scene file, ";
msg += mXMLDoc->ErrorDesc();

OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
errDesc.c_str(),
"OSMScene::initialise");
}

/*
mXMLDoc = TiXmlDocumentPtr(new TiXmlDocument(pszXMLFile));
if(!mXMLDoc->LoadFile())
{
mXMLDoc->ClearError();
try
{
{
DataStreamPtr pStream=ResourceGroupManager::getSingleton().openResource(pszXMLFile);
if(!pStream->size())
{
mXMLDoc.setNull();

OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
"Empty oScene file",
"OSMScene::initialise");

}

size_t iSize=pStream->size();
char *pBuf=new char[iSize+1];
memset(pBuf, 0, iSize+1);
pStream->read(pBuf, iSize);
pStream.setNull();
mXMLDoc->Parse(pBuf);
delete[] pBuf;
}

// check for errors
if(mXMLDoc->Error())
{
mXMLDoc.setNull();

String errDesc = mXMLDoc->ErrorDesc();

OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
errDesc.c_str(),
"OSMScene::initialise");

return false;
}
} catch(...)
{
mXMLDoc.setNull();

// LOGERROR "Failed to load scene file");
return false;
}
}
else {

mXMLDoc.setNull();

OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND, "oScene file not found", "OSMScene::initialise")

return false;
}
*/

return true;
}

// Declare all resources used in the scene
void OSMScene::declareResources(void)
{
if(!mXMLDoc.isNull())
{

TiXmlElement* rootElem = mXMLDoc->RootElement();

try
{
// Get mesh filename from entities
TiXmlElement *pMeshNode = rootElem->FirstChildElement("entities");
if(pMeshNode)
{
// Iterate all meshes, creating them.
for (TiXmlElement* pMeshElem = pMeshNode->FirstChildElement();
pMeshElem != 0; pMeshElem = pMeshElem->NextSiblingElement())
{
// Declare mesh resource
const char *pszFileName = pMeshElem->Attribute("filename");
ResourceGroupManager::getSingleton().declareResource(pszFileName, "Mesh");
}
}
} catch(...)
{
}

}
}

// Create scene, optionally attaching it to a parent node
bool OSMScene::createOgreScene(Ogre::SceneNode* pParent)
{
if(!mXMLDoc.isNull())
{
String msg("oSceneLoader: Creating scene on '");
msg += pParent ? pParent->getName() : "Root";
msg += "' node";
LogManager::getSingleton().logMessage(msg);

TiXmlElement* rootElem = mXMLDoc->RootElement();

if(mSceneMgr == NULL)
{
if(rootElem->FirstChildElement("sceneManager"))
setSceneProperties(rootElem);
else
mSceneMgr = Root::getSingleton().createSceneManager(Ogre::ST_EXTERIOR_CLOSE);
}

if(pParent==NULL)
pParent=mSceneMgr->getRootSceneNode()->createChildSceneNode();

TiXmlElement* list;

try {
// Entities
list = rootElem->FirstChildElement("entities");
if(list)
createNormalEntities(list, pParent);
} catch(...)
{
}

try {
// lights
list = rootElem->FirstChildElement("lights");
if(list)
createLights(list, pParent);
} catch(...)
{
}

try {
// cameras
list = rootElem->FirstChildElement("cameras");
if(list)
createCameras(list, pParent);
} catch(...)
{
}


LogManager::getSingleton().logMessage("********************************");
LogManager::getSingleton().logMessage("** oSceneLoader: Scene loaded **");
LogManager::getSingleton().logMessage("********************************");

return true;
}

return false;


}
bool OSMScene::createOgreNewtScene(Ogre::SceneNode* pParent, Ogre::Vector3 scale, Ogre::Vector3 trans)
{
if(!mXMLDoc.isNull())
{
String msg("oSceneLoader: Creating scene on '");
msg += pParent ? pParent->getName() : "Root";
msg += "' node";
LogManager::getSingleton().logMessage(msg);

TiXmlElement* rootElem = mXMLDoc->RootElement();

if(mSceneMgr == NULL)
{
if(rootElem->FirstChildElement("sceneManager"))
setSceneProperties(rootElem);
else
mSceneMgr = Root::getSingleton().createSceneManager(Ogre::ST_EXTERIOR_CLOSE);
}

if(pParent==NULL)
pParent=mSceneMgr->getRootSceneNode()->createChildSceneNode();

TiXmlElement* list;

try {
// Entities
list = rootElem->FirstChildElement("entities");
if(list)
createEntities(list, pParent);
} catch(...)
{
}

try {
// lights
list = rootElem->FirstChildElement("lights");
if(list)
createLights(list, pParent);
} catch(...)
{
}

try {
// cameras
list = rootElem->FirstChildElement("cameras");
if(list)
createCameras(list, pParent);
} catch(...)
{
}


pParent->setPosition(trans);
pParent->scale(scale);

/*for(i=mLastCreatedEntityList.begin();i!=mLastCreatedEntityList.end();++i)
{
SceneNode* sn = (*i)->getParentSceneNode();
OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::TreeCollision(mWorld, sn, true);
OgreNewt::Body* bod = new OgreNewt::Body(mWorld, col);
delete col;
bod->attachToNode(sn);
bod->setPositionOrientation(sn->getPosition(), sn->getOrientation());
}*/

/*OgreNewt::Collision* colDotScen = new OgreNewt::CollisionPrimitives::TreeCollision( mWorld, pParent, true );
OgreNewt::Body* bodDotScen = new OgreNewt::Body( mWorld, colDotScen );
delete colDotScen;
bodDotScen->attachToNode( pParent );
bodDotScen->setPositionOrientation(pParent->getPosition(), pParent->getOrientation());*/

LogManager::getSingleton().logMessage("********************************");
LogManager::getSingleton().logMessage("** oSceneLoader: Scene loaded **");
LogManager::getSingleton().logMessage("********************************");

return true;
}

return false;


}

// Get list of cameras in this scene
OSMScene::CameraList& OSMScene::getCameraList(void)
{
return mCameras;
}

// Get list of lights in this scene
OSMScene::LightList& OSMScene::getLightList(void)
{
return mLights;
}

// Get list of lights in this scene
OSMScene::EntityList& OSMScene::getEntityList(void)
{
return mEntities;
}

OSMScene::EntityList& OSMScene::getLastCreatedEntityesList(void)
{
return mLastCreatedEntityList;
}
Ogre::SceneNode *OSMScene::createNode(TiXmlElement* pElem, Ogre::SceneNode* pSceneRoot)
{
SceneNode *pNode;

// Try to find the parent node
const char *pszName = pElem->Attribute("name");
if(pszName==NULL) return NULL;

// Check if this node has a parent
const char *pszParent = pElem->Attribute("parent");
if(pszParent==NULL)
{
// Check if the scene node has already been created by a child
DISABLE_LOGMANAGER

try
{
pNode=mSceneMgr->getSceneNode(pszName);
} catch(...)
{
pNode=pSceneRoot->createChildSceneNode(pszName);
}

ENABLE_LOGMANAGER

} else
{
SceneNode *pParent=0;
DISABLE_LOGMANAGER

try
{
// Try to find parent scenenode
pParent=mSceneMgr->getSceneNode(pszParent);
} catch(...)
{
// We try to create the parent node as child of root node.
// Later when the parent (hopefully) is created, we can adjust it,
// if it is child of another node.
pParent=pSceneRoot->createChildSceneNode(pszParent);
}


try
{
// Check if the scene node has already been created by a child
// In this case we would have to change the parent.
pNode=mSceneMgr->getSceneNode(pszName);

// Get old parent (probably scene root)
SceneNode *pOldParent=pNode->getParentSceneNode();

// Remove this node
pOldParent->removeChild(pNode);

// Insert us as child on the "real" parent
pParent->addChild(pNode);
} catch(...)
{
pNode=pParent->createChildSceneNode(pszName);
}

ENABLE_LOGMANAGER
}

// Position
TiXmlElement* posElem=pElem->FirstChildElement("position");
if(posElem)
{
Vector3 pos;
pos.x = StringConverter::parseReal(posElem->Attribute("x"));
pos.y = StringConverter::parseReal(posElem->Attribute("y"));
pos.z = StringConverter::parseReal(posElem->Attribute("z"));
pNode->setPosition(pos);
}

// Rotation
TiXmlElement* rotElem=pElem->FirstChildElement("rotation");
if(rotElem)
{
pNode->setOrientation(
StringConverter::parseReal(rotElem->Attribute("w")),
StringConverter::parseReal(rotElem->Attribute("x")),
StringConverter::parseReal(rotElem->Attribute("y")),
StringConverter::parseReal(rotElem->Attribute("z")));

}

//Scale -- made by LonWolf
TiXmlElement* scaleElem=pElem->FirstChildElement("scale");
if(scaleElem)
{
Vector3 scalevect;
scalevect.x = StringConverter::parseReal(scaleElem->Attribute("x"));
scalevect.y = StringConverter::parseReal(scaleElem->Attribute("y"));
scalevect.z = StringConverter::parseReal(scaleElem->Attribute("z"));
pNode->scale(scalevect);
}

// Notify
if(mCallbacks)
mCallbacks->OnNodeCreate(pNode, pElem);

// Animation
TiXmlElement* animList=pElem->FirstChildElement("animations");
if(animList)
{
//
for (TiXmlElement* animElem = animList->FirstChildElement();
animElem != 0; animElem = animElem->NextSiblingElement())
{
// Get name of animation
const char *pszName=animElem->Attribute("name");

Animation *pAnim=0;
DISABLE_LOGMANAGER
try
{
pAnim=mSceneMgr->getAnimation(pszName);
} catch(...)
{
}
ENABLE_LOGMANAGER

// If this animation has not been created yet, we create it
if(pAnim==0)
{
float fLength=StringConverter::parseReal(animElem->Attribute("length"));
pAnim=mSceneMgr->createAnimation(pszName, fLength);
pAnim->setInterpolationMode(Animation::IM_LINEAR);
}

// Create animation track for this node
//AnimationTrack *pTrack = pAnim->createTrack(pAnim->getNumTracks()+1, pNode);

/*// Iterate all keyframes for this node
for (TiXmlElement* pKeyframeElem = animElem->FirstChildElement();
pKeyframeElem != 0; pKeyframeElem = pKeyframeElem->NextSiblingElement())
{
float fTime=StringConverter::parseReal(pKeyframeElem->Attribute("time"));
KeyFrame *pKeyFrame=pTrack->createKeyFrame(fTime);

// Position
TiXmlElement* posElem=pKeyframeElem->FirstChildElement("position");
if(posElem)
{
Vector3 trans;
trans.x = StringConverter::parseReal(posElem->Attribute("x"));
trans.y = StringConverter::parseReal(posElem->Attribute("y"));
trans.z = StringConverter::parseReal(posElem->Attribute("z"));
pKeyFrame->setTranslate(trans);
}

// Rotation
TiXmlElement* rotElem=pKeyframeElem->FirstChildElement("rotation");
if(rotElem)
{
Quaternion qRot;
qRot.x = StringConverter::parseReal(rotElem->Attribute("x"));
qRot.y = StringConverter::parseReal(rotElem->Attribute("y"));
qRot.z = StringConverter::parseReal(rotElem->Attribute("z"));
qRot.w = StringConverter::parseReal(rotElem->Attribute("w"));
pKeyFrame->setRotation(qRot);
}
}*/
}
}

return pNode;
}

Ogre::SceneNode *OSMScene::createNormalNode(TiXmlElement* pElem, Ogre::SceneNode* pSceneRoot)
{
SceneNode *pNode=0;

// Try to find the parent node
const char *pszName = pElem->Attribute("name");
if(pszName==NULL) return NULL;

// Check if this node has a parent
const char *pszParent = pElem->Attribute("parent");
if(pszParent==NULL)
{
// Check if the scene node has already been created by a child
DISABLE_LOGMANAGER

try
{
pNode=mSceneMgr->getSceneNode(pszName);
} catch(...)
{
pNode=pSceneRoot->createChildSceneNode(pszName);
}

ENABLE_LOGMANAGER

} else
{
SceneNode *pParent=0;
DISABLE_LOGMANAGER

try
{
// Try to find parent scenenode
pParent=mSceneMgr->getSceneNode(pszParent);
} catch(...)
{
// We try to create the parent node as child of root node.
// Later when the parent (hopefully) is created, we can adjust it,
// if it is child of another node.
pParent=pSceneRoot->createChildSceneNode(pszParent);
}


try
{
// Check if the scene node has already been created by a child
// In this case we would have to change the parent.
pNode=mSceneMgr->getSceneNode(pszName);

// Get old parent (probably scene root)
SceneNode *pOldParent=pNode->getParentSceneNode();

// Remove this node
pOldParent->removeChild(pNode);

// Insert us as child on the "real" parent
pParent->addChild(pNode);
} catch(...)
{
pNode=pParent->createChildSceneNode(pszName);
}

ENABLE_LOGMANAGER
}

// Position
TiXmlElement* posElem=pElem->FirstChildElement("position");
if(posElem)
{
Vector3 pos;
pos.x = StringConverter::parseReal(posElem->Attribute("x"));
pos.y = StringConverter::parseReal(posElem->Attribute("y"));
pos.z = StringConverter::parseReal(posElem->Attribute("z"));
pNode->setPosition(pos);
}

// Rotation
TiXmlElement* rotElem=pElem->FirstChildElement("rotation");
if(rotElem)
{
pNode->setOrientation(
StringConverter::parseReal(rotElem->Attribute("w")),
StringConverter::parseReal(rotElem->Attribute("x")),
StringConverter::parseReal(rotElem->Attribute("y")),
StringConverter::parseReal(rotElem->Attribute("z")));

}

//Scale -- made by LonWolf
TiXmlElement* scaleElem=pElem->FirstChildElement("scale");
if(scaleElem)
{
Vector3 scalevect;
scalevect.x = StringConverter::parseReal(scaleElem->Attribute("x"));
scalevect.y = StringConverter::parseReal(scaleElem->Attribute("y"));
scalevect.z = StringConverter::parseReal(scaleElem->Attribute("z"));
pNode->scale(scalevect);
}

// Notify
if(mCallbacks)
mCallbacks->OnNodeCreate(pNode, pElem);

// Animation
TiXmlElement* animList=pElem->FirstChildElement("animations");
if(animList)
{
//
for (TiXmlElement* animElem = animList->FirstChildElement();
animElem != 0; animElem = animElem->NextSiblingElement())
{
// Get name of animation
const char *pszName=animElem->Attribute("name");

Animation *pAnim=0;
DISABLE_LOGMANAGER
try
{
pAnim=mSceneMgr->getAnimation(pszName);
} catch(...)
{
}
ENABLE_LOGMANAGER

// If this animation has not been created yet, we create it
if(pAnim==0)
{
float fLength=StringConverter::parseReal(animElem->Attribute("length"));
pAnim=mSceneMgr->createAnimation(pszName, fLength);
pAnim->setInterpolationMode(Animation::IM_LINEAR);
}

// Create animation track for this node
//AnimationTrack *pTrack = pAnim->createTrack(pAnim->getNumTracks()+1, pNode);

/*// Iterate all keyframes for this node
for (TiXmlElement* pKeyframeElem = animElem->FirstChildElement();
pKeyframeElem != 0; pKeyframeElem = pKeyframeElem->NextSiblingElement())
{
float fTime=StringConverter::parseReal(pKeyframeElem->Attribute("time"));
KeyFrame *pKeyFrame=pTrack->createKeyFrame(fTime);

// Position
TiXmlElement* posElem=pKeyframeElem->FirstChildElement("position");
if(posElem)
{
Vector3 trans;
trans.x = StringConverter::parseReal(posElem->Attribute("x"));
trans.y = StringConverter::parseReal(posElem->Attribute("y"));
trans.z = StringConverter::parseReal(posElem->Attribute("z"));
pKeyFrame->setTranslate(trans);
}

// Rotation
TiXmlElement* rotElem=pKeyframeElem->FirstChildElement("rotation");
if(rotElem)
{
Quaternion qRot;
qRot.x = StringConverter::parseReal(rotElem->Attribute("x"));
qRot.y = StringConverter::parseReal(rotElem->Attribute("y"));
qRot.z = StringConverter::parseReal(rotElem->Attribute("z"));
qRot.w = StringConverter::parseReal(rotElem->Attribute("w"));
pKeyFrame->setRotation(qRot);
}
}*/
}
}

return pNode;
}

// Set Scene Properties
void OSMScene::setSceneProperties(TiXmlElement* sceneProp) {
assert(sceneProp);

// Scene manager
TiXmlElement* sceneMgrElem = sceneProp->FirstChildElement("sceneManager");
int type = Ogre::StringConverter::parseInt(sceneMgrElem->Attribute("type"));
Ogre::SceneType sceneType = static_cast<Ogre::SceneType>(type);
mSceneMgr = Ogre::Root::getSingleton().createSceneManager(sceneType);
assert(mSceneMgr);

const char* worldGeometry = sceneMgrElem->Attribute("worldGeometry");
if(worldGeometry != NULL)
mSceneMgr->setWorldGeometry(worldGeometry);

// Background Color
TiXmlElement* colorElem = sceneProp->FirstChildElement("bkgcolor");
if(colorElem && mWindow)
{
Ogre::ColourValue color;
color.r = StringConverter::parseReal(colorElem->Attribute("r"));
color.g = StringConverter::parseReal(colorElem->Attribute("g"));
color.b = StringConverter::parseReal(colorElem->Attribute("b"));

mWindow->getViewport(0)->setBackgroundColour(color);
}

// Ambient light Color
colorElem = sceneProp->FirstChildElement("lightColor");
if(colorElem)
{
Ogre::ColourValue color;
color.r = StringConverter::parseReal(colorElem->Attribute("r"));
color.g = StringConverter::parseReal(colorElem->Attribute("g"));
color.b = StringConverter::parseReal(colorElem->Attribute("b"));

mSceneMgr->setAmbientLight(color);
}

// Scene shadows
TiXmlElement* shadowsElem = sceneProp->FirstChildElement("shadowTechnique");
if(shadowsElem)
{
int type = Ogre::StringConverter::parseInt(shadowsElem->Attribute("type"));
Ogre::ShadowTechnique shadowType = static_cast<Ogre::ShadowTechnique>(type);

mSceneMgr->setShadowTechnique(shadowType);

int tex_size = Ogre::StringConverter::parseInt(shadowsElem->Attribute("tex_size"));
int tex_count = Ogre::StringConverter::parseInt(shadowsElem->Attribute("tex_count"));

mSceneMgr->setShadowTextureSettings(tex_size, tex_count);

// Shadow Color
TiXmlElement* colorElem = shadowsElem->FirstChildElement("color");
if(colorElem)
{
Ogre::ColourValue color;
color.r = StringConverter::parseReal(colorElem->Attribute("r"));
color.g = StringConverter::parseReal(colorElem->Attribute("g"));
color.b = StringConverter::parseReal(colorElem->Attribute("b"));

mSceneMgr->setShadowColour(color);
}
}

// Scene sky
TiXmlElement* skyElem = sceneProp->FirstChildElement("skyTechnique");
if(skyElem)
{
int type = Ogre::StringConverter::parseInt(skyElem->Attribute("type"));
Ogre::String materialName = skyElem->Attribute("material");

if(materialName != " ") {
bool drawFirst = Ogre::StringConverter::parseBool(skyElem->Attribute("drawFirst"));
float tiling = Ogre::StringConverter::parseReal(skyElem->Attribute("tiling"));
float scale = Ogre::StringConverter::parseReal(skyElem->Attribute("scale"));
float dist = Ogre::StringConverter::parseReal(skyElem->Attribute("dist"));
float bow = Ogre::StringConverter::parseReal(skyElem->Attribute("bow"));
int xSegments = Ogre::StringConverter::parseInt(skyElem->Attribute("xSegments"));
int ySegments = Ogre::StringConverter::parseInt(skyElem->Attribute("ySegments"));
Ogre::Quaternion quat(Ogre::Quaternion::IDENTITY);
Ogre::Plane plane;
plane.d = dist;
plane.normal = -(Ogre::Vector3::UNIT_Y);

switch(type) {

case SCENE_SKYPLANE:

mSceneMgr->setSkyPlane(true, plane, materialName, scale,
tiling, drawFirst, bow, xSegments, ySegments);

mSceneMgr->setSkyBox(false, "");
mSceneMgr->setSkyDome(false, "");

break;

case SCENE_SKYBOX:

mSceneMgr->setSkyBox(true, materialName, dist, drawFirst, quat);
mSceneMgr->setSkyPlane(false, plane, "");
mSceneMgr->setSkyDome(false, "");

break;

case SCENE_SKYDOME:

mSceneMgr->setSkyDome(true, materialName, bow, tiling, dist,
drawFirst, quat, xSegments, ySegments);

mSceneMgr->setSkyPlane(false, plane, "");
mSceneMgr->setSkyBox(false, "");

break;

}
}
}

}

// Create all entities in scene
void OSMScene::createNormalEntities(TiXmlElement* pEntityNode, Ogre::SceneNode* pSceneRoot)
{
mLastCreatedEntityList.clear();
// Iterate all meshes, creating them.
for (TiXmlElement* pMeshElem = pEntityNode->FirstChildElement();
pMeshElem != 0; pMeshElem = pMeshElem->NextSiblingElement())
{
// Ogre could cast an exception, in which case we just try to
// continue reading the other meshes
try
{
const char *pszName = pMeshElem->Attribute("name");
const char *pszFileName = pMeshElem->Attribute("filename");


const char *pszBuildTangent=pMeshElem->Attribute("buildtangents");
if(pszBuildTangent!=NULL && stricmp(pszBuildTangent, "yes")==0)
{
//LOGINFO "Building tangent vectors for %s", pszName);
MeshPtr pMesh = MeshManager::getSingleton().load(pszFileName,
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY,
HardwareBuffer::HBU_STATIC_WRITE_ONLY,
true, true); //so we can still read it

// Build tangent vectors, all our meshes use only 1 texture coordset
unsigned short src, dest;
if(!pMesh->suggestTangentVectorBuildParams(src, dest))
{
pMesh->buildTangentVectors(src, dest);
}
}


// try to create the mesh
Entity *pEntity = mSceneMgr->createEntity(pszName, pszFileName);
if(pEntity==0) continue;

// Check if the object should cast shadows
const char *pszCastShadows=pMeshElem->Attribute("CastShadows");
if(pszCastShadows && stricmp(pszCastShadows, "no")==0)
pEntity->setCastShadows(false);
else
pEntity->setCastShadows(true);

// Create node with full information
SceneNode *pObjNode=createNormalNode(pMeshElem, pSceneRoot);

// Attach the mesh entity to node
pObjNode->attachObject(pEntity);

// Notify
if(mCallbacks)
mCallbacks->OnEntityCreate(pEntity, pMeshElem);

// Add to entity list
mEntities.push_back(pEntity);
mLastCreatedEntityList.push_back(pEntity);
} catch(...)
{
continue;
}
}
}
void OSMScene::createEntities(TiXmlElement* pEntityNode, Ogre::SceneNode* pSceneRoot)
{
mLastNodes.clear();
mLastCreatedEntityList.clear();
// Iterate all meshes, creating them.
for (TiXmlElement* pMeshElem = pEntityNode->FirstChildElement();
pMeshElem != 0; pMeshElem = pMeshElem->NextSiblingElement())
{
// Ogre could cast an exception, in which case we just try to
// continue reading the other meshes
try
{
const char *pszName = pMeshElem->Attribute("name");
const char *pszFileName = pMeshElem->Attribute("filename");


const char *pszBuildTangent=pMeshElem->Attribute("buildtangents");
if(pszBuildTangent!=NULL && stricmp(pszBuildTangent, "yes")==0)
{
//LOGINFO "Building tangent vectors for %s", pszName);
MeshPtr pMesh = MeshManager::getSingleton().load(pszFileName,
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY,
HardwareBuffer::HBU_STATIC_WRITE_ONLY,
true, true); //so we can still read it

// Build tangent vectors, all our meshes use only 1 texture coordset
unsigned short src, dest;
if(!pMesh->suggestTangentVectorBuildParams(src, dest))
{
pMesh->buildTangentVectors(src, dest);
}
}


// try to create the mesh
Entity *pEntity = mSceneMgr->createEntity(pszName, pszFileName);
if(pEntity==0) continue;

// Check if the object should cast shadows
const char *pszCastShadows=pMeshElem->Attribute("CastShadows");
if(pszCastShadows && stricmp(pszCastShadows, "no")==0)
pEntity->setCastShadows(false);
else
pEntity->setCastShadows(true);

// Create node with full information
SceneNode *pObjNode=createNode(pMeshElem, pSceneRoot);

// Attach the mesh entity to node
pObjNode->attachObject(pEntity);
mLastNodes.push_back(pObjNode);

OgreNewt::Collision* colDotScen = new OgreNewt::CollisionPrimitives::TreeCollision(mWorld, pObjNode, true);
OgreNewt::Body* bodDotScen = new OgreNewt::Body( mWorld, colDotScen );
delete colDotScen;
bodDotScen->attachToNode(pObjNode);
bodDotScen->setPositionOrientation(pObjNode->getPosition(), pObjNode->getOrientation());

// Notify
if(mCallbacks)
mCallbacks->OnEntityCreate(pEntity, pMeshElem);

// Add to entity list
mEntities.push_back(pEntity);
mLastCreatedEntityList.push_back(pEntity);
} catch(...)
{
continue;
}
}
}

// Create all Lights in scene
void OSMScene::createLights(TiXmlElement* pLightNode, Ogre::SceneNode* pSceneRoot)
{
// Iterate all Lights, creating them. We do not attach them yet, since
// we need to make sure all potential parent entities have been created.
for (TiXmlElement* pLightElem = pLightNode->FirstChildElement();
pLightElem != 0; pLightElem = pLightElem->NextSiblingElement())
{
// Ogre could cast an exception, in which case we just try to
// continue reading the other Lights
try
{
const char *pszName = pLightElem->Attribute("name");

Light *pLight=mSceneMgr->createLight(pszName);
if(pLight==0) continue;

// Figure out which type of light we are using
const char *pszType=pLightElem->Attribute("type");
if(stricmp(pszType, "omni")==0)
{
pLight->setType(Ogre::Light::LightTypes::LT_POINT);
} else if(stricmp(pszType, "spot")==0)
{
pLight->setType(Ogre::Light::LightTypes::LT_SPOTLIGHT);
pLight->setSpotlightRange(
Degree(StringConverter::parseReal(pLightElem->Attribute("hotspot"))),
Degree(StringConverter::parseReal(pLightElem->Attribute("falloff"))));
pLight->setDirection(0,0,-1);

} else if(stricmp(pszType, "directional")==0)
{
pLight->setType(Ogre::Light::LightTypes::LT_DIRECTIONAL);
}

// Check if the light should be on
const char *pszOn=pLightElem->Attribute("on");
if(pszOn!=0 && stricmp(pszOn, "true")==0)
pLight->setVisible(true);
else
pLight->setVisible(false);

// Check if the object should cast shadows
const char *pszCastShadows=pLightElem->Attribute("CastShadows");
if(pszCastShadows && stricmp(pszCastShadows, "no")==0)
pLight->setCastShadows(false);
else
pLight->setCastShadows(true);

//intensity - LonWolf

TiXmlElement* posElem=pLightElem->FirstChildElement("position");
if(posElem)
{
Vector3 posvect;
posvect.x=StringConverter::parseReal(posElem->Attribute("x"));
posvect.y=StringConverter::parseReal(posElem->Attribute("y"));
posvect.z=StringConverter::parseReal(posElem->Attribute("z"));
pLight->setPosition(posvect);
}

// Diffuse Color
TiXmlElement* colorElem=pLightElem->FirstChildElement("color");
if(colorElem)
{
pLight->setDiffuseColour(
StringConverter::parseReal(colorElem->Attribute("r")),
StringConverter::parseReal(colorElem->Attribute("g")),
StringConverter::parseReal(colorElem->Attribute("b")));
}

// Specular Color
TiXmlElement* specularElem=pLightElem->FirstChildElement("specular");
if(specularElem)
{
pLight->setSpecularColour(
StringConverter::parseReal(specularElem->Attribute("r")),
StringConverter::parseReal(specularElem->Attribute("g")),
StringConverter::parseReal(specularElem->Attribute("b")));
}

// Create node with full information
SceneNode *pLightNode=createNode(pLightElem, pSceneRoot);

// Attach the Light entity to node
pLightNode->attachObject(pLight);

// Target
TiXmlElement* targetElem=pLightElem->FirstChildElement("target");
if(targetElem)
{
// Create node with full information
SceneNode *pTargetNode=createNode(targetElem, pSceneRoot);
pLightNode->setAutoTracking(true, pTargetNode);
}

// Notify
if(mCallbacks)
mCallbacks->OnLightCreate(pLight, pLightElem);

// Add to light list
mLights.push_back(pLight);
} catch(...)
{
continue;
}
}
}


// Create all Cameras in scene
void OSMScene::createCameras(TiXmlElement* pCameraNode, Ogre::SceneNode* pSceneRoot)
{
// Iterate all Cameras, creating them. We do not attach them yet, since
// we need to make sure all potential parent entities have been created.
for (TiXmlElement* pCameraElem = pCameraNode->FirstChildElement();
pCameraElem != 0; pCameraElem = pCameraElem->NextSiblingElement())
{
// Ogre could cast an exception, in which case we just try to
// continue reading the other Cameras
try
{
const char *pszName = pCameraElem->Attribute("name");

// Create camera
Camera *pCamera=mSceneMgr->createCamera(pszName);
if(pCamera==0) continue;

// Set Field of View on camera
pCamera->setFOVy(Radian(StringConverter::parseReal(pCameraElem->Attribute("FOV"))));
pCamera->setNearClipDistance(5);

// Create node with full information
SceneNode *pCameraNode=createNode(pCameraElem, pSceneRoot);

// Attach the Camera entity to node
pCameraNode->attachObject(pCamera);

// Target
TiXmlElement* targetElem=pCameraElem->FirstChildElement("target");
if(targetElem)
{
// Create node with full information
SceneNode *pTargetNode=createNode(targetElem, pSceneRoot);
pCameraNode->setAutoTracking(true, pTargetNode);
}

// Notify
if(mCallbacks)
mCallbacks->OnCameraCreate(pCamera, pCameraElem);

// Add to camera list
mCameras.push_back(pCamera);
} catch(...)
{
continue;
}
}
}


use oScene.createOgreNewtScene() to create scene with bodyes and createOgreScene to just load the meshes visually. just copy paste and it should work

Operator

03-09-2006 15:01:12

Thx a lot but it wasn't needed! Now I know what was the problem! I had bad files included and the OSMScene header file wasn't that I edited... silly me... :oops: Now it's working fine! Thx so much for help lonwolf! :wink:

lonwolf

03-09-2006 19:42:59

well just in case my files are better than the original so you can choose what type of osms to load. Normal meshes or the meshes and newton bodys :D so it's kinda handy. Also i included a vector containing all the bodyes (usefull if uure using RayCasting , ey 8) ) hope it helps some other ppl looking to use osm and ONewt. The pretty part is you can modify it to do whatever you want. I think in one of my functions you can also pass a Vector3 trans and a scale to perform manually :)

with pleasure and anytime Operator ;) im back to my rpg making. Till next time good luck mate!

r1cky17

23-10-2007 05:51:26

to : lonwolf

i use your OSMscene.h, it's work for the createOgreNewtScene.

this is your code to add the collision

OgreNewt::Collision* colDotScen = new OgreNewt::CollisionPrimitives::TreeCollision(mWorld, pObjNode, true);
OgreNewt::Body* bodDotScen = new OgreNewt::Body( mWorld, colDotScen );
delete colDotScen;
bodDotScen->attachToNode(pObjNode);
bodDotScen->setPositionOrientation(pObjNode->getPosition(), pObjNode->getOrientation());


in my case, i want to load another scene.
so the collision from the old scene is not destroy.

can you help me how to destroy the body collision?

lonwolf

23-10-2007 17:33:06

just get the body by its name (or store it somewhere after it was created like a class or an array of bodies) and then destroy it.. you can do that manually in your code not in ogreosmloader.cpp

r1cky17

24-10-2007 08:52:29

thx for your reply :) , but i don't know how to get the name of the body or to store that in array of bodies.
i already try to do 2 thing about that, and the result is still not work and error.

yup, before using your ogreosmloader code, i have try to destroy that and i only done in destroy the entity, so the entity not shown again if i load another scene. but in destroy the bodies i can't find how to do that

here is the code to load the entity

Entity *pEntity = mSceneMgr->createEntity(pszName, pszFileName);


here is to destroy that entity

mSceneMgr->destroyEntity(pszFileName);


in creating the entity, it pass the name.
but to load the body, it's not pass the name of the body

OgreNewt::Body* bod = new OgreNewt::Body( m_World, col );


can you help me how to destroy that?
or is there another way to create the body but contain the name of the body?

thx for the help

lonwolf

24-10-2007 17:25:18

Body* bod = new OgreNewt

when you do this, a part oy your memory (RAM) uses some bits to store a Body type variable, that is an object of OgreNewt::Body class. Body* bod means 'bod' contains the adress of the object that is created in the memory using the 'new' operator (new creates a reserved space in memory and returns the adress). That specific memory is released when you call delete bod; - just as you create a collision body, use it to create the body, but after that you do not need to keep the collision body anymore so you destroy it using delete operator as in:
OgreNewt::Collision* colDotScen = new OgreNewt::CollisionPrimitives::TreeCollision( mWorld, pObjNode, true );
delete colDotScen;

store the body* pointer somewhere (watch out for performance issues like keep 1 scene's body adress at a time and not the entire game's because you will occupy ram without practical use) then when you want to destroy the body simply
delete bod;
also, when you have dynamic bodies you attach them to a ogreNode body->attachToNode(SceneNode* node) then you can destroy the node in the same manner you destroy the entity, and that would destroy the body too (in theory). so either destroy it via ogreNode or via delete body; operator

hope that puts your thoughts in order :D

r1cky17

27-10-2007 08:11:21

thx lonwolf,
i can delete body right now :D

lonwolf

27-10-2007 14:57:22

no problem :) try and learn more about c++ (tips and tricks) to avoid further problems

r1cky17

12-11-2007 07:55:58

ask again.. :)
i delete the entity and the sceneNode but it doesn't deleted.
any idea?
this is the code that i use to delete the entity and the sceneNode
http://www.ogre3d.org/phpBB2addons/view ... 3249#33249