Combining trimeshes

syedhs

01-09-2006 10:42:57

Hi,

2 or 3 months ago, I have managed to combine trimeshes of many objects such as trees, houses only with ONE major bug - there is a weird thin kind of 'ribbon' collision connecting among those meshes (if you turn the debugging on via world's setShowDebugObjects method). I know that this is due to some error whilst merging those trimeshes (vertices and indices), and since it is not critical I turn the code off for sometime.. and now I am coming back to fix the code!

However.. I now always get the error assertion from Ode's dGeomTriMeshDataBuildSimple. The code is :-

Structure Definition:


struct TriMeshStruct {
int verticesCnt;
int indicesCnt;
const Vector3* vertices;
unsigned* indices;
TriMeshStruct(int v, int i, const Vector3* pv, const unsigned* pi)
{
verticesCnt = v;
indicesCnt = i;
vertices = pv;
indices = (unsigned*)pi;
}
};



Trimeshes construction

std::vector<TriMeshStruct*>tmsContainer;
int allVerticesCnt = 0;
int allIndicesCnt = 0;
..
.. *snip*
while (*looping thru meshes) {
Entity* col = mSceneMgr->createEntity("collision_"+name, collisionName);
OgreOde::EntityInformer ei(col, node->_getFullTransform());
ei.setDeleteOnExit(false); // this is my own modification to OgreOde -> a flag not to delete the vertices+indices upon execution of entityinformer's destructor
allVerticesCnt += ei.getVertexCount();
allIndicesCnt += ei.getIndexCount();

TriMeshStruct* tms = new TriMeshStruct(
ei.getVertexCount(),
ei.getIndexCount(),
ei.getVertices(),
ei.getIndices());
tmsContainer.push_back(tms);
}




Merging trimeshes

Vector3* vertices = new Vector3[allVerticesCnt];
int* indices = new int[allIndicesCnt];
Vector3* pv = vertices;
int *pi = indices;

int offset = 0;
int i;

std::vector<TriMeshStruct*>::iterator iterTris;
for (iterTris = tmsContainer.begin(); iterTris != tmsContainer.end(); ++iterTris) {
TriMeshStruct* tms = *iterTris;
// but the indices have to be incremented first..
for (i = 0; i < tms->indicesCnt; ++i)
tms->indices[i] += offset;
memcpy(pv, tms->vertices, tms->verticesCnt);
memcpy(pi, tms->indices, tms->indicesCnt);
pv += tms->verticesCnt;
pi += tms->indicesCnt;

// update indices offset
offset += tms->verticesCnt;
// and then dont forget to delete the structure
delete tms;
}

// okay now we have the vertices lets built the trimeshes
OgreOde::TriangleMeshGeometry* triGeom = new OgreOde::TriangleMeshGeometry((const Vector3*)vertices, allVerticesCnt, (const unsigned*)indices, allIndicesCnt);
delete [] vertices;
delete [] indices;


The assertion occurs at the construction of triGeom above. Any ideas on where the code goes wrong?

p/s: I am not aware of this feature in OgreOde - Tuan, I think this is nice addition to OgreOde - merging all those static trimeshes because large performance increase can be had by merging even 30 static trimeshes into one!

tuan kuranes

01-09-2006 17:10:49

I added support for multiple entities in EntityInformer.
Please Have a Try.


OgreOde::EntityInformer ei;

while (*looping thru meshes) {
Entity* col = mSceneMgr->createEntity("collision_"+name, collisionName);
ei.addEntity (col, , node->_getFullTransform())
}
OgreOde::TriangleMeshGeometry* triGeom = ei.createStaticTriangleMesh(_space)


Let me know how it goes.

syedhs

01-09-2006 20:12:35

Downloading.. will report in a few hours time :wink:

Edit: Flawless, thanks. Btw, I merely copy paste into OgreOde dagon compatible as I am afraid of breaking anything between versions. :wink: