Mesh::~Mesh() { destroyD3DBuffers(); delete[] vertexData; delete[] triangleData; delete[] quadData; }
inline void setDirty() { destroyD3DBuffers(); }
void Mesh::createD3DBuffers() { destroyD3DBuffers(); // CREATE VERTEX BUFFER { int vertexBytes = numVertices * vertexStride * sizeof(float); fvf = D3DFVF_XYZ | D3DFVF_NORMAL | (numUVSets << D3DFVF_TEXCOUNT_SHIFT) | (vertexColors * D3DFVF_DIFFUSE); globalDirect3DDevice->CreateVertexBuffer ( vertexBytes, // Length 0, // Usage fvf, // FVF D3DPOOL_DEFAULT, // Pool &vertexBuffer, // ppVertexBuffer NULL // pSharedHandle ); void *vertexBufferData; vertexBuffer->Lock(0, vertexBytes, &vertexBufferData, 0); memcpy(vertexBufferData, vertexData, vertexBytes); vertexBuffer->Unlock(); } // CREATE INDEX BUFFER { int triangleBytes = numTriangles * sizeof(int) * 3; int quadBytes = numQuads * 2 * sizeof(int) * 3; int indexBytes = triangleBytes + quadBytes; globalDirect3DDevice->CreateIndexBuffer( indexBytes, // Length 0, // Usage D3DFMT_INDEX32, // Format D3DPOOL_DEFAULT, // Pool &indexBuffer, // ppIndexBuffer NULL // pSharedHandle ); int *indexBufferData; indexBuffer->Lock(0, indexBytes, (void **)(&indexBufferData), 0); // Simply copy in triangle data since it is in the right format memcpy(indexBufferData, triangleData, triangleBytes); // Quads needs some processing for (int i = 0; i < numQuads; i++) { int *q = quad(i); int offset = numTriangles * 3 + i * 6; indexBufferData[offset + 0] = q[0]; indexBufferData[offset + 1] = q[1]; indexBufferData[offset + 2] = q[3]; indexBufferData[offset + 3] = q[1]; indexBufferData[offset + 4] = q[2]; indexBufferData[offset + 5] = q[3]; } indexBuffer->Unlock(); } }