Make a index buffer

Started by
24 comments, last by XVincentX 15 years, 11 months ago
I thought you were going to remove the duplicate vertices and save the mesh to a new mesh file. Then you can use the new mesh file in your final application. You only need to change it once, correct?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Advertisement
I was thinking to the same thing: rewrinte on the original file the new vertex and index buffer.
I had a discussion with my friend and he said me his implementation, with run time index buffer generation, with same mesh and epsilon = 0.0001, uses only 2 seconds.
It's very strange.
He gave me his implementation...i'm trying to understand something. Someone can suggest something?
protected static void Triangulize(Model3D model)        {            int totalVertices = 0;            int strideSize = model.Stride;            float[] vertexTemp = new float[strideSize];            List<float> newVerticesList = new List<float>();            List<int> newIndexList = new List<int>();            for (int i = 0; i < model.VertexCount; i++)            {                //per ogni vertice                //salvo il vertice                for (int j = 0; j < strideSize; j++)                {                    vertexTemp[j] = model.Data;<br>                }<br><br>                <span class="cpp-comment">//controlla se già esiste</span><br>                <span class="cpp-keyword">int</span> currentVertex = -<span class="cpp-number">1</span>;<br>                <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> j = <span class="cpp-number">0</span>; j &lt; totalVertices; j++)<br>                {<br>                    <span class="cpp-comment">//confronta il vertice attuale con quelli già inseriti</span><br>                    <span class="cpp-keyword">bool</span> equal = <span class="cpp-keyword">true</span>;<br>                    <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> k = <span class="cpp-number">0</span>; k &lt; strideSize; k++)<br>                    {<br>                        <span class="cpp-keyword">if</span> (Math.Abs(vertexTemp[k] - newVerticesList[j * strideSize + k])&lt;epsilon)<br>                        {<br>                            equal = <span class="cpp-keyword">false</span>;<br>                            <span class="cpp-keyword">break</span>;<br>                        }<br>                    }<br><br>                    <span class="cpp-keyword">if</span> (equal)<br>                    {<br>                        currentVertex = j;<br>                        <span class="cpp-keyword">break</span>;<br>                    }<br>                }<br><br>                <span class="cpp-keyword">if</span> (currentVertex == -<span class="cpp-number">1</span>)<br>                {<br>                    <span class="cpp-comment">//nuovo vertice, aggiungere</span><br>                    newIndexList.Add(totalVertices);<br>                    totalVertices++;<br>                    <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> j = <span class="cpp-number">0</span>; j &lt; strideSize; j++)<br>                        newVerticesList.Add(vertexTemp[j]);<br>                }<br>                <span class="cpp-keyword">else</span><br>                {<br>                    <span class="cpp-comment">//il vertice già esiste, aggiungere solo l'indice</span><br>                    newIndexList.Add(currentVertex);<br>                }<br>            }<br><br>            model.Data.Clear();<br>            model.Data.AddRange(newVerticesList);<br>            model.IndexData.Clear();<br>            model.IndexData.AddRange(newIndexList);<br>        }<br><br></pre></div><!–ENDSCRIPT–> 
Your friend's algorithm (method, sequence) does the same thing as the algorithm you came up with, but it is more efficient (faster, quicker).

Part of that efficiency (quickness, speed) is because the new algorithm tests the data in the model's vertex buffer without copying it to a new list.

Time is also saved by not converting (changing, copying) the vertices into D3DXVector3 structures before the epsilon comparison (test) is made.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I modified the code in this way

		std::vector<float> OrigVertices;		std::vector<float> NewVertices;	std::vector<unsigned int> OptIndices;	for (UINT i = 0; i < this->ColladaBuffer.Indici;i++)	{		Indices = i;	}	OptIndices.insert(OptIndices.begin(),Indices,Indices + this->ColladaBuffer.Indici);	OrigVertices.insert(OrigVertices.begin(),buffer,buffer + bufsize);	NewVertices.insert(NewVertices.begin(),OrigVertices.begin(),OrigVertices.begin() + 9);	{//		D3DVECTOR Vx,Nx,Tx;//		D3DVECTOR VxN,NxN,TxN;		int OrigVertIdx, NewVertIdx;		bool DuplicateV;		for (int i = 9; i < (OrigVertices.size());i += 9)		{			OrigVertIdx = i/9;/*			Vx.x = OrigVertices;			Vx.y = OrigVertices[i+1];			Vx.z = OrigVertices[i+2];			Nx.x = OrigVertices[i+3];			Nx.y = OrigVertices[i+4];			Nx.z = OrigVertices[i+5];			Tx.x = OrigVertices[i+6];			Tx.y = OrigVertices[i+7];			Tx.z = OrigVertices[i+8];*/			DuplicateV = false;			for (int j = 0; j < (NewVertices.size());j +=9 )			{/*				VxN.x = NewVertices[j];				VxN.y = NewVertices[j+1];				VxN.z = NewVertices[j+2];				NxN.x = NewVertices[j+3];				NxN.y = NewVertices[j+4];				NxN.z = NewVertices[j+5];				TxN.x = NewVertices[j+6];				TxN.y = NewVertices[j+7];				TxN.z = NewVertices[j+8];*/				if (fabs(OrigVertices-NewVertices[j]) < Epsilon && fabs(OrigVertices[i+1] - NewVertices[j+1]) < Epsilon && fabs(OrigVertices[i+2] - NewVertices[j+2]) < Epsilon)				{					NewVertIdx = j/9;					OptIndices[OrigVertIdx] = NewVertIdx;					DuplicateV = true;					break;				}			}			if (!DuplicateV)			{				NewVertIdx = (NewVertices.size())/9;				NewVertices.insert(NewVertices.end(),OrigVertices.begin() + i,OrigVertices.begin() + i + 9);				OptIndices[OrigVertIdx] = NewVertIdx ;			}		}	}


Now the code requires 6,7 seconds.
I should try now to remove completely the vector's usage.
I did it:
Debug mode: 2490 ms
Release: 140

The problem was in project configuration: Basic Runtime Checks to default speeded up the entire project!

This topic is closed to new replies.

Advertisement