So I have created a 3D maze in blender and have successfully loaded the object into opengl and drawn it. I read through the OBJ file that was exported and extracted all the needed information.
After I load the object in and draw it I try to read back through the file and add the vertices of each triangle into the Bullet Triangle Mesh to handle the collision for me. I am able to load the information but I’m getting collisions where I should not be getting collision. Essentially I am not loading the triangle information to the mesh correctly, I believe.
Here is what I have for my file parser so far. I edited it from the file parser that loads the object and draws for OpenGL.
bool ObjModel::reloadVertices(std::string path, std::string filename){
std::vector<unsigned int> vertexIndices, texIndices, normalIndices;
std::vector<glm::vec3> file_vertices;
std::vector<glm::vec2> file_texcoords;
std::vector<glm::vec3> file_normals;
std::string nextmatname = "";
std::string matname = "";
bool firstfaceset = true;
std::string fullFilename = path + filename;
FILE * file = fopen(fullFilename.c_str(), "r");
if(file == NULL)
{
std::cout << "Cannot open file: " << filename << std::endl;
return false;
}
vertices.clear();
texcoords.clear();
normals.clear();
bool finished = false;
while(!finished)
{
char lineHeader[512];
int res = fscanf(file, "%s", lineHeader);
if (res == EOF)
{
finished = true;
}
else
{
if (strcmp(lineHeader, "mtllib") == 0)
{
char MaterialFilename[4096];
fscanf(file, "%s", MaterialFilename);
std::string matfile = MaterialFilename;
LoadMateials(path, matfile);
}
else if (strcmp(lineHeader, "usemtl") == 0)
{
matname = nextmatname;
char MaterialName[4096];
fscanf(file, "%s", MaterialName);
nextmatname = MaterialName;
if (firstfaceset)
{
firstfaceset = false;
}
else
{
for(unsigned int i = 0; i < vertexIndices.size(); i++)
{
unsigned int vertexIndex = vertexIndices[i];
glm::vec3 vertex = file_vertices[vertexIndex - 1];
vertices.push_back(vertex);
}
for(unsigned int i = 0; i < normalIndices.size(); i++)
{
unsigned int normalIndex = normalIndices[i];
glm::vec3 normal = file_normals[normalIndex - 1];
normals.push_back(normal);
}
for(unsigned int i = 0; i < texIndices.size(); i++)
{
unsigned int texIndex = texIndices[i];
glm::vec2 texcoord = file_texcoords[texIndex - 1];
texcoords.push_back(texcoord);
}
LoadDataToGraphicsCard(matname);
//vertexIndices.clear();
texIndices.clear();
normalIndices.clear();
//vertices.clear();
texcoords.clear();
normals.clear();
}
}
else if (strcmp(lineHeader, "v") == 0)
{
glm::vec3 vertex;
fscanf(file, "%f %f %f
", &vertex.x, &vertex.y, &vertex.z);
file_vertices.push_back(vertex);
}
else if (strcmp(lineHeader, "vt") == 0)
{
glm::vec2 tex;
fscanf(file, "%f %f
", &tex.x, &tex.y);
file_texcoords.push_back(tex);
}
else if (strcmp(lineHeader, "vn") == 0)
{
glm::vec3 normal;
fscanf(file, "%f %f %f
", &normal.x, &normal.y, &normal.z);
file_normals.push_back(normal);
}
else if (strcmp(lineHeader, "f") == 0)
{
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
vertexIndex[0] = 1;
vertexIndex[1] = 1;
vertexIndex[2] = 1;
uvIndex[0] = 1;
uvIndex[1] = 1;
uvIndex[2] = 1;
normalIndex[0] = 1;
normalIndex[1] = 1;
normalIndex[2] = 1;
long filepos = ftell(file);
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d
", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
if (matches == 9)
{
vertexIndices.push_back(vertexIndex[0]);
vertexIndices.push_back(vertexIndex[1]);
vertexIndices.push_back(vertexIndex[2]);
//texIndices.push_back(uvIndex[0]);
//texIndices.push_back(uvIndex[1]);
//texIndices.push_back(uvIndex[2]);
//normalIndices.push_back(normalIndex[0]);
//normalIndices.push_back(normalIndex[1]);
//normalIndices.push_back(normalIndex[2]);
}
else
{
fseek(file, filepos, SEEK_SET);
matches = fscanf(file, "%d//%d %d//%d %d//%d
", &vertexIndex[0], &normalIndex[0], &vertexIndex[1], &normalIndex[1], &vertexIndex[2], &normalIndex[2]);
if (matches == 6)
{
vertexIndices.push_back(vertexIndex[0]);
vertexIndices.push_back(vertexIndex[1]);
vertexIndices.push_back(vertexIndex[2]);
//normalIndices.push_back(normalIndex[0]);
//normalIndices.push_back(normalIndex[1]);
//normalIndices.push_back(normalIndex[2]);
}
else
{
fseek(file, filepos, SEEK_SET);
matches = fscanf(file, "%d/%d %d/%d %d/%d
", &vertexIndex[0], &uvIndex[0], &vertexIndex[1], &uvIndex[1], &vertexIndex[2], &uvIndex[2]);
if (matches == 6)
{
vertexIndices.push_back(vertexIndex[0]);
vertexIndices.push_back(vertexIndex[1]);
vertexIndices.push_back(vertexIndex[2]);
//texIndices.push_back(uvIndex[0]);
//texIndices.push_back(uvIndex[1]);
//texIndices.push_back(uvIndex[2]);
}
else
{
fseek(file, filepos, SEEK_SET);
matches = fscanf(file, "%d %d %d
", &vertexIndex[0], &vertexIndex[1], &vertexIndex[2]);
if (matches == 3)
{
vertexIndices.push_back(vertexIndex[0]);
vertexIndices.push_back(vertexIndex[1]);
vertexIndices.push_back(vertexIndex[2]);
}
else
{
std::cout << "File read error" << std::endl;
return false;
}
}
}
}
}
}
}
for(unsigned int i = 0; i < vertexIndices.size(); i++)
{
unsigned int vertexIndex = vertexIndices[i];
glm::vec3 vertex = file_vertices[vertexIndex - 1];
vertices.push_back(vertex);
}
for(unsigned int i = 0; i < normalIndices.size(); i++)
{
unsigned int normalIndex = normalIndices[i];
glm::vec3 normal = file_normals[normalIndex - 1];
normals.push_back(normal);
}
for(unsigned int i = 0; i < texIndices.size(); i++)
{
unsigned int texIndex = texIndices[i];
glm::vec2 texcoord = file_texcoords[texIndex - 1];
texcoords.push_back(texcoord);
}
texIndices.clear();
normalIndices.clear();
texcoords.clear();
normals.clear();
fclose(file);
return true;
}
Here is what I have for loading the triangles to the mesh:
void ObjModel::btLoadTriangleMesh(btTriangleMesh* tm)
{
for(int i = 0; i < vertexIndices.size(); i+=3){
glm::vec3 v1 = vertices[vertexIndices[i]];
glm::vec3 v2 = vertices[vertexIndices[i + 1]];
glm::vec3 v3 = vertices[vertexIndices[i + 2]];
btVector3 A(v1.x, v1.y, v1.z);
btVector3 B(v2.x, v2.y, v2.z);
btVector3 C(v3.x, v3.y, v3.z);
tm->addTriangle(A, B, C);
}
}
I am getting collision with bullet but not in the correct places.
I am not sure if I am not parsing the file correctly causing the triangles to not be correctly loaded to the mesh or if I am actually adding the triangles incorrectly.
Here is a pastebin link for my obj file since I cannot upload it to the post.
Any help would be greatly appreciated.