Click here to Skip to main content
15,895,746 members
Articles / Desktop Programming / MFC

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

Rate me:
Please Sign up or sign in to vote.
1.00/5 (12 votes)
6 Apr 2004CPOL 51K   14   4
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

MC++
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)

MC++
CustomVertex m_vMeshVertices[];

3. Set a DirectX 9.0 device

MC++
Device* m_pDevice;

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

MC++
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;i<pmesh- />NumberVertices;i++)
{
     pMvr= __try_cast<customvertex* />(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();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Schneider Electric
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalthanx alot...and a suggestion... Pin
Member 12923816-Apr-04 1:30
Member 12923816-Apr-04 1:30 
GeneralGuide Pin
Johann Gerell7-Apr-04 0:31
Johann Gerell7-Apr-04 0:31 
GeneralLess would have been more Pin
Franz Brunner6-Apr-04 23:20
Franz Brunner6-Apr-04 23:20 
GeneralRe: Less would have been more Pin
Erik Thompson7-Apr-04 6:57
sitebuilderErik Thompson7-Apr-04 6:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.