Game Development Reference
In-Depth Information
// Define the triangles of the box
WORD*i=0;
Mesh->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 right face index data
i[30] = 20; i[31] = 21; i[32] = 22;
i[33] = 20; i[34] = 22; i[35] = 23;
Mesh->UnlockIndexBuffer();
Once the geometry of the mesh has been written, we must not forget
to specify the subset in which each triangle exists. Recall that the
attribute buffer stores the subset to which each triangle in the mesh
belongs. In this sample, we specify that the first four triangles defined
in the index buffer exist in subset 0, the next four triangles exist in sub-
set 1, and the last four triangles (12 total) exist in subset 2. We express
this in code as follows:
DWORD* attributeBuffer = 0;
Mesh->LockAttributeBuffer(0, &attributeBuffer);
for(inta=0;a<4;a++) // triangles 1-4
attributeBuffer[a] = 0; // subset 0
for(intb=4;b<8;b++) // triangles 5-8
attributeBuffer[b] = 1; // subset 1
for(intc=8;c<12;c++) // triangles 9-12
attributeBuffer[c] = 2; // subset 2
Mesh->UnlockAttributeBuffer();
Now we have a created mesh that contains valid data. We could render
the mesh at this point, but let's optimize it first. Note that for a trivial
box mesh, nothing is really gained by optimizing the mesh data, but
nonetheless we get practice using the ID3DXMesh interface methods.
In order to optimize a mesh, we first need to compute the adjacency
info of the mesh:
std::vector<DWORD> adjacencyBuffer(Mesh->GetNumFaces() * 3);
Mesh->GenerateAdjacency(0.0f, &adjacencyBuffer[0]);
Then we can optimize the mesh, as shown here:
hr = Mesh->OptimizeInplace(
D3DXMESHOPT_ATTRSORT |
D3DXMESHOPT_COMPACT |
D3DXMESHOPT_VERTEXCACHE,
Team-Fly ®
Search WWH ::




Custom Search