Hey there,
So i've recently been trying to parse an OBJ file and I can parse it successfully as long as theres no normals or textures. Now I want to parse textures as well but as we all know the obj file face indices are different from the texture indices. For example f 1/2/3 should be f 1/1/1 in order to load it properly in opengl.
So I was wondering if someone could help me out on how to fix this problem here's my code, note it may be messy because i've been playing around with it trying to solve this problem:
int COS::LoadTexOBJ(string filename ,int objects)
{
ifstream fobj;
string line;
string mtl;
GLuint temp[3];
char *fu;
int size;
fobj.open(filename, ios::in);
while (!fobj.eof())
{
getline(fobj, line);
if (line.find("v ") != line.npos)
{
//parse vertices
sscanf_s(line.c_str(), "v %f %f %f", &com[objects].Object.Triangle.xyz[0], &com[objects].Object.Triangle.xyz[1], &com[objects].Object.Triangle.xyz[2]);
com[objects].Object.Triangle.vertices.push_back(com[objects].Object.Triangle.xyz[0]);
com[objects].Object.Triangle.vertices.push_back(com[objects].Object.Triangle.xyz[1]);
com[objects].Object.Triangle.vertices.push_back(com[objects].Object.Triangle.xyz[2]);
com[objects].Object.Triangle.vertex_num += 1;
}
if (line.find("vt ") != line.npos)
{
//parse texture coordinates
sscanf_s(line.c_str(), "vt %f %f", &com[objects].Object.Triangle.Texture.uv[0], &com[objects].Object.Triangle.Texture.uv[1]);
com[objects].Object.Triangle.Texture.tex.push_back(com[objects].Object.Triangle.Texture.uv[0]);
com[objects].Object.Triangle.Texture.tex.push_back(com[objects].Object.Triangle.Texture.uv[1]);
com[objects].Object.Triangle.Texture.tex_num += 1;
}
/*if (line.find("vt ") != line.npos)
{
sscanf_s(line.c_str(), "f %f %f ", &com[object].u, &com[object].v);
}
*/
}
fobj.close();
//close and re open it because we now have the number of texture coordinates
fobj.open(filename, ios::in);
while (!fobj.eof())
{
getline(fobj, line);
if (line.find("f ") != line.npos)
{
sscanf_s(line.c_str(), "f %d/%d %d/%d %d/%d", &com[objects].Object.Triangle.points[0], &temp[0], &com[objects].Object.Triangle.points[1], &temp[1], &com[objects].Object.Triangle.points[2], &temp[2]);
com[objects].Object.Triangle.points[0] -= 1;
com[objects].Object.Triangle.points[1] -= 1;
com[objects].Object.Triangle.points[2] -= 1;
temp[0] -= 1;
temp[1] -= 1;
temp[2] -= 1;
com[objects].Object.Triangle.faces.push_back(com[objects].Object.Triangle.points[0]);
com[objects].Object.Triangle.faces.push_back(com[objects].Object.Triangle.points[1]);
com[objects].Object.Triangle.faces.push_back(com[objects].Object.Triangle.points[2]);
//temp storage for the texture indices
com[objects].Object.Triangle.Texture.temp.push_back(temp[0]);
com[objects].Object.Triangle.Texture.temp.push_back(temp[1]);
com[objects].Object.Triangle.Texture.temp.push_back(temp[2]);
com[objects].Object.Triangle.face_num += 1;
}
}
fobj.close();
//unrelated stuff just code for getting the texture path and buffer stuff
mtl = filename.erase(filename.length() - 3, 3);
mtl.append("mtl");
fobj.open(mtl, ios::in);
while (!fobj.eof())
{
getline(fobj, line);
if (line.find(" map_Ka")!= line.npos)
{
com[objects].Object.Triangle.Texture.filename = line.substr(8, line.length());
com[objects].Object.Triangle.Texture.texpath = com[objects].Object.Triangle.Texture.filename.c_str();
}
}
fobj.close();
com[object].Object.Triangle.Texture.TexID = LoadTGATexture((char*)com[objects].Object.Triangle.Texture.texpath);
glGenTextures(1, &com[object].Object.Triangle.Texture.TexID);
texture = true;
com[objects].type = MESH;
com[objects].Object.Triangle.draw = true;
object = object + 1;
GenerateColors(com[objects].Object.Triangle.rgb[0], com[objects].Object.Triangle.rgb[1], com[objects].Object.Triangle.rgb[2]);
glGenBuffers(1, &com[objects].Object.Triangle.vbo);
glBindBuffer(GL_ARRAY_BUFFER, com[objects].Object.Triangle.vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*com[objects].Object.Triangle.vertices.size(), &com[objects].Object.Triangle.vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &com[objects].Object.Triangle.Texture.vto);
glBindBuffer(GL_ARRAY_BUFFER, com[objects].Object.Triangle.Texture.vto);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*com[objects].Object.Triangle.Texture.tex.size(), &com[objects].Object.Triangle.Texture.tex[0], GL_STATIC_DRAW);
glGenBuffers(1, &com[objects].Object.Triangle.vio);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, com[objects].Object.Triangle.vio);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*com[objects].Object.Triangle.faces.size(), &com[objects].Object.Triangle.faces[0], GL_STATIC_DRAW);
glGenVertexArrays(1, &com[objects].Object.Triangle.vao);
glBindVertexArray(com[objects].Object.Triangle.vao);
return 0;
}