Getting the terrain mesh

calsmurf2904

24-10-2008 21:45:27

Hello,
I am currently creating a Map Editor for my game but since my game uses meshes instead of heightmaps I need to be able to export the mesh.
I already found the a class in the wiki code snippets page to export the mesh to a file so the only thing I now need is getting the mesh.
I thought doing this :

Ogre::String mName = "ETTerrain";
mTerrainMgr = new TerrainManager(mSceneMgr,mName);
...Removed this piece since it was not related to the topic...
mTerrainMgr->createTerrain(mTerrainInfo);
Ogre::String mNodeName = mName + "/Terrain";
Ogre::SceneNode* mTerrainNode = mSceneMgr->getSceneNode(mNodeName);

However when I try this :

mCamera->setPosition(mTerrainNode->getPosition() + 10);

Then I don't see anything while I need to be at :
TerrainPosition + Vector3(10,10,10);
(I am doing the position thing to see If i get the correct node)
Does anyone knows how to get the correct scenenode ?

Thanks in advance,
Calsmurf2904

CABAListic

24-10-2008 22:07:25

Looks correct, if it doesn't crash, then it's the correct scene node. You might need to move up further or rotate the camera, because the origin of the terrain is at one of its borders, so if you look in the wrong direction, you won't see anything.

However, I'm not sure what you're going to do. ETM does not use the Mesh class, so if your export snippet relies on the mesh class, it's not going to work. I think it would be easier to take the terrain heightmap and create the mesh yourself and then export it in any way you like.

calsmurf2904

24-10-2008 22:57:56

mmm.....I thought the attachedObjects to the scenenode are the pieces of the terrain and If they are entity's or Manualobjects they will have meshes wich you then can request.

CABAListic

25-10-2008 07:41:56

No, they are neither. They are Renderables with custom vertex buffers (which are private attributes and not accessible).

calsmurf2904

25-10-2008 08:56:19

ok...so how will I go and create a mesh from the heightmaps ?
I tried something and it did save but it keeps failing at assertion :
something_indexes > 0
when I load the mesh in.

calsmurf2904

25-10-2008 11:51:46

This is my code that I am using to save the mesh :

void saveMesh()
{
ManualObject* mMan = mSceneMgr->createManualObject("Terrain");
mMan->begin("ETTerrainMaterial",RenderOperation::OT_TRIANGLE_LIST);
for(int x = 1;x<mTerrainInfo.getWidth();x++)
{
for(int y = 1;y<mTerrainInfo.getHeight();y++)
{
mMan->position(Vector3(x-1,mTerrainInfo.getHeightAt(x-1,y-1),y-1));
mMan->position(Vector3(x,mTerrainInfo.getHeightAt(x,y),y));
mMan->position(Vector3(x-1,mTerrainInfo.getHeightAt(x-1,y),y));

mMan->position(Vector3(x-1,mTerrainInfo.getHeightAt(x-1,y-1),y-1));
mMan->position(Vector3(x,mTerrainInfo.getHeightAt(x,y-1),y-1));
mMan->position(Vector3(x,mTerrainInfo.getHeightAt(x,y),y));
}
}
mMan->end();
Ogre::MeshPtr mMeshPtr = mMan->convertToMesh("TerrainMesh");
Ogre::Mesh* mMesh = mMeshPtr.getPointer();
Ogre::MeshSerializer* mMeshSeri = new Ogre::MeshSerializer();
mMeshSeri->exportMesh(mMesh,"Test.mesh");
mSceneMgr->destroyManualObject("Terrain");
delete mMesh;
delete mMeshSeri;
}

And this is the error I get when loading it (it does save to almost 1.2 mb)
http://i37.photobucket.com/albums/e52/calsmurf2904/error-1.jpg

Edit:
nvm.wasn't calling index so the indexes weren't created.
This is the fixed code :

void saveMesh(Ogre::String mFolder)
{
ManualObject* mMan = mSceneMgr->createManualObject("Terrain");
mMan->begin("ETTerrainMaterial",RenderOperation::OT_TRIANGLE_LIST);
int i = 0;
for(int x = 1;x<mTerrainInfo.getWidth();x++)
{
for(int y = 1;y<mTerrainInfo.getHeight();y++)
{
mMan->index(i);
mMan->position(Vector3(x-1,mTerrainInfo.getHeightAt(x-1,y-1),y-1));
mMan->position(Vector3(x,mTerrainInfo.getHeightAt(x,y),y));
mMan->position(Vector3(x-1,mTerrainInfo.getHeightAt(x-1,y),y));
i++;

mMan->index(i);
mMan->position(Vector3(x-1,mTerrainInfo.getHeightAt(x-1,y-1),y-1));
mMan->position(Vector3(x,mTerrainInfo.getHeightAt(x,y-1),y-1));
mMan->position(Vector3(x,mTerrainInfo.getHeightAt(x,y),y));
i++;
}
}
mMan->end();
Ogre::MeshPtr mMeshPtr = mMan->convertToMesh("TerrainMesh");
Ogre::Mesh* mMesh = mMeshPtr.getPointer();
Ogre::MeshSerializer* mMeshSeri = new Ogre::MeshSerializer();
mMeshSeri->exportMesh(mMesh,mFolder + "/" + mFolder + ".mesh");
mSceneMgr->destroyManualObject("Terrain");
delete mMesh;
delete mMeshSeri;
}