# Triangles of Mesh

This topic is 2948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

	m_stream.open("log_level.txt");    // Clone the original mesh in a more simple only for readability (though not necessary)    ID3DXMesh *pClone = NULL;    m_Mesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM, D3DFVF_XYZ, m_pDevice, &pClone);    // get some data ...    const int nFaces = pClone->GetNumFaces(); // number of polygons    const int nVBVertices = pClone->GetNumVertices(); // number of vertices in the vertex buffer    const int nVertices = nFaces*3; // total number of vertices (unless we use the index buffer)    // Pointers that will point to the vertex and index buffer    WORD *pIndices = NULL;    D3DXVECTOR3 *pVertices = NULL;    // lock the index buffer and vertex of the mesh cloned    pClone->LockIndexBuffer(D3DLOCK_READONLY, (LPVOID*)&pIndices);    pClone->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);    // Goes through all the polygons of the mesh    for (int t=0; t < nVertices; t++)    {		m_stream << "Vertice " << t << ": " <<  pVertices[pIndices[t]].x << " " <<												pVertices[pIndices[t]].y << " " <<												pVertices[pIndices[t]].z << std::endl;	}	m_stream << "===============================================================" 		     << std::endl << std::endl << std::endl;    // Unlocks the buffers    pClone->UnlockVertexBuffer();    pClone->UnlockIndexBuffer();    // Release the clone    pClone->Release();	m_stream.close();

The log show:
Quote:
 Vertice 0: -77.5857 4.81243 -7.5613Vertice 1: 68.5184 4.81243 20.5482Vertice 2: -77.5857 4.81243 20.5482Vertice 3: 68.5184 4.81243 20.5482Vertice 4: -77.5857 4.81243 -7.5613//...Vertice 4645: -49.3102 8.39155 -7.6623Vertice 4646: -49.3102 5.49066 -7.6623Vertice 4647: -49.3102 8.39155 -7.6623Vertice 4648: -49.3102 5.49066 6.80519Vertice 4649: -49.3102 8.39155 6.80519

These are just the vertices of the mesh, right?

how do I get the indices of the vertices that make up each triangle??
(is the index buffer??)

Thanks

##### Share on other sites
Yes, from the index buffer. Each triangle is a "face." The index buffer is arranged with 3 vertex indices per face.

//face 0
WORD indexToFace0Vertex0; // index into the vertex buffer
WORD indexToFace0Vertex1;
WORD indexToFace0Vertex2;
// face 1
WORD indexToFace1Vertex0;
WORD indexToFace1Vertex1;
WORD indexToFace1Vertex2;
// etc.

NOTE: Different faces may use the same vertex.

##### Share on other sites
I did so now:
		m_stream.open("log_level.txt");    // Clone the original mesh in a more simple only for readability (though not necessary)    ID3DXMesh *pClone = NULL;    m_Mesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM, D3DFVF_XYZ, m_pDevice, &pClone);    // get some data ...    const int nFaces = pClone->GetNumFaces(); // number of polygons    const int nVBVertices = pClone->GetNumVertices(); // number of vertices in the vertex buffer    const int nVertices = nFaces*3; // total number of vertices (unless we use the index buffer)	const int trianglenum = pClone->GetNumFaces()*3;	int b = pClone->GetOptions();	if(b & D3DXMESH_32BIT){		m_stream << "32BIT" << std::endl;	}	WORD *indexbuffercopy = new WORD[trianglenum];	// Pointers that will point to the vertex and index buffer    WORD *pIndices = NULL;    D3DXVECTOR3 *pVertices = NULL;    // lock the index buffer and vertex of the mesh cloned    pClone->LockIndexBuffer(D3DLOCK_READONLY, (LPVOID*)&pIndices);    pClone->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);    // Goes through all the polygons of the mesh    for (int t=0; t < nVertices; t++)    {		m_stream << "Vertice " << t << ": " <<  pVertices[pIndices[t]].x << " " <<												pVertices[pIndices[t]].y << " " <<												pVertices[pIndices[t]].z << std::endl;	}	m_stream << "===============================================================" 		     << std::endl << std::endl << std::endl;	memcpy(indexbuffercopy, pIndices, sizeof(WORD)*trianglenum);    // Unlocks the buffers    pClone->UnlockVertexBuffer();    pClone->UnlockIndexBuffer();	for(int i=0; i<trianglenum;i++){		int z = indexbuffercopy;		m_stream << z << std::endl;	}    // Release the clone    pClone->Release();	m_stream.close();

