Hello there,
I'm trying to load an .obj wavefront file (for simplicity reasons - later I want to read other - better - fromats).
The models are exported with Blender 2.68a.
I assume that the vertices are read correctly - the problem is rather in the index generating part as this picture suggests:
(The left model was recreated from the Input Assembler input)
The main problem with loading the indices is similar to this:
My solution to the problem is to look in each face definition if the vertex already exists and then add the corresponding index.
Otherwise the new vertex is added to a list and then add a the index (wich is the size of the vertex array...)
//Split the line with delim ' ' (Space) to gather the data of each vertex
vector<string> triangle_data = Split(line,' '); //Vetor of vertex data
//Only triangles are supported!
if (triangle_data.size() != 4) //1st element is a empty string!
throw invalid_argument("OBJ file contains primitive data other than Triangle Lists. (NGON, L ines, ...)");
//Read the vertices
for (int iVertex = 0; iVertex < 3; iVertex++) //Process each vertex
{
/*
one vertex data looks like this:
5/8/9 or 6//4
*/
string vertex_data = triangle_data.at(iVertex);
if (vertex_data == "")
continue;
//Split vertex_data with delimeter '/' to gather indices of position and normal
vector<string> vertex_index_data = Split(vertex_data);
//Get Position data
UINT position_index = stoi(vertex_index_data.at(0)) - 1;
UINT normal_index = stoi(vertex_index_data.at(2)) - 1;
if (position_index >= position_data.size() || normal_index >= normal_data.size())
continue; //We should never get here
XMFLOAT3 pos = position_data.at (position_index);
XMFLOAT3 nrm = normal_data.at (normal_index);
//Assemble the vertex
VertexFormats::VertexPositionNormal vertex;
vertex.position = pos;
vertex.normal = nrm;
//Update the vertex and index vectors
//Check if the vertex already exists
auto vertex_pos = find(vertices.begin(), vertices.end(), vertex);
if (vertex_pos == vertices.end())
{
//The vertex does not exist - add it to the vertex list
vertices.push_back(vertex);
//Update the index list
indices.push_back(vertices.size() - 1);
}
else
{
//the vertex already exists - add the corresponding index
indices.push_back(vertex_pos - vertices.begin());
}
} //for each vertex
(The whole code of the class is attached.)