Hi, brand new to the forums and needing a bit of help with loading in an obj for an assignment. My model loader reads the obj file, and assigns the values for the vertices, normals, texture mapping and faces to 4 seperate vertex arrays. It then using the faces as an index to create 3 new sorted arrays for the vertices, normals and texture mapping co-ordinates, and uses these sorted arrays to render.
Trouble is the model is a complete mess and looks nothing like it should, although when I try and debug it looks as if the file is being read correctly, and the correct values are being sorted when using the appropriate index. Completely stumped.
The vector3 class contains 3 float values xyz.
sortedvertex, sortednormals and texCoords are all vector arrays of type float.
bool Model::loadModel(char* filename)
{
vector<Vector3> verts;
vector<Vector3> norms;
vector<Vector3> texCs;
vector<unsigned int> faces;
FILE* file = fopen(filename, "r");
if (file == NULL)
{
return false;
}
while (true)
{
char lineHeader[128];
// Read first word of the line
int res = fscanf(file, "%s ", lineHeader);
if (res == EOF)
{
break; // exit loop
}
else // Parse
{
if (strcmp(lineHeader, "v") == 0) // Vertex
{
Vector3 vertex;
fscanf(file, "%f %f %f
", &vertex.x, &vertex.y, &vertex.z);
verts.push_back(vertex);
}
else if (strcmp(lineHeader, "vt") == 0) // Tex Coord
{
Vector3 uv;
fscanf(file, "%f %f
", &uv.x, &uv.y);
texCs.push_back(uv);
}
else if (strcmp(lineHeader, "vn") == 0) // Normal
{
Vector3 normal;
fscanf(file, "%f %f %f
", &normal.x, &normal.y, &normal.z);
norms.push_back(normal);
}
else if (strcmp(lineHeader, "f") == 0) // Face
{
unsigned int face[9];
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d
", &face[0], &face[1], &face[2],
&face[3], &face[4], &face[5],
&face[6], &face[7], &face[8]);
if (matches != 9)
{
// Parser error, or not triangle faces
return false;
}
for (int i = 0; i < 9; i++)
{
double value = face[i] - 1;
faces.push_back(value);
}
}
}
}
// "Unroll" the loaded obj information into a list of triangles.
for (int f = 0; f < (int)faces.size(); f += 3) {
int faceIndex = faces[f];
// first face value (vertex)
sortedvertex.push_back(verts[faces[f]].x);
sortedvertex.push_back(verts[faces[f]].y);
sortedvertex.push_back(verts[faces[f]].z);
vertexCount++;
/*
// second face value (texture mapping)
texCoords.push_back(texCs[faces[f + 1]].x);
texCoords.push_back(texCs[faces[f + 1]].y);
*/
//third face value (normals)
sortednormals.push_back(norms[faces[f + 2]].x);
sortednormals.push_back(norms[faces[f + 2]].y);
sortednormals.push_back(norms[faces[f + 2]].z);
}
verts.clear();
norms.clear();
texCs.clear();
faces.clear();
return true;
}
void Model::render()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY); disable texturing for now
glVertexPointer(3, GL_FLOAT, 0, &sortedvertex);
glNormalPointer(GL_FLOAT, 0, &sortednormals);
//glTexCoordPointer(2, GL_FLOAT, 0, &texCoords);
glBindTexture(GL_TEXTURE_2D, NULL);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}