I'm programming a 3D space simulator with OpenGL. I wrote a .obj file loader and i have the total number of vertices/normals/texCoord/triangles and even indices. Currently i skip the external files for materials and textures though.
But I can still render the mesh out nicely in the window nonetheless so it should not be a problem for the trimesh part.
I tried multiple ways to use the btBvhTriangleMeshShape function. Searching through forums and stuff like that all points back to refer to concave demo or vehicle demo. Even so i had problem getting the trimesh collision to work. I followed and copied out the required steps. Since everyone in the forum kinda got it to work i feel kinda depress not to make any progress after a week T_T.
Anyways these are the ways i tried to make the trimesh rigidbody. Initially i attempted with a complex model but after having the impression that it's my model might be fualty i switch to a simple box model.
Method 1:
Code: Select all
int vertStride = sizeof(btVector3);
int indexStride = 3*sizeof(int);
int totalTriangles = 0;
int totalVertices = 0;
int TempTotalVertices = 0;
int index=0;
// Vertices clean up, arrange them into btVector3
gVertices = new btVector3[ObjExtend.mNumVertices];
for (int i = 0; i < (ObjExtend.mNumVertices); i++)
{
gVertices[i] = btVector3(ObjExtend.mVertex[index],ObjExtend.mVertex[index+1],ObjExtend.mVertex[index+2]);
index += 3;
}
index = 0;
// Another clean up, in Obj i took out the indices of the Vertices/UV/Normals
// Doing strides of 3 to get only the indices for the vertices
gIndices = new int[totalTriangles*3];
for (int i = 0; i < (ObjExtend.mNumIndices); i++)
{
gIndices[i] = ObjExtend.mIndices[index];
index += 3;
}
index = 0;
indexVertexArray = new btTriangleIndexVertexArray(
totalTriangles,
gIndices,
indexStride,
ObjExtend.mNumVertices,(btScalar*) &gVertices[0].x(),vertStride);
bool useQuantizedAabbCompression = true;
btVector3 aabbMin(-1000,-1000,-1000);
btVector3 aabbMax(1000,1000,1000);
trimeshShape = new btBvhTriangleMeshShape(indexVertexArray,useQuantizedAabbCompression,aabbMin,aabbMax);
collisionShapesArray.push_back(trimeshShape);
// Tried with and without serialization
void* buffer = 0;
int numBytes = trimeshShape->getOptimizedBvh()->calculateSerializeBufferSize();
buffer = btAlignedAlloc(numBytes,16);
bool swapEndian = false;
trimeshShape->getOptimizedBvh()->serialize(buffer,numBytes,swapEndian);
FILE* file = fopen("bvh.bin","wb");
fwrite(buffer,1,numBytes,file);
fclose(file);
btAlignedFree(buffer);
//btCollisionShape* ObjectShape, has been intialized, it was used for a sphere shape, collisionShapesArray[0] holds
//a sphere
ObjectShape = collisionShapesArray[1];
mass = 0;
trans.setIdentity();
staticBody = new btRigidBody(mass,0,ObjectShape,btVector3(0,0,0));
staticBody->setWorldTransform(trans);
dynamicsWorld->addRigidBody(staticBody);
staticBody->setCollisionFlags(staticBody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);//STATIC_OBJECT);
Method 2 i create a btTriangleMesh using the loaded vertices and indices. Then i pass it into btBvhTriangleMeshShape for the first parameter replacing the indexVertexArray.
Code: Select all
btCollisionShape* terrainShape = new btBvhTriangleMeshShape(pTriMesh, true, false);
collisionShapesArray.push_back(terrainShape);
btTransform trans;
trans.setIdentity();
trans.setOrigin(btVector3(0, 0, 0));
mass = 0;
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0, 0, 0);
if (isDynamic)
terrainShape->calculateLocalInertia(mass,localInertia);
btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,terrainShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
dynamicsWorld->addRigidBody(body);
Am i missing anything? Or am i missing lots of things. I'm not sure since thats the stuff i figured i need from the examples and they somehow having the same problem whichever i tried.
I can post my codes here but tell me which part thats the likelyhood to cause problems since my file is kinda huge.
Still any simplified example will be of great help, seeing those #ifdef and #endif makes me wanna cry T_T, though i would wanna do the same one day since it looks leet.