Original Post
Im currently writing an obj file "parser" and my current solution is too slow taking about 100 seconds to load a 7mb file, which is not acceptable im trying to do this using the C++ Standard Library. Ideias are welcome to boost performance. EDIT: I've already tried to load the file before hand to a string buffer, thinking that i maybe where loosing some performance with the OS I/O buffering but that didn't help much.
bool cObjModel::Parse()
{
string buffer, keyword;
Vertex v;
TextCoord t;
char c;
while (m_file.good() && (m_file >> keyword))
{
if (keyword == "#" || keyword == "g") // comment TODO: for now we're also not parsing 'g' operations
{
ParseComment();
}
else if (keyword == "v") // vertex data
{
m_file >> v.x >> v.y >> v.z;
AddVertex(v);
}
else if(keyword == "vt") // texture data
{
m_file >> t.x >> t.y >> t.z;
AddTextCoord(t);
}
else if(keyword == "vn") // vertex normal
{
m_file >> v.x >> v.y >> v.z;
AddNormal(v);
}
else if (keyword == "f") //polygon face
{
PolygonFace face;
int i = 0;
int vIndex, tIndex, nIndex;
char c;
getline(m_file, buffer);
stringstream line(buffer);
while(line.good())
{
// if doesn't have texture coordinates and normal info
if (m_textCoordData.size() == 0 && m_normalData.size() == 0)
{
line >> vIndex;
face.m_vertexIndexes.push_back(vIndex - 1);
}
else if(m_textCoordData.size() == 0)
{
line >> vIndex >> c >> c >> nIndex;
face.m_vertexIndexes.push_back(vIndex - 1);
face.m_normalIndexes.push_back(nIndex - 1);
}
else if(m_normalData.size() == 0)
{
line >> vIndex >> c >> tIndex;
face.m_vertexIndexes.push_back(vIndex - 1);
face.m_textCoordsIndexes.push_back(tIndex - 1);
}
else
{
line >> vIndex >> c >> tIndex >> c >> nIndex;
face.m_vertexIndexes.push_back(vIndex - 1);
face.m_textCoordsIndexes.push_back(tIndex - 1);
face.m_normalIndexes.push_back(nIndex - 1);
}
}
if (!line.good())
{
face.m_vertexIndexes.pop_back();
face.m_textCoordsIndexes.pop_back();
face.m_textCoordsIndexes.pop_back();
}
m_faceData.push_back(face);
}
else
ParseComment(); // skip line
}
m_file.close();
return true;
}