I used to use Irrlicht for my paged terrain system but it would just crash randomly so I decided to go for bullet and I couldn't find a code to cook a Terrain to a Bullet triangle mesh, so I made one and decided to post it for people who will need it in the future !
Here it is :
Code: Select all
void createHeightField(scene::ITerrainSceneNode* node)
{
btVector3 vertices[3];
s32 j,k;
btTriangleMesh * mTriMesh = new btTriangleMesh();
irr::scene::CDynamicMeshBuffer* buffer = 0;
buffer = new irr::scene::CDynamicMeshBuffer(irr::video::EVT_2TCOORDS, irr::video::EIT_16BIT);
node->getMeshBufferForLOD(*buffer, 0);
const irr::u32 vertexCount = buffer->getVertexCount();
const irr::u32 indexCount = buffer->getIndexCount();
irr::video::S3DVertex2TCoords* mb_vertices = (irr::video::S3DVertex2TCoords*)buffer->getVertexBuffer().getData();
u16* mb_indices = buffer->getIndices();
for(j=0;j<indexCount;j+=3)
{
for (k=0;k<3;k++)
{
s32 index = mb_indices[j+k];
vertices[k] = btVector3(
mb_vertices[index].Pos.X*node->getScale().X,
mb_vertices[index].Pos.Y*node->getScale().Y,
mb_vertices[index].Pos.Z*node->getScale().Z);
}
mTriMesh->addTriangle(vertices[0], vertices[1], vertices[2]);
}
buffer->drop();
btCollisionShape* mShape = new btBvhTriangleMeshShape(mTriMesh, true);
btDefaultMotionState* state = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),
btVector3(node->getPosition().X,node->getPosition().Y,node->getPosition().Z)));
btRigidBody* mRigidBody = new btRigidBody(0, state, mShape, btVector3(0, 0, 0));
mRigidBody->setCollisionFlags(mRigidBody->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
mDynamicsWorld->addRigidBody(mRigidBody);
}