Jump to content
  • Advertisement
Sign in to follow this  
VitaliBR

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.

If you intended to correct an error in the post then please contact us.

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.5613
Vertice 1: 68.5184 4.81243 20.5482
Vertice 2: -77.5857 4.81243 20.5482
Vertice 3: 68.5184 4.81243 20.5482
Vertice 4: -77.5857 4.81243 -7.5613

//...

Vertice 4645: -49.3102 8.39155 -7.6623
Vertice 4646: -49.3102 5.49066 -7.6623
Vertice 4647: -49.3102 8.39155 -7.6623
Vertice 4648: -49.3102 5.49066 6.80519
Vertice 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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:
0
3
2
3
0
1
4

//...

3407
3406
3405
3408
3409
3410
3413
3410
3409
3414


right?

where is it:
Quote:
0 //indexToFace0Vertex0
3 //indexToFace0Vertex1
2 //indexToFace0Vertex2

3 //indexToFace1Vertex0
0 //indexToFace1Vertex1
1 //indexToFace1Vertex2

//...

?

Thanks for help

Share this post


Link to post
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 this post


Link to post
Share on other sites
Well, 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.

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
Well, 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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!