# OBJ Loader not working correctly

I'm currently trying to implement my own OBJ Loader for use with FreeGLUT with C++. This is my current OBJ Loader: #include "OBJLoader.h" #include #include #include #include #include

Mesh* OBJLoader::LoadModel(char* path)
{
Mesh* mesh = new Mesh(); //New Mesh

std::vector<Vertex> vertices;
std::vector<TexCoord> textureCoords;
std::vector<Vector3> normals;

std::ifstream file;
file.open(path);
if (!file.good())
{
std::cout << "Can't open texture file " << path << std::endl;
return nullptr;
}

std::string text;
while (!file.eof())
{
file >> text;
if (text == "v")
{
Vertex vertex;

file >> vertex.X;
file >> vertex.Y;
file >> vertex.Z;

vertices.push_back(vertex);
}

if (text == "vt")
{
TexCoord texCoord;

file >> texCoord.U;
file >> texCoord.V;
textureCoords.push_back(texCoord);
}
}

file.close();

mesh->vertexCount = vertices.size();
mesh->vertices = new Vertex[mesh->vertexCount];
for (int i = 0; i < mesh->vertexCount; i++)
{
mesh->vertices[i].X = vertices[i].X;
mesh->vertices[i].Y = vertices[i].Y;
mesh->vertices[i].Z = vertices[i].Z;
}

mesh->texCoordCount = textureCoords.size();
mesh->texCoords = new TexCoord[mesh->texCoordCount];
for (int i = 0; i < mesh->texCoordCount; i++)
{
mesh->texCoords[i].U = textureCoords[i].U;
mesh->texCoords[i].V = textureCoords[i].V;
}

return mesh;
}


What I do is create 3 vectors to hold the vertices, texture coords and normals, I then put the data in the corresponding vectors from the OBJ file and then finally loop through these vectors to add them to basically 3 array's which will contain the vertices, texture coordinates and normals so I can use them with "glVertexPointer" and "glTexCoordPointer" (I haven't loaded in normals yet but I will eventually)

The problem is that I'm not sure why my model is displaying really strange. It seems like it should work but the model I'm rendering which is meant to be sonic looks like... this:

Is there something I've done wrong? I imagine it's to do with the faces at the bottom of the OBJ File though I just want to make sure.

EDIT This is the section of code I'm using to put the values from the vectors into the array's used in glVertexPointer and glTexCoordPointer:

mesh->vertexCount = vertices.size();
mesh->vertices = new Vertex[mesh->vertexCount];
for (int i = 0; i < mesh->vertexCount; i++)
{
mesh->vertices[i].X = vertices[i].X;
mesh->vertices[i].Y = vertices[i].Y;
mesh->vertices[i].Z = vertices[i].Z;
}

mesh->texCoordCount = textureCoords.size();
mesh->texCoords = new TexCoord[mesh->texCoordCount];
for (int i = 0; i < mesh->texCoordCount; i++)
{
mesh->texCoords[i].U = textureCoords[i].U;
mesh->texCoords[i].V = textureCoords[i].V;
}

• Where are you parsing the faces? the vertex array itself is not enough to render the mesh. – Jimmy Mar 14 '18 at 2:36
• I'm not currently, I've still got to implement it though I just assumed it would render without the indices, just with added performance. – codelyoko373 Mar 14 '18 at 2:47
• unless the indices look like "f 1 2 3 / f 4 5 6 / f 7 8 9", then the mesh would come out quite differently – Jimmy Mar 14 '18 at 2:50
• How exactly should I do it? Like can I use the indices within my glDrawElements call which takes in indices or should I be use the indices to rearrange the vectors containing vertices and tex coords? – codelyoko373 Mar 14 '18 at 2:53
• Yes, you can use drawElements with the indices, although the indices are 0-indexed rather than 1-indexed like OBJ. I've added an answer summarizing this comment chain. – Jimmy Mar 14 '18 at 2:58

I think the issue is that you're not using the face data. The vertex data you've parsed dictate the point locations of the geometry, but the ordering there doesn't usually correspond to where the *triangles lie.

In face, if you squint at the points of your current triangles, you can kind of see a Sonic head, two arms in a T Pose, and two large feet. This means the vertex positions are correct.

Your triangles should be fixed once you parse and render the face data from the OBJ.

How exactly should I do it? Like can I use the indices within my glDrawElements call which takes in indices or should I be use the indices to rearrange the vectors containing vertices and tex coords

Yes, you can use the indices with glDrawElements. You'll have to subtract one from each index since OBJ indices start at 1 and OpenGL indices start at 0.

• I see, I thought it was likely the indices. Though the thing I'm confused about is how exactly I should hold these indices. Should the array I pass into the glDrawElements hold all the indices for the vertices, texture coordinates and normal's or just the indices for the vertices? – codelyoko373 Mar 14 '18 at 3:00
• The vertex data should be in an array you store with glBindBuffer first. Then glDrawElements will data from that array. – Jimmy Mar 14 '18 at 3:30
• The full scope of how to draw vertices is in openGL is outside of the bounds of this question, but generally you bind an ARRAY_BUFFER with combined vertex data, set your vertexpointers on that (texcoords and normals and positions) [although you could also bind mlutiple buffers as an option] and then drawElements with an an element array buffer. – Jimmy Mar 14 '18 at 3:33
• If this sounds really confusing, you could also manually index your buffers in your code, i.e. have a new vertex array that you fill with finalvertices[0] = inputVertices[inputIndices[0] etc. – Jimmy Mar 14 '18 at 3:35
• I think I could work it out with enough research :). So the array that I'll be using with glBindBuffer will hold all the indices for the vertices, texcoords and normals? – codelyoko373 Mar 14 '18 at 3:37