What I do is:
Load a standard wavefront obj file into memory
For every vertex found in the file, store it in a 'bank' of memory
When I reach the end of the file, I know how many vertices the mesh has, so I malloc the vertex cloud to be the correct size
Then go back through all of my banks, copying their data into the vertex cloud (console output confirms this is working properly)
Here I call NewtonCreateConvexHull() but it crashes. So I must have made a small mistake somewhere, but I cannot find where
The exact code I use is:
- Code: Select all
NewtonCollision* CreateConvexMesh(std::string MeshFileName)
{
std::cout << "Creating convex mesh from file " << MeshFileName << "\n";
if(MeshFileName.compare("") != 0)
{
NewtonCollision* ReturnedCollision;
unsigned long VertexCount = 0;
dFloat *VertexList; //Pointer to an array
unsigned long MeshFileBank = mlib::bank::FileToBank(MeshFileName,0);
unsigned long ReadOffset = 0;
unsigned long VertexDataHead = 0;
unsigned long VertexDataTail = 0; //FIFO is slow especially when it has to iterate through a long queue many times per second.
//We can speed it up by jumping straight to the last element
bool EndOfVertexList = false;
do
{
//Read a string from the bank, terminated by ascii character 13
std::string RecoveredString = mlib::bank::PeekString(MeshFileBank,ReadOffset,(unsigned char)13);
//If there are 4 words on the line, and the first word is the letter 'v'
if((mlib::string::CountWords(RecoveredString) == 4) && (mlib::string::GetWord(RecoveredString,1).compare("v") == 0))
{
//Add this vertex entry into a list of banks, fifo style.
unsigned long VertexBankEntry = mlib::bank::CreateBank(20);
mlib::bank::PokeFloat(VertexBankEntry,0,(float)atof(mlib::string::GetWord(RecoveredString,2).c_str()));
mlib::bank::PokeFloat(VertexBankEntry,4,(float)atof(mlib::string::GetWord(RecoveredString,3).c_str()));
mlib::bank::PokeFloat(VertexBankEntry,8,(float)atof(mlib::string::GetWord(RecoveredString,4).c_str()));
mlib::bank::Poke64(VertexBankEntry,12,0);
if(VertexDataHead == 0)
{
VertexDataHead = VertexBankEntry;
}
else
{
mlib::bank::Poke64(VertexDataTail,12,VertexBankEntry);
}
VertexDataTail = VertexBankEntry;
VertexCount++;
}
else //if there are not 4 words, or the first word is not "v"
{
//If there are three words -> a hash(#) and the number of vertices, then there are no more verts to read, so exit now.
if((mlib::string::CountWords(RecoveredString) == 3) && (mlib::string::GetWord(RecoveredString,1).compare("#") == 0) && (mlib::string::GetWord(RecoveredString,3).compare("vertices") == 0))
{
std::cout << "Finished adding vertices\n";
EndOfVertexList = true; //because we don't want to waste time iterating through the normals, faces, etc.
}
}
ReadOffset += RecoveredString.size() + 2; //each string is terminated by an ascii 13, and an ascii 10 following that.
}while((ReadOffset < mlib::bank::PeekSize(MeshFileBank)) && (EndOfVertexList == false));
//Now, put those vertices in a manner that newton understands
//Remember, convex hulls do not need the faces - they just try to 'wrap up' all of the vertices into the smallest possible parcel
VertexList = (dFloat*) malloc (3 * VertexCount * sizeof (dFloat));
std::cout << "malloc'd " << 3 * VertexCount * sizeof (dFloat) << " bytes of memory for the vertex list\n";
unsigned long CurrentElement = 0;
//Stick all of the vertices in VertexList, and whilst doing it,
//free all that memory that I just used up
unsigned long CurrentMemoryAddress, NextMemoryAddress = VertexDataHead;
while(NextMemoryAddress != 0)
{
CurrentMemoryAddress = NextMemoryAddress;
NextMemoryAddress = mlib::bank::Peek64(CurrentMemoryAddress,12);
VertexList[CurrentElement] = mlib::bank::PeekFloat(CurrentMemoryAddress,0);
VertexList[CurrentElement + 1] = mlib::bank::PeekFloat(CurrentMemoryAddress,4);
VertexList[CurrentElement + 2] = mlib::bank::PeekFloat(CurrentMemoryAddress,8);
std::cout << "Copied vertex " << CurrentElement / 3 << " (" << VertexList[CurrentElement] << ", " << VertexList[CurrentElement + 1] << ", " << VertexList[CurrentElement + 2] << ")\n";
CurrentElement += 3;
mlib::bank::FreeBank(CurrentMemoryAddress);
}
mlib::bank::FreeBank(MeshFileBank);
std::cout << "Creating collision shape (" << VertexCount << " vertices)\n";
ReturnedCollision = NewtonCreateConvexHull(CurrentNewtonWorld,VertexCount,VertexList,3 * sizeof(dFloat),0.05f,0,NULL); //crash on this line
std::cout << "done!\n";
delete[] VertexList;
return ReturnedCollision;
}
//Empty file name
return NULL;
}
I've also attached my console output. I don't think it will help at all, but just in case it does...
Edit: Is this the correct board for this question? Or would it be better in General Discussion?

