Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Indices of an obj file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 haansn08   Members   -  Reputation: 172

Like
0Likes
Like

Posted 13 August 2013 - 06:27 AM

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

Attached Files



Sponsor:

#2 haansn08   Members   -  Reputation: 172

Like
0Likes
Like

Posted 13 August 2013 - 08:17 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS