Jump to content


Building VBO from model data in opengl


  • You cannot reply to this topic
No replies to this topic

#1 ashstampede

    New Member

  • Members
  • Pip
  • 3 posts

Posted 05 April 2008 - 01:15 AM

Hi new here and wonder if you guys could help. Currently I have read the model data from .obj file format, and I am now trying to load into a vertex buffer with offset and into an index array.

I am having trouble building a c++ algorithm to remove the duplicate vertices when I am building the index array. What I am working on is model after http://www.robtheblo...e/ObjLoader.zip but his example was written in c using pointer arrays and malloc and realloc. the section of his code I am using is the convertmeshto function which can be found in objLoader7 and up.

The problem I have is when I am comparing my current face data to data I may have gotten before. apart form creating a new set of for loops I dont see how to get around it. In the code that follows I
-loop for the total of faces the mesh may have, then i loop for a count of 3 to represent a triangle. then loop for the total count of my Index Array I am building.

in my loop for a count of 3, using the current face i get the reference it has to the normal, vertex, and uv data. put each in a temp 3 float array.

The trouble I am having, Now i want to go and check the VBO I am building if it has any of this data in it already. if it does use the same index for it other wise its a new index. Short from creating for loops to go back through the vbo I have built, I dont see how to get around this, now the code example from that site doesnt have another loop, but reading through the source example he has a global loop counter called "j" that is incremented with the loop for 3.

so I have worked it out as at face 0 j is 0
then j incremented 3 times as its the first face

next face j is at 3, j is the reference to get the vertex,normal and uv data we may have in the VBO, yet i dont see how it compares past data.

in my code I am concerned with having the variable "index" read the right data to do the correct compare. an example, verts gets the right data that I compare next to see if I have it already or need it. does it mean more loops>

Vertex Verts = currentMesh.meshData->vertexList[index];


unsigned int index = 0;

for(unsigned int currentFace= 0; currentFace < currentMesh.meshData->numFaces; currentFace++)

{

	bool hasNormal =false; bool hasUV = 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;

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

	{

		//temp data 

		 float verts[3]={0},norms[3]={0},uvs[2]={0};

		//at least we will have 3 vertices to make up a triangle	 			 verts[0] = pVT[ pf.vertIndices[k] ].x ;

		 verts[1] = pVT[ pf.vertIndices[k] ].y ;

		 verts[2] = pVT[ pf.vertIndices[k] ].z ;

		 tempVerts = pVT[ pf.vertIndices[k] ];

		 if(hasNormal)

		{

		  norms[0] = pNT[ pf.normalIndices[k] ].x ;

		  norms[1] = pNT[ pf.normalIndices[k] ].y ;

		  norms[2] = pNT[ pf.normalIndices[k] ].z ;

		 tempNorm = pNT[ pf.normalIndices[k] ];

		 }

		 if(hasUV)

		 {

		  uvs[0] = pUT[ pf.uvIndices[k] ].u ;

		  uvs[1] = pUT[ pf.uvIndices[k] ].v ;

		  tempUV = pUT[ pf.uvIndices[k] ];

		 }

			

		 //verts norms uvs now have the current triangle data.

		 //test for reoccurrence 

		 //if we have incremented the IBO we check against the indices and vertex we have crossed referenced

		 //with the other vertex data of the obj.

		 int ib = 0;

		 while(ib < ActualVertexArraySize;++ib) 

		 {

		  //loop through all the triangle data we may have stored

		 //compare if we added this vertex yet

		Vertex Verts = currentMesh.meshData->vertexList[index];

		//test if the vertex exsist already if not add it as a new VBO data reference

		// wrong comparrison need to check normalList.

		if(FLOATCOMPARE(tempVerts.x,Verts.x)&&

		   FLOATCOMPARE(tempVerts.y,Verts.y)&&

		   FLOATCOMPARE(tempVerts.z,Verts.z) )

		{

		 //comparison true so we reuse this vertex 

		 if(hasNormal)

		 {

		   //short form

		   Vertex Norm = currentMesh.meshData->normalList[index];

		  //test if the vertex exsist already if not add it as a new VBO data reference

		  // wrong comparrison need to check normalList.

		  if(FLOATCOMPARE(tempNorm.x,Norm.x)&&

		     FLOATCOMPARE(tempNorm.y,Norm.y)&&

		     FLOATCOMPARE(tempNorm.z,Norm.z) )

		  {

		 	//test if UV's exsist

		       if(hasUV)

			{

			 goto douv;

			 }

			 else

			  {

			   //comparison true so we reuse this vertex 

			   goto reuse;

			  }

		   }//compare normals

		}//end had normals

		//no normals so just UV coords maybe

		else if(hasUV)

		{

		  //goto douv;

		  douv:

	          {

		   //short form

		  TexUV UV = currentMesh.meshData->uvList[index];

		  if(FLOATCOMPARE(tempUV.u,UV.u)&&

		     FLOATCOMPARE(tempUV.v,UV.v))

		  {

			 goto reuse;

		   }

		}//end goto label

	}//else if has UV only

	else

	{

		  goto reuse;

	}

      }//if compare duplicate vertex 

	 

    }//end while actualVertexArraySize has a count

//the first data for actualVertexArraySize hence we start at 1 in the loop but ensure we do have a element in the vertex count

//procedual steps, the element was unique so add it as a new vertexsize, jump and increment the index buffer.

 currentMesh.triangleIndexBuffer[IBcount]= ActualVertexArraySize++;

 goto endElement;

 //jumped here, it is a old index, so reuse it, then increment the new index place

 reuse:

   currentMesh.triangleIndexBuffer[IBcount]= ib;

 newElement:

   IBcount++; //next indices position

}//end loop per trangle







1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users