model loader really slow

Started by
6 comments, last by yahastu 16 years ago
I would like some suggestions on making my model loader faster, it is loading .obj file format. There area where it is slow is converting the mesh data to opengl VBO data. so it looks for duplicate vertex,normal and uv data and creates indices as appropriatly. a model with 570 faces loads in like 2 seconds, but i have a model with 25000+ faces, I waited maybe 20mins with it not showing on the screen, yet i kept hitting a break point and it was still processing. I am using stl vectors as my containers I think this is the main problem area, the compare section

for(unsigned int h = 0; h < ActualVertexArraySize; h++)//we will already set 0 so move to next element for the IB
{
	//because we start from the beginning each time need to ensure we dont count the current data 
   if(iterVT != endVT)
   {
		//check the vertices
	   //use the temp data to compare with the rest of the vertex
		if(FLOATCOMPARE(pVT.x,iterVT->x)&&
		  FLOATCOMPARE(pVT.y,iterVT->y)&&
		  FLOATCOMPARE(pVT.z,iterVT->z) )
	   {
	     if(hasNormal)
		 {
		  if(iterNT != endNT)
		  {
		   if(FLOATCOMPARE(pNT.x,iterNT->x)&&
			  FLOATCOMPARE(pNT.y,iterNT->y)&&
			  FLOATCOMPARE(pNT.z,iterNT->z) )
		   {
			if(hasUV)
			{
			  if(FLOATCOMPARE(pUV.u,iterUV->u)&&
				 FLOATCOMPARE(pUV.v,iterUV->v) )
			  {
				//it is a old index, so reuse it, then increment the new index place
	   			currentMesh.triangleIndexBuffer[index]= h;
				reuse = true;
				break;
			  }//end compare UV
			}
			else
			{
		     //it is a old index, so reuse it, then increment the new index place
	   	     currentMesh.triangleIndexBuffer[index]= h;
			 reuse = true;
			 break;
			}
		   }//end compare normals
		  }//check if normal iterator in range
		 }//end if hasNormal
		 else if(hasUV)
		 {
		  if(iterUV != endUV)
		  {
		   if(FLOATCOMPARE(pUV.u,iterUV->u)&&
			  FLOATCOMPARE(pUV.v,iterUV->v) )
		   {
		   	 //it is a old index, so reuse it, then increment the new index place
	   	     currentMesh.triangleIndexBuffer[index]= h;
			 reuse = true;
			 break;
		   }//end comapre UV
		  }//check if uv iterator in range
		 }//end else if hasUV
		 //it is a old index, so reuse it, then increment the new index place
	   	 currentMesh.triangleIndexBuffer[index]= h;
		 reuse = true;
		 break;
	   }//end vert compare
   }
   if(iterVT != endVT)
		iterVT++;
   if(iterNT != endNT && hasNormal)
	 iterNT++;
   if(iterUV != endUV && hasUV)
	  iterUV++;
}//end for actual vertex size
//the element was unique so add it as a new vertexsize, jump and increment the index buffer.
if(!reuse)
{
	currentMesh.triangleIndexBuffer[index]= ActualVertexArraySize++;
	vtBuffer.push_back(pVT);
	endVT = vtBuffer.end();
	if(hasNormal)
	{
		ntBuffer.push_back(pNT);
		endNT = ntBuffer.end();
	}
	if(hasUV)
	{
		uvBuffer.push_back(pUV);
		endUV = uvBuffer.end();
	}
}
  
//reset the iterators
iterVT = vtBuffer.begin();
iterNT = ntBuffer.begin();
iterUV = uvBuffer.begin();

and the model sorting in its entirity

