65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Managed C++ 从网格基对象获取顶点

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.50/5 (7投票s)

2004年4月7日

CPOL
viewsIcon

51707

如何使用托管 C++ 从网格基对象获取顶点。

引言

本文档解释了如何使用托管 C++ 从网格基对象获取顶点。

1. 创建一个只包含点和法线的自定义顶点

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

2. 设置自定义顶点数组(它将被创建的基网格的顶点填充)

CustomVertex m_vMeshVertices[];

3. 设置 DirectX 9.0 设备

Device* m_pDevice;

4. 创建获取基网格对象顶点的函数

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();
© . All rights reserved.