Here is the part of my code the generates the mesh:
Code: Select all
void WorldChunk::generatePhysics()
{
if( mPhysicsBody != NULL ) {
OpencraftCore::Singleton->getPhysicsWorld()->removeRigidBody( mPhysicsBody );
delete mPhysicsState;
delete mPhysicsBody;
delete mPhysicsShape;
delete mPhysicsMesh;
}
if( mGeometry->vertexCount > 5 && mGeometry->edgeCount > 5 ) {
btTriangleIndexVertexArray* meshInterface = new btTriangleIndexVertexArray;
btIndexedMesh part;
int vertSize = sizeof( GLvertex );
int indexSize = sizeof( GLedge );
part.m_vertexBase = (const unsigned char*)&mGeometry->vertexData[0].x;
part.m_vertexStride = vertSize;
part.m_numVertices = mGeometry->vertexCount;
part.m_vertexType = PHY_FLOAT;
part.m_triangleIndexBase = (const unsigned char*)&mGeometry->edgeData[0];
part.m_triangleIndexStride = indexSize * 3;
part.m_numTriangles = mGeometry->edgeCount/3;
part.m_indexType = PHY_SHORT;
meshInterface->addIndexedMesh( part, PHY_SHORT );
mPhysicsShape = new btBvhTriangleMeshShape( meshInterface, true );
btTransform t;
t.setOrigin( btVector3( getX() * CHUNK_WIDTH, getY() * CHUNK_HEIGHT, getZ() * CHUNK_WIDTH ) );
mPhysicsState = new btDefaultMotionState( t );
btRigidBody::btRigidBodyConstructionInfo ci( 0.f, mPhysicsState, mPhysicsShape, btVector3(0,0,0) );
mPhysicsBody = new btRigidBody( ci );
mPhysicsBody->setCollisionFlags( mPhysicsBody->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT );
OpencraftCore::Singleton->getPhysicsWorld()->addRigidBody( mPhysicsBody );
}
}
Code: Select all
struct GLvertex {
float x, y, z;
float nx, ny, nz;
float u0, v0;
float r, g, b;
int padding[5];
};
typedef unsigned short GLedge;