DirectX / C ++:Marching Cubes索引

时间:2016-05-26 15:45:05

标签: c++ algorithm indexing directx marching-cubes

我在DirectX环境中实现了Marching Cube算法(测试并玩得开心)。完成后,我注意到结果模型看起来严重扭曲,好像指数已关闭。

enter image description here

我试图提取索引,但我认为顶点已经正确排序,使用查找表,http://paulbourke.net/geometry/polygonise/处的示例。当前版本使用15 ^ 3卷。

Marching立方体正常迭代数组:

for (float iX = 0; iX < CellFieldSize.x; iX++){
    for (float iY = 0; iY < CellFieldSize.y; iY++){
        for (float iZ = 0; iZ < CellFieldSize.z; iZ++){
            MarchCubes(XMFLOAT3(iX*StepSize, iY*StepSize, iZ*StepSize), StepSize);
        }
    }
}

调用MarchCube函数:

void MC::MarchCubes(){

...

int Corner, Vertex, VertexTest, Edge, Triangle, FlagIndex, EdgeFlags;
float Offset;
XMFLOAT3 Color;
float CubeValue[8];
XMFLOAT3 EdgeVertex[12];
XMFLOAT3 EdgeNorm[12];

//Local copy 
for (Vertex = 0; Vertex < 8; Vertex++) {
    CubeValue[Vertex] = (this->*fSample)(
        in_Position.x + VertexOffset[Vertex][0] * Scale,
        in_Position.y + VertexOffset[Vertex][1] * Scale,
        in_Position.z + VertexOffset[Vertex][2] * Scale
  );  
}

FlagIndex = 0;

交叉口计算:

...
//Test vertices for intersection.
for (VertexTest = 0; VertexTest < 8; VertexTest++){
    if (CubeValue[VertexTest] <= TargetValue) 
        FlagIndex |= 1 << VertexTest;
}

//Find which edges are intersected by the surface.
EdgeFlags = CubeEdgeFlags[FlagIndex];
if (EdgeFlags == 0){
    return;
}

for (Edge = 0; Edge < 12; Edge++){
    if (EdgeFlags & (1 << Edge)) {
        Offset = GetOffset(CubeValue[EdgeConnection[Edge][0]], CubeValue[EdgeConnection[Edge][1]], TargetValue); // Get offset function definition. Needed!
        EdgeVertex[Edge].x = in_Position.x + VertexOffset[EdgeConnection[Edge][0]][0] + Offset * EdgeDirection[Edge][0] * Scale;
        EdgeVertex[Edge].y = in_Position.y + VertexOffset[EdgeConnection[Edge][0]][1] + Offset * EdgeDirection[Edge][1] * Scale;
        EdgeVertex[Edge].z = in_Position.z + VertexOffset[EdgeConnection[Edge][0]][2] + Offset * EdgeDirection[Edge][2] * Scale;

        GetNormal(EdgeNorm[Edge], EdgeVertex[Edge].x, EdgeVertex[Edge].y, EdgeVertex[Edge].z); //Need normal values
    }
}

原始实现被推送到DirectX的保存结构中。

for (Triangle = 0; Triangle < 5; Triangle++) {

    if (TriangleConnectionTable[FlagIndex][3 * Triangle] < 0) break;

    for (Corner = 0; Corner < 3; Corner++) {
        Vertex = TriangleConnectionTable[FlagIndex][3 * Triangle + Corner];3 * Triangle + Corner]);
        GetColor(Color, EdgeVertex[Vertex], EdgeNorm[Vertex]);
        Data.VertexData.push_back(XMFLOAT3(EdgeVertex[Vertex].x, EdgeVertex[Vertex].y, EdgeVertex[Vertex].z));
        Data.NormalData.push_back(XMFLOAT3(EdgeNorm[Vertex].x, EdgeNorm[Vertex].y, EdgeNorm[Vertex].z));
        Data.ColorData.push_back(XMFLOAT4(Color.x, Color.y, Color.z, 1.0f));
    }
}

(这与原始GL实施的顺序相同)

1 个答案:

答案 0 :(得分:0)

事实证明,我错过了一个显示运算符优先级的括号。

    EdgeVertex[Edge].x = in_Position.x + (VertexOffset[EdgeConnection[Edge][0]][0] + Offset * EdgeDirection[Edge][0]) * Scale;
    EdgeVertex[Edge].y = in_Position.y + (VertexOffset[EdgeConnection[Edge][0]][1] + Offset * EdgeDirection[Edge][1]) * Scale;
    EdgeVertex[Edge].z = in_Position.z + (VertexOffset[EdgeConnection[Edge][0]][2] + Offset * EdgeDirection[Edge][2]) * Scale;

更正,获得Visine;恢复了乐趣。