I want to create a simple .OBJ parser. Code below works, but is slow. Im testing it with a 26MB file and it takes about 22 seconds to parse it. If i commented out the lines seen in the code, the time went to 17 seconds. It takes 17 seconds to iterate the file line by line and to extract data. Is there any way i can make it faster? I also tried reading the file into memory and then parsing it. It became slower.
while (std::getline(file, line)) {
if (line[0] == 'v') {
std::istringstream iss(line);
std::string type;
GLfloat x, y, z;
iss >> type >> x >> y >> z;
if (type.compare("v") == 0) {
//vertices.push_back(x);
//vertices.push_back(y);
//vertices.push_back(z);
}
else if (type.compare("vn") == 0) {
//normals.push_back(x);
//normals.push_back(y);
//normals.push_back(z);
}
else if (type.compare("vt") == 0) {
//UVs.push_back(x);
//UVs.push_back(y);
}
}
else if (line[0] == 'f') {
unsigned int size = out_vertices.size() / 3;
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
sscanf_s(line.c_str(),
"f %d/%d/%d %d/%d/%d %d/%d/%d",
&vertexIndex[0], &uvIndex[0], &normalIndex[0],
&vertexIndex[1], &uvIndex[1], &normalIndex[1],
&vertexIndex[2], &uvIndex[2], &normalIndex[2]);
/*GLfloat* vertex = &vertices[(vertexIndex[0] - 1) * 3];
out_vertices.insert(out_vertices.end(), vertex, vertex + 3);
vertex = &vertices[(vertexIndex[1] - 1) * 3];
out_vertices.insert(out_vertices.end(), vertex, vertex + 3);
vertex = &vertices[(vertexIndex[2] - 1) * 3];
out_vertices.insert(out_vertices.end(), vertex, vertex + 3);
GLfloat* uv = &UVs[(uvIndex[0] - 1) * 2];
out_UVs.insert(out_UVs.end(), uv, uv + 2);
uv = &UVs[(uvIndex[1] - 1) * 2];
out_UVs.insert(out_UVs.end(), uv, uv + 2);
uv = &UVs[(uvIndex[2] - 1) * 2];
out_UVs.insert(out_UVs.end(), uv, uv + 2);
GLfloat* normal = &normals[(normalIndex[0] - 1) * 3];
out_normals.insert(out_normals.end(), normal, normal + 3);
normal = &normals[(normalIndex[1] - 1) * 3];
out_normals.insert(out_normals.end(), normal, normal + 3);
normal = &normals[(normalIndex[2] - 1) * 3];
out_normals.insert(out_normals.end(), normal, normal + 3);
out_indices.push_back(size);
out_indices.push_back(size + 1);
out_indices.push_back(size + 2);*/
}
}