65.9K
CodeProject is changing. Read more.
Home

Get vertices from a mesh base object with Managed C++

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.50/5 (7 votes)

Apr 7, 2004

CPOL
viewsIcon

51254

How to get the vertices from a mesh base object with Managed C++.

Introduction

This article explains how to get the vertices from a mesh base object with Managed C++.

1. Create a custom vertex with only point and normals

public __value class CustomVertex{
    public :
        Vector3 p;
        Vector3 n;
        const static VertexFormats Format=(VertexFormat)
              ( VertexFormats::Position|VertexFormats::Normal);
};

2. Set an array of the custom vertex (it will be filled with the vertices of the base mesh created)

CustomVertex m_vMeshVertices[];

3. Set a DirectX 9.0 device

Device* m_pDevice;

4. Create the function which will get the vertices of the base mesh object

Mesh* pMesh;   //The mesh object from witch we will get vertices
int nb __gc[];  //Use it to get the number of vertice
CustomVertex v3; //Use it for GetType() in LockVertexBuffer
Array* pArr;      //Use it for the result of LockVertexBuffer
CustomVertex* pMvr; //Use it for the cast of the Array* to CustomVertex*
int i;
// Create a mesh base object : a box for example
pMesh=Mesh::Box(m_pDevice,0.1f,0.1f,0.1f); 
//Format nb and v3 to be used with LockVertexBuffer
nb=new int __gc[1];
__box CustomVertex* ovar = __box(v3);
//Get the nubmer of vertices of the mesh
nb[0]=pMesh->NumberVertices;
//Init our array of vertices 
m_vMeshVertices=new CustomVertex[pMesh->NumberVertices];
//Lock the vertex buffer
pArr=pMesh->LockVertexBuffer(ovar->GetType(),LockFlags::None,nb);
//Copy vertices to our array
for(i=0;iNumberVertices;i++)
{
     pMvr= __try_cast(pArr->GetValue(i));
     m_vMeshVertices[i].n=pMvr->n;
     m_vMeshVertices[i].p=pMvr->p;
}
//Unlock the vertex buffer and free the mesh
pMesh->UnlockVertexBuffer();

pMesh->Dispose();