Skip to main content
GameDev.net gamedev.net
🔒 Locked

Need a more eficient way to parse a obj file

Started by force_of_will Apr 14, 2008 at 3:42 PM 6 replies 5.2k views
Original Post
force_of_will
force_of_will
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;
}



ViperG
ViperG
I don't see anything wrong with your code, but it shouldn't take 100 seconds...

7mb text file is a lot of text to go through.

how many lines is that file? or word count it.

only thing I can think of to make it faster would be to save it in binary.
Black Sky A Star Control 2/Elite like game
VypR
VypR
Only thing I can see is to read the entire file into memory first, then parse the content.

That should speed it up a bit, but not sure how much.
force_of_will
force_of_will
Line count is 246580, alot even so loading the same file with GLC-Player a obj model previewer takes about 10 seconds most !.
Since it's open source i had a chance to look in code, but they're using QT to parse it, and as so concurrency, even so i didn't and still don't think the performance boost would be soo much significant. About 10X slower mine aproach, as so i must be doing something terribly wrong

(The fault is always of the programmer in first place never the hardware LOL)
kiwibonga
kiwibonga
Are you running debug or release?
force_of_will
force_of_will
Tried both, both results suck, if anyone is willing to try it self i can send the project file with source code included (VS 2008), except model (7mb) off course.
nemebean
nemebean
Have you profiled this code? That's always a good place to start with performance problems.
force_of_will
force_of_will
Ok this is strange it seems that running it directly from the VS IDE makes it bottleneck alot even if in Release mode, running it by hand makes it faster, from +/- 100 to 6-7 seconds ... go figure




Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.