Jump to content
  • Advertisement
Sign in to follow this  
haansn08

Indices of an obj file

This topic is 1895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

 

objindices.png(The left model was recreated from the Input Assembler input)

 

The main problem with loading the indices is similar to this:

http://www.gamedev.net/topic/589494-opengl-es-20-how-to-obtain-indices-from-a-wavefront-obj-file/#entry4741209

 

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.)

Share this post


Link to post
Share on other sites
Advertisement

I found the problem(s):

1: I only read 2 vertices instead of 3 per triangle

//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, Lines, ...)");
	//Read the vertices
	//for (int iVertex = 0; iVertex < 3; iVertex++) //<---- Wrong
for (int iVertex = 1; iVertex < 4; iVertex++) //Process each vertex

2: I skip every secound triangle, since I jump to the next line after processing one triangle, but since I use getline the '\n' char is already read.

if (data_type == "f") //current line contains a triangle specification
		{
			ReadFace(file, positions, normals, vertices, indices);
			continue; //ReadFace does already read the \n char!
		}
		//Simply ignore other commands for now
#undef max
		//Skip the line
		file.ignore(numeric_limits<streamsize>::max(), '\n');
#define max

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!