I tried the code below, but I have incorrect indices. I'm trying to render lines using CGAL and 3D Delaunay, but I get correct indices. It was hard to get the indices of the 3D Delaunay. I don't know which part is wrong, I try with 4 vertices, it looks correct, but with more than 5 indices I get wrong triangles...
std::vector<int> triangles;
std::map<Delaunay3::Vertex_handle, int> index_of_vertex;
int j = 0;
for (Delaunay3::Finite_vertices_iterator it = dt.finite_vertices_begin();
it != dt.finite_vertices_end(); ++it, ++j) {
index_of_vertex[it.base()] = j;
}
for (Delaunay3::Finite_facets_iterator itFacet = dt.finite_facets_begin();
itFacet != dt.finite_facets_end(); itFacet++) {
triangles.push_back(index_of_vertex[itFacet->first->vertex(0)]);
triangles.push_back(index_of_vertex[itFacet->first->vertex(1)]);
triangles.push_back(index_of_vertex[itFacet->first->vertex(2)]);
triangles.push_back(index_of_vertex[itFacet->first->vertex(0)]);
triangles.push_back(index_of_vertex[itFacet->first->vertex(2)]);
triangles.push_back(index_of_vertex[itFacet->first->vertex(3)]);
triangles.push_back(index_of_vertex[itFacet->first->vertex(1)]);
triangles.push_back(index_of_vertex[itFacet->first->vertex(2)]);
triangles.push_back(index_of_vertex[itFacet->first->vertex(3)]);
}
std::vector<WORD> lineIndex;
lineIndex.resize(triangles.size() * sizeof(int) * 4);
int l, t;
for (l = 0, t = 0; t < triangles.size(); t += 4) {
// Each triangle has 3 lines, so D3D_PRIMITIVE_TOPOLOGY_LINELIST needs 6 vertices
// Each vertex has to be listed twice
lineIndex[l] = triangles[t];
l++;
lineIndex[l] = triangles[t + 1];
l++;
lineIndex[l] = triangles[t + 1];
l++;
lineIndex[l] = triangles[t + 2];
l++;
lineIndex[l] = triangles[t + 2];
l++;
lineIndex[l] = triangles[t];
l++;
}
// Fill in a buffer description for drawing only the triangle outlines
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = numTriangleVertices * 3 * sizeof(int); // <---- from the function
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = static_cast<void*>(lineIndex.data());
pd3dDevice->CreateBuffer(&bd, &InitData, &g_pTriOutlineIndexBuffer);