ID3DXMesh接口 创建自己的立方体网格
D3DXCreateMeshFVF
首先创建一个ID3DXMesh接口。 ID3DXMesh接口的说明可以参数龙书。
这里主要是用代码来讲解:
#define VERTEX_FVF (D3DFVF_XYZ | D3DFVF_TEX1) hr = D3DXCreateMeshFVF( 12, 24, D3DXMESH_MANAGED, VERTEX_FVF, m_pd3dDevice, &pMesh ); VERTEX vertices[] = { //正y面? {-5.0f, -5.0f, 0.0f, 0,1}, {-5.0f, 5.0f, 0.0f, 0,0}, {5.0f, 5.0f, 0.0f,1,0}, {5.0f, -5.0f, 0.0f, 1,1}, //侧¨¤面?r {5.0f, -5.0f, 0.0f, 0,1}, {5.0f, 5.0f, 0.0f,0,0}, {5.0f, 5.0f, 10.0f,1,0}, {5.0f, -5.0f, 10.0f, 1,1}, //后¨®面? {5.0f, -5.0f, 10.0f, 0,1}, {5.0f, 5.0f, 10.0f,0,0}, {-5.0f, 5.0f, 10.0f, 1,0}, {-5.0f, -5.0f, 10.0f, 1,1}, {-5.0f, -5.0f, 10.0f, 0,1}, {-5.0f, 5.0f, 10.0f, 0,0}, {-5.0f, 5.0f, 0.0f, 1,0}, {-5.0f, -5.0f, 0.0f, 1,1}, {-5.0f, 5.0f, 0.0f, 0,1}, {-5.0f, 5.0f, 10.0f, 0,0}, {5.0f, 5.0f, 10.0f, 1,0}, {5.0f, 5.0f, 0.0f, 1,1}, /* {-5.0f, -5.0f, 0.0f, 0,1}, {-5.0f, -5.0f, 10.0f, 0,0}, {5.0f, -5.0f, 10.0f, 1,0}, {5.0f, -5.0f, 0.0f, 1,1}*/ {-5.0f, -5.0f, 0.0f, 0,1}, {5.0f, -5.0f, 0.0f, 0,0}, {5.0f, -5.0f, 10.0f, 1,0}, {-5.0f, -5.0f, 10.0f, 1,1}, //底下的面。需要用逆时针。不然会被剔除 }; 把上面的顶点数据拷贝到 网格里面 void* pBuffer; pMesh->LockVertexBuffer(0,(void**)&pBuffer); memcpy(pBuffer,vertices,sizeof(vertices)); pMesh->UnlockVertexBuffer(); 接着创建 索引数据 WORD* i = 0; pMesh->LockIndexBuffer(0, (void**)&i); // fill in the front face index data i[0] = 0; i[1] = 1; i[2] = 2; i[3] = 0; i[4] = 2; i[5] = 3; // fill in the back face index data i[6] = 4; i[7] = 5; i[8] = 6; i[9] = 4; i[10] = 6; i[11] = 7; // fill in the top face index data i[12] = 8; i[13] = 9; i[14] = 10; i[15] = 8; i[16] = 10; i[17] = 11; // fill in the bottom face index data i[18] = 12; i[19] = 13; i[20] = 14; i[21] = 12; i[22] = 14; i[23] = 15; // fill in the left face index data i[24] = 16; i[25] = 17; i[26] = 18; i[27] = 16; i[28] = 18; i[29] = 19; // fill in the right face index data i[30] = 20; i[31] = 21; i[32] = 22; i[33] = 20; i[34] = 22; i[35] = 23; pMesh->UnlockIndexBuffer(); //下面是创建了6个集合,创建多少个接口。后面就需要绘制几个集合 DWORD* attributeEuffer = 0; pMesh->LockAttributeBuffer(0,&attributeEuffer); for ( int a = 0; a < 2; a++) { attributeEuffer[a] = 0; } for ( int b = 2; b < 4; b++) { attributeEuffer[b] = 1; } for ( int c = 4; c < 6; c++) { attributeEuffer[c] = 2; } //优化 hr = pMesh->OptimizeInplace( D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE, &adjacencyBuffer[0], 0, 0, 0); for ( int c = 6; c < 8; c++) { attributeEuffer[c] = 3; } for ( int c = 8; c < 10; c++) { attributeEuffer[c] = 4; } for ( int c = 10; c < 12; c++) { attributeEuffer[c] = 5; } pMesh->UnlockAttributeBuffer(); std::vector<DWORD> adjacencyBuffer(pMesh->GetNumFaces() * 3); pMesh->GenerateAdjacency(0.0f, &adjacencyBuffer[0]); for(int i = 0; i < 6; i++) { pMesh->DrawSubset( i ); }