Exemple #1
0
        private void Polygonize(int x, int y, int z)
        {
            if (Cells[x, y, z] == null)
                return;

            int cube_index = 0;
            for (int i = 0; i < 8; i++)
            {
                if (Sampler.Sample(new Vector3(x, y, z) + CornerDeltas[i]) < 0)
                    cube_index |= 1 << i;
            }

            if (cube_index == 0 || cube_index == 255)
                return;

            Cells[x, y, z].Vertices = new Vertex[VerticesNumberTable[cube_index]];
            /*for (int i = 0; i < 12; i++)
            {
                Cells[x, y, z].Edges[i].A = new Vector3(x, y, z) + CornerDeltas[EdgePairs[i, 0]];
                Cells[x, y, z].Edges[i].B = new Vector3(x, y, z) + CornerDeltas[EdgePairs[i, 1]];

                Cells[x, y, z].Edges[i].ValueA = Sampler.Sample(Cells[x, y, z].Edges[i].A);
                Cells[x, y, z].Edges[i].ValueB = Sampler.Sample(Cells[x, y, z].Edges[i].B);
            }*/

            int v_index = 0;
            Cells[x, y, z].Vertices[0] = new Vertex();
            for (int e = 0; e < EdgesTable.GetLength(1); e++)
            {
                if (EdgesTable[cube_index, e] == -2)
                    break;
                if (EdgesTable[cube_index, e] == -1)
                {
                    v_index++;
                    if (v_index < Cells[x, y, z].Vertices.Length)
                        Cells[x, y, z].Vertices[v_index] = new Vertex();
                    continue;
                }

                //Cells[x, y, z].Vertices[v_index].Index = v_index;
                Cells[x, y, z].Vertices[v_index].Edges.Add(EdgesTable[cube_index, e]);
                Cells[x, y, z].Edges[EdgesTable[cube_index, e]].Vertices.Add(Cells[x, y, z].Vertices[v_index]);
                //Cells[x, y, z].Edges[EdgesTable[cube_index, e]].Flipped = Cells[x, y, z].Edges[EdgesTable[cube_index, e]].ValueA < 0;
            }

            foreach (Vertex v in Cells[x, y, z].Vertices)
            {
                Vertex tx = v;
                if (v == null) //for edges 241/243, which were originally marked as having 2 vertices...?
                    continue;
                Vector3 point = new Vector3();

                if (VertexMode != VertexModes.Block)
                {
                    //QEF3D qef = new QEF3D();
                    QEFProper.QEFSolver qef = new QEFProper.QEFSolver();
                    VertexPlacement qem = new VertexPlacement();
                    for (int e_i = 0; e_i < tx.Edges.Count; e_i++)
                    {
                        Edge e = Cells[x, y, z].Edges[tx.Edges[e_i]];
                        if (VertexMode == VertexModes.Edges)
                            point += e.GetIntersection();
                        else if (VertexMode == VertexModes.QEF)
                            qef.Add(e.GetIntersection() - new Vector3(x, y, z), Sampler.GetNormal(e.GetIntersection()));
                        else
                            qem.AddPlane(e.GetIntersection() - new Vector3(x, y, z), Sampler.GetNormal(e.GetIntersection()));
                    }

                    if (VertexMode == VertexModes.Edges)
                        point /= (float)tx.Edges.Count;
                    else if (VertexMode == VertexModes.QEF)
                        point = qef.Solve(1e-6f, 4, 1e-6f) + new Vector3(x, y, z);
                    else
                        point = qem.Solve() + new Vector3(x, y, z);
                }
                else
                    point = new Vector3(x, y, z) + Vector3.One * 0.5f;
                //point = Vector3.Clamp(point, new Vector3(x, y, z), new Vector3(x + 1, y + 1, z + 1));

                tx.Position = point;
                Vector3 norm = Sampler.GetNormal(point);
                Vector3 c_v = norm * 0.5f + Vector3.One * 0.5f;
                c_v.Normalize();
                Color clr = new Color(c_v);
                if (!UseFlatShading)
                {
                    tx.Index = Vertices.Count;
                    VertexPositionColorNormal pv = new VertexPositionColorNormal(tx.Position, clr, norm);
                    Vertices.Add(pv);
                }
                else
                {
                    tx.Index = CalculatedVertices.Count;
                    VertexPositionColor pv = new VertexPositionColor(tx.Position, clr);
                    CalculatedVertices.Add(pv);
                }

                VertexPositionColor[] vs = new VertexPositionColor[24];
                Color c = Color.Red;
                float vx = tx.Position.X;
                float vy = tx.Position.Y;
                float vz = tx.Position.Z;
                float r = 0.25f;
                vs[0] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + 0)), c);
                vs[1] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + 0)), c);
                vs[2] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + 0)), c);
                vs[3] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + 0)), c);
                vs[4] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + 0)), c);
                vs[5] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + 0)), c);
                vs[6] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + 0)), c);
                vs[7] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + 0)), c);

                vs[8] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + r)), c);
                vs[9] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + r)), c);
                vs[10] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + r)), c);
                vs[11] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + r)), c);
                vs[12] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + r)), c);
                vs[13] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + r)), c);
                vs[14] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + r)), c);
                vs[15] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + r)), c);

                vs[16] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + 0)), c);
                vs[17] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + r)), c);
                vs[18] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + 0)), c);
                vs[19] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + r)), c);

                vs[20] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + 0)), c);
                vs[21] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + r)), c);
                vs[22] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + 0)), c);
                vs[23] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + r)), c);

                OutlineBuffer.SetData<VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 24, VertexPositionColor.VertexDeclaration.VertexStride);
                OutlineLocation += 24;
            }
        }