and log show:
Quote:
 0323014//...3407340634053408340934103413341034093414

right?

where is it:
Quote:
 0 //indexToFace0Vertex03 //indexToFace0Vertex12 //indexToFace0Vertex23 //indexToFace1Vertex00 //indexToFace1Vertex11 //indexToFace1Vertex2//...

?

Thanks for help

##### Share on other sites
Looks like you've got it.

##### Share on other sites
Thanks Buckeye :)

I have my list of vertices and triangles! and now need the edges :(

I thought the following algorithm:
an algorithm nothing optimized could create a list indexed by the index of two vertices, then you scroll through the list of triangles to draw each pair of corner vertices of the index, find the list, if this pair in the list, then simply add this triangle as second triangle edge, failing that, insert a new one.

My thought is right?
Possesses some easiest and fastest?

I need to pass a function the following parameters:
Quote:
 addEdge( ai_index i_t1, ai_index i_t2, ai_index i_v1, ai_index i_v2 ) Parameters:i_t1 Index of the first adjacent triangle of winged edge.i_t2 Index of the second adjacent triangle of winged edge.i_v1 Index of the first vertex of the edge.i_v2 Index of the second vertex of the edge.

Thanks again

##### Share on other sites

What is an edge? A line connection two vertices. But which two vertices? two vertices that are on the same triangle. That's pretty much the whole thing.

##### Share on other sites
Quote:
 Original post by szecsWell, there were a thread about the edges alread, and you've got pretty decent solutions.What is an edge? A line connection two vertices. But which two vertices? two vertices that are on the same triangle. That's pretty much the whole thing.

Yes friend,
but I'm having trouble make the logic:

Quote:
 Parameters:i_t1 Index of the first adjacent triangle of winged edge.i_t2 Index of the second adjacent triangle of winged edge.i_v1 Index of the first vertex of the edge.i_v2 Index of the second vertex of the edge.

The first 2 parameters that are adjacent to the line. I found a comparison:
"The name of this structure comes from the body of a butterfly, imagining the two polygons as their wings and the edge as your body, separating its wings."
and the other two parameters is the first and second vertices so that the edge

I can not think of a way to organize this order (first adjacent triangle, second adjacent triangle, first vertex of the edge, second vertex of the edge)
:(

I found two very interesting images, which helped me understand "winged edge":

http://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/subdivision/img028.gif
and
http://cse.csusb.edu/tongyu/courses/cs520/images/mesh/Mesh_we2.jpg

##### Share on other sites
I'm using the following algorithm:

struct Edge{    unsigned short      vertexIndex[2];    unsigned short      faceIndex[2];};struct Triangle{    unsigned short      index[3];};long BuildEdges(long vertexCount, long triangleCount,                const Triangle *triangleArray, Edge *edgeArray){    long maxEdgeCount = triangleCount * 3;    unsigned short *firstEdge = new unsigned short[vertexCount + maxEdgeCount];    unsigned short *nextEdge = firstEdge + vertexCount;        for (long a = 0; a < vertexCount; a++) firstEdge[a] = 0xFFFF;        // First pass over all triangles. This finds all the edges satisfying the    // condition that the first vertex index is less than the second vertex index    // when the direction from the first vertex to the second vertex represents    // a counterclockwise winding around the triangle to which the edge belongs.    // For each edge found, the edge index is stored in a linked list of edges    // belonging to the lower-numbered vertex index i. This allows us to quickly    // find an edge in the second pass whose higher-numbered vertex index is i.        long edgeCount = 0;    const Triangle *triangle = triangleArray;    for (long a = 0; a < triangleCount; a++)    {        long i1 = triangle->index[2];        for (long b = 0; b < 3; b++)        {            long i2 = triangle->index;            if (i1 < i2)            {                Edge *edge = &edgeArray[edgeCount];                                edge->vertexIndex[0] = (unsigned short) i1;                edge->vertexIndex[1] = (unsigned short) i2;                edge->faceIndex[0] = (unsigned short) a;                edge->faceIndex[1] = (unsigned short) a;                                long edgeIndex = firstEdge[i1];                if (edgeIndex == 0xFFFF)                {                    firstEdge[i1] = edgeCount;                }                else                {                    for (;;)                    {                        long index = nextEdge[edgeIndex];                        if (index == 0xFFFF)                        {                            nextEdge[edgeIndex] = edgeCount;                            break;                        }                                                edgeIndex = index;                    }                }                                nextEdge[edgeCount] = 0xFFFF;                edgeCount++;            }                        i1 = i2;        }                triangle++;    }        // Second pass over all triangles. This finds all the edges satisfying the    // condition that the first vertex index is greater than the second vertex index    // when the direction from the first vertex to the second vertex represents    // a counterclockwise winding around the triangle to which the edge belongs.    // For each of these edges, the same edge should have already been found in    // the first pass for a different triangle. So we search the list of edges    // for the higher-numbered vertex index for the matching edge and fill in the    // second triangle index. The maximum number of comparisons in this search for    // any vertex is the number of edges having that vertex as an endpoint.        triangle = triangleArray;    for (long a = 0; a < triangleCount; a++)    {        long i1 = triangle->index[2];        for (long b = 0; b < 3; b++)        {            long i2 = triangle->index;            if (i1 > i2)            {                for (long edgeIndex = firstEdge[i2]; edgeIndex != 0xFFFF;                        edgeIndex = nextEdge[edgeIndex])                {                    Edge *edge = &edgeArray[edgeIndex];                    if ((edge->vertexIndex[1] == i1) &&                            (edge->faceIndex[0] == edge->faceIndex[1]))                    {                        edge->faceIndex[1] = (unsigned short) a;                        break;                    }                }            }                        i1 = i2;        }                triangle++;    }        delete[] firstEdge;    return (edgeCount);}

And my code which uses the function is:
   // Clone the original mesh in a more simple only for readability (though not necessary)    ID3DXMesh *pClone = NULL;    m_Mesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM, D3DFVF_XYZ, m_pDevice, &pClone);    // get some data ...    const int nFaces = pClone->GetNumFaces(); // number of polygons (triangles)    const int nVBVertices = pClone->GetNumVertices(); // number of vertices in the vertex buffer    const int nVertices = nFaces*3; // total number of vertices (unless we use the index buffer)	// Pointers that will point to the vertex and index buffer    D3DXVECTOR3 *pVertices = NULL;	Triangle *DXMeshIBPtr;    // lock the index buffer and vertex of the mesh cloned	pClone->LockIndexBuffer(D3DLOCK_READONLY, (void**)&DXMeshIBPtr);    pClone->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);	Triangle *triangleArray;	triangleArray = new Triangle[nFaces];	Edge *edgeArray;	edgeArray = new Edge[nFaces*3];	for(int NumInd = 0; NumInd < nFaces; NumInd++)	{		triangleArray[NumInd].index[0] = DXMeshIBPtr[NumInd].index[0];		triangleArray[NumInd].index[1] = DXMeshIBPtr[NumInd].index[1];		triangleArray[NumInd].index[2] = DXMeshIBPtr[NumInd].index[2];	}    // Unlocks the buffers    pClone->UnlockVertexBuffer();	pClone->UnlockIndexBuffer();    // Release the clone    pClone->Release();	BuildEdges(nFaces, nVBVertices, triangleArray, edgeArray);

But it is giving error (debug mode) in the line of the function:
long edgeIndex = firstEdge[i1];

The image of error:
http://img522.imageshack.us/img522/9034/semttulohc.png

Thanks

##### Share on other sites
looks like the first two parameters you pass to BuildEdges are in the wrong order.

##### Share on other sites
Not seen as a mistake so stupid?! I'm sorry friend, and thanks for the reply :D

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633384
• Total Posts
3011598
• ### Who's Online (See full list)

There are no registered users currently online

×