我正在实现一个行进立方体算法,通常基于Paul Bourke的实现,并进行了一些重大调整:
- 标量场的预计算(浮点值)
- 使用 std::map 避免最终列表中的重复顶点
- 顶点存储以可视化 Ogre3D 中的最终网格
基本上我改变了他近 80% 的代码。我生成的网格有一些难看的 terrasses,我不知道如何避免它们。我认为对标量字段使用浮点可以完成这项工作。这是常见的效果吗?你怎么能避免它?
计算边上的顶点位置。(cell.val[p1] 包含给定顶点的标量值):
//if there is an intersection on this edge
if (cell.iEdgeFlags & (1 << iEdge))
{
const int* edge = a2iEdgeConnection[iEdge];
int p1 = edge[0];
int p2 = edge[1];
//find the approx intersection point by linear interpolation between the two edges and the density value
float length = cell.val[p1] / (cell.val[p2] + cell.val[p1]);
asEdgeVertex[iEdge] = cell.p[p1] + length * (cell.p[p2] - cell.p[p1]);
}
你可以在这里找到完整的源代码:https ://github.com/DieOptimistin/MarchingCubes 我使用 Ogre3D 作为这个例子的库。