I have the following c++ code to load an object file, at least the vertices and the vertex indices.
bool ObjMeshImporter::from_file(const std::string& filepath, nelems::Mesh* pMesh)
{
std::ifstream in(filepath, std::ios::in);
if (!in)
{
return false;
}
std::vector<glm::vec3> t_vert;
std::string line;
while (std::getline(in, line))
{
if (line.substr(0, 2) == "v ")
{
// read vertices
std::istringstream s(line.substr(2));
glm::vec3 v; s >> v.x; s >> v.y; s >> v.z;
// Add to temporary vertices before indexing
t_vert.push_back(v);
}
else if (line.substr(0, 2) == "f ")
{
// TODO: Store UVs and Normals
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
int count_found = sscanf_s(line.substr(2).c_str(),
"%d/%d/%d %d/%d/%d %d/%d/%d\n",
&vertexIndex[0], &uvIndex[0], &normalIndex[0],
&vertexIndex[1], &uvIndex[1], &normalIndex[1],
&vertexIndex[2], &uvIndex[2], &normalIndex[2]);
if (count_found != 9) {
return false;
}
pMesh->add_vertex_index(vertexIndex[0]-1);
pMesh->add_vertex_index(vertexIndex[1]-1);
pMesh->add_vertex_index(vertexIndex[2]-1);
}
}
// Now use the indices to create the concrete vertices for the mesh
for (auto v_idx : pMesh->GetVertexIndices())
{
glm::vec3 vertex = t_vert[v_idx];
pMesh->add_vertex(vertex);
}
return true;
}
It works pretty good for an object like this (Blender icosphere):
This model is made of triangles.
But when I load one which has quads, it doesnt work:
of course, because this is my face render method:
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)mVertexIndices.size());
So I know, I have to extend the parser and for every line in "f" check how many indices I have got and best would be to store a VFace-class that knows how many vertices a face is made of - ok.
So I would render faces with tris as GL_TRIANGLE and faces with quads as GL_QUADS... but what about the NGONS? And also how to render the lines?
EDIT: I saw there is GL_POLYGON. But do I have to create separate vertexbuffers for these?