for(unsigned int currentFace= 0; currentFace < currentMesh.meshData->numFaces; currentFace++)
{
  bool hasNormal =false; bool hasUV = false;
  bool reuse = false;
  //short hand reference
  pf  = currentMesh.meshData->faceList[currentFace];

  //did we count normals and uv's when reading the mesh data
  if(currentMesh.meshData->numNormals > 0)
  {
    hasNormal = true; 
    pNT = currentMesh.meshData->normalList[pf.normalIndices[0]];
  }
  if(currentMesh.meshData->numUV > 0)
 	hasUV = true;
		
  //check for duplicate vertices,normals and uv
  /*Here we use K to represent 3 points of a triangle
  We use index to move along the currentFace and reference the actual data of a uv,normal,or vertex*/
  for(int k = 0; k < 3; k++, index++)
  {
    pVT = currentMesh.meshData->vertexList[pf.vertIndices[k]];
    if(hasNormal && currentMesh.meshData->numNormals > 1 )
    {
     pNT = currentMesh.meshData->normalList[pf.normalIndices[k]];
    }
    if(hasUV)
    {
	pUV = currentMesh.meshData->uvList[pf.uvIndices[k]];
    }
    //go through all the list list of normals, UV and vertices to check if they are in the VBO
   //assign Indices based on, 
   for(unsigned int h = 0; h < ActualVertexArraySize; h++)
   {
    //because we start from the beginning each time need to ensure we dont count the current data 
     if(iterVT != endVT)
     {
	//check the vertices
	//use the temp data to compare with the rest of the vertex
	if(FLOATCOMPARE(pVT.x,iterVT->x)&&
	   FLOATCOMPARE(pVT.y,iterVT->y)&&
	   FLOATCOMPARE(pVT.z,iterVT->z) )
	 {
	    if(hasNormal)
	    {
	     if(iterNT != endNT)
	     {
		if(FLOATCOMPARE(pNT.x,iterNT->x)&&
		   FLOATCOMPARE(pNT.y,iterNT->y)&&
		   FLOATCOMPARE(pNT.z,iterNT->z) )
	       {
		 if(hasUV)
		 {
		   if(FLOATCOMPARE(pUV.u,iterUV->u)&&
		      FLOATCOMPARE(pUV.v,iterUV->v) )
		   {
		   //it is a old index, so reuse it, then increment the new index place
		   currentMesh.triangleIndexBuffer[index]= h;
		   reuse = true;
		   break;
		  }//end compare UV
		}
		else
		{
		   //it is a old index, so reuse it, then increment the new index place
		  currentMesh.triangleIndexBuffer[index]= h;
		  reuse = true;
		  break;
		}
	     }//end compare normals
	   }//check if normal iterator in range
       }//end if hasNormal
       else if(hasUV)
       {
	 if(iterUV != endUV)
	 {
	  if(FLOATCOMPARE(pUV.u,iterUV->u)&&
	     FLOATCOMPARE(pUV.v,iterUV->v) )
          {
           //it is a old index, so reuse it, then increment the new index place
	    currentMesh.triangleIndexBuffer[index]= h;
	    reuse = true;
	    break;
	  }//end comapre UV
	 }//check if uv iterator in range
	}//end else if hasUV
      //it is a old index, so reuse it, then increment the new index place
      currentMesh.triangleIndexBuffer[index]= h;
      reuse = true;
      break;
   }//end vert compare
  }
  if(iterVT != endVT)
    iterVT++;
  if(iterNT != endNT && hasNormal)
    iterNT++;
  if(iterUV != endUV && hasUV)
    iterUV++;
 }//end for actual vertex size
//the element was unique so add it as a new vertexsize, jump and increment the index buffer.
if(!reuse)
{
 currentMesh.triangleIndexBuffer[index]= ActualVertexArraySize++;
 vtBuffer.push_back(pVT);
 if(hasNormal)
 {
  ntBuffer.push_back(pNT);
 }
 if(hasUV)
 {
  uvBuffer.push_back(pUV);
 }
}
 
//reset the iterators
iterVT = vtBuffer.begin();
iterNT = ntBuffer.begin();
iterUV = uvBuffer.begin();
endVT = vtBuffer.end();
endNT = ntBuffer.end();
endUV = uvBuffer.end();
reuse = false;
}//end loop per trangle
if(currentIterVT != currentMesh.meshData->vertexList.end())
 currentIterVT++;
if(currentIterUV != currentMesh.meshData->uvList.end() && hasUV)
  currentIterUV++;
if(currentIterNT != currentMesh.meshData->normalList.end() && hasNormal)
	currentIterNT++;
}//for current face

Advertisement
I dunno, don't really want to look through your code...but here is an .obj file reader I wrote that I wrote to be fast, as I recall loads most things in less than a second. I only load .obj's for debugging, its about 100,000,000 times faster to store the mesh data structure directly into a binary file and then load it back up in 1 file read. You'll want to strip out the exception macros.

edit: actually, im looking over it now, this is pretty shit code (embarrassed), could be optimized a lot more, but maybe it will help you nonetheless.

void Mesh::ParseObj(const std::string &name)	{		using namespace std;		using namespace Core;		string err_msg = "";		Face *cur_face = NULL;#define CHECK_ERR_LOAD(msg)											if (!file.good() )											{																err_msg = (msg);											SAFE_DELETE(cur_face);										NOVA_EXCEPT(err_msg.c_str(), "LoadFromObj");			}				ifstream file(name.c_str(), ios::in);		//load raw data		while (file.good() )		{			string ident;			file >> ident;			if (ident == "v" )			{				//process vertex				float x,y,z;				file >> x >> y >> z;				CHECK_ERR_LOAD("Invalid vertex");				positions.push_back( new Vector3(x,y,z) );			} else if (ident == "vn" )			{				//process normal				float nx,ny,nz;				file >> nx >> ny >> nz;				CHECK_ERR_LOAD("Invalid normal");				normals.push_back( new Vector3(nx,ny,nz) );			} else if (ident == "vt" )			{				//process texture coord				float u,v;				file >> u >> v;				CHECK_ERR_LOAD("Invalid UV");				texcoords.push_back( new Vector3(u,v,0.0f) );			} else if (ident == "f" )			{				cur_face = new Face();				//process face				for(int v=0; v<3; ++v)				{					char slash;					UINT pos_ind, tex_ind, norm_ind;					file >> pos_ind >> slash >> tex_ind >> slash >> norm_ind;					CHECK_ERR_LOAD("Invalid face");					//change from 1-based indexing to 0-based					cur_face->ind_pos[v] = pos_ind - 1;					cur_face->ind_tex[v] = tex_ind - 1;					cur_face->ind_norm[v] = norm_ind - 1;				}				faces.push_back( cur_face );							}			//skip to end of line			string skip;			getline(file, skip);		}		file.close();		return;	}
looking over your code, that you supplied that isnt where I have my problem, I load in the mesh data into my program fast and fine. Its when I am building Vertex Buffer with Indices and I dont want duplicates vertices.
First of all: fix your indentation! Without proper, consistent indentation, it becomes very hard to read your code.

And you "think" that it's the main problem area? You should always run it through a profiler to see what is actually taking up the time. Programmers are notoriously bad at guessing bottlenecks in code. [grin]

In any case, your algorithm has an O(n^2) running time. For each face, you're looping through each vertex. If you've got 25000 faces and 75000 vertices, then that means that your inner loop is executed 1.875 billion times. That's a lot.

As I understand it, you're performing some sort of vertex welding here. That is, you're combining or joining two vertices which are very close together. Vertex welding is just a special case of collision detection. There are a lot of ways to speed collision detection up, but the most common is to use spatial partitioning. Recently I wrote my own vertex welder and was experiencing the same problems as you - it was just too slow. Originally, my welder took ~12.7 seconds to process a 22,000 vertex mesh. After spatial partitioning and optimisation, I was able to get it down to 47ms for that same 22,000 vertex mesh.

I just used a simple octree to split up the vertices into spatial partitions. But it greatly reduced the search space required for each vertex. Rather than comparing every vertex against tens of thousands of other vertices, I was only comparing each vertex against a few hundred others. You can read up on octrees here.
NextWar: The Quest for Earth available now for Windows Phone 7.
Quote:Original post by ashstampede
looking over your code, that you supplied that isnt where I have my problem, I load in the mesh data into my program fast and fine. Its when I am building Vertex Buffer with Indices and I dont want duplicates vertices.


You have to duplicate vertices because the same vertex may have different normal or texture coordinate for different triangles it is shared by. If you do it this way then building the buffers is trivial.
Quote:
You have to duplicate vertices because the same vertex may have different normal or texture coordinate for different triangles it is shared by. If you do it this way then building the buffers is trivial.

that to me sounds like I should just have the data as is in the VBO, and dont do indices.
Regarding speeding up vertex welding: here you can find a mesh class (called EditTriMesh) that implements a more efficient vertex welding algorithm that has O(n) complexity, and it doesn't even use spatial partitioning. The code is very readable and is heavily commented.
Quote:Original post by ashstampede
Quote:
You have to duplicate vertices because the same vertex may have different normal or texture coordinate for different triangles it is shared by. If you do it this way then building the buffers is trivial.

that to me sounds like I should just have the data as is in the VBO, and dont do indices.


Yep, .obj files use indexed lists for position, normal, and texture...whereas in directx you cannot split your vertex data among multiple index lists (at least not to my knowledge!). It's less space efficient, but more efficient for rendering apparently...so I just use a straight vertex buffer without an index buffer.

This topic is closed to new replies.

Advertisement