Sign in to follow this  

Retrieving polygon information from a Mesh (X file)

This topic is 4609 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

Hi. I've a scene in X. I want to create a bsp tree of this scene. For that i need to retrieve information of the polygons from the scene. The information of the polygons are in the index buffer. With the index buffer i catch the vertex information from the vertex buffer. But the index buffer is giving me wrong index of the vertices that a polygon is formed. With that index i get a segmentation fault because is not within the vertex buffer memory bounds. To get an ideia i get a index above 1 million. I will post some code that i use to get the index and vertex buffer and how i read it. To retrieve the index buffer: <code> LPDIRECT3DINDEXBUFFER9 ppIB; m_Mesh->GetIndexBuffer(&ppIB); D3DINDEXBUFFER_DESC pDesc; ppIB->GetDesc(&pDesc); uint32 numBytesIndexBufferEntry = (pDesc.Format == D3DFMT_INDEX16) ? 2 : 4; uint32 size = m_Mesh->GetNumFaces() * 3 * numBytesIndexBufferEntry; void* memTempIndexBuffer; ppIB->Lock(0, size, &memTempIndexBuffer, D3DLOCK_READONLY); memIndexBuffer = malloc(size); memcpy(memIndexBuffer, memTempIndexBuffer, size); ppIB->Unlock(); </code> Retrieve the vertex buffer <code> LPDIRECT3DVERTEXBUFFER9 ppVB; m_Mesh->GetVertexBuffer(&ppVB); D3DVERTEXBUFFER_DESC pVertexDesc; ppVB->GetDesc(&pVertexDesc); void* memTempVertexBuffer; ppVB->Lock(0, pVertexDesc.Size, &memTempVertexBuffer, D3DLOCK_READONLY); memVertexBuffer = malloc(pVertexDesc.Size); memcpy(memVertexBuffer, memTempVertexBuffer, pVertexDesc.Size); ppVB->Unlock(); </code> Now a polygon is composed by three vertices. Read the vertex index and read the vertex information with that vertex index: <code> uint32 indexBufferFormat = model->GetSizeOfIndexBufferEntry(); uint32 m_NumFaces = model->GetNumFaces(); void* memIndexBuffer = model->GetMemIndexBuffer(); void* memVertexBuffer = model->GetMemVertexBuffer(); for(uint32 i = 0; i < m_NumFaces; i++) { uint32 vertice0, vertice1, vertice2; if(indexBufferFormat == 2) { vertice0 = *(((UINT16*) memIndexBuffer) + (i * 3) + 0); vertice1 = *(((UINT16*) memIndexBuffer) + (i * 3) + 1); vertice2 = *(((UINT16*) memIndexBuffer) + (i * 3) + 2); } else { vertice0 = *(((uint32*) memIndexBuffer) + (i * 3) + 0); vertice1 = *(((uint32*) memIndexBuffer) + (i * 3) + 1); vertice2 = *(((uint32*) memIndexBuffer) + (i * 3) + 2); } VertexPTN v0 = *((VertexPTN*) memVertexBuffer + vertice0); VertexPTN v1 = *((VertexPTN*) memVertexBuffer + vertice1); VertexPTN v2 = *((VertexPTN*) memVertexBuffer + vertice2); uint32 indice_material=0; polygons.push_back(BspPolygon(v0,v1,v2,indice_material)); </code>

Share this post


Link to post
Share on other sites
I resolved the problem. The variable indexBufferFormat was wrong so it was giving odd indexes. Thanks to all.

Share this post


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