Ejemplo n.º 1
0
    public static IReadOnlyCollection <int> GetTriangles <TVertex>(
        this IDivisibleMesh <TVertex> mesh, VertexWindingOrder winding)
        where TVertex : struct
    {
        Contracts.Requires.That(mesh != null);
        Contracts.Requires.That(winding.IsValidEnumValue());

        return(new ReadOnlyCollection <int>(mesh.EnumerateTriangles(winding), mesh.Offsets.Count));
    }
Ejemplo n.º 2
0
        public static void CopyIn <TVertex>(
            IMeshDataTransfer <TVertex> instance, IDivisibleMesh <TVertex> mesh, VertexWindingOrder winding)
            where TVertex : struct
        {
            Contracts.Requires.That(instance != null);
            Contracts.Requires.That(mesh != null);
            Contracts.Requires.That(
                mesh.Vertices.Count <= instance.MaxVertices || instance.MaxVertices == Capacity.Unbounded);
            Contracts.Requires.That(winding.IsValidEnumValue());

            DivisibleMesh.VerifyContracts(mesh);
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public void CopyIn(IDivisibleMesh <TVertex> mesh, VertexWindingOrder winding)
        {
            IMeshDataTransferContracts.CopyIn(this, mesh, winding);

            var triangles = mesh.GetTriangles(winding);

            this.IndicesCount = triangles.Count;
            this.indices.CopyIn(
                CountedEnumerable.New(triangles.Select(index => (ushort)index), triangles.Count));

            this.VerticesCount = mesh.Vertices.Count;
            this.vertices.CopyIn(mesh.Vertices.AsCounted(), this.MaxVertices);
        }
Ejemplo n.º 4
0
    public static IEnumerable <int> EnumerateTriangles <TVertex>(
        this IDivisibleMesh <TVertex> mesh, VertexWindingOrder winding)
        where TVertex : struct
    {
        Contracts.Requires.That(mesh != null);
        Contracts.Requires.That(winding.IsValidEnumValue());

        var offsets      = mesh.Offsets.GetEnumerator();
        int globalOffset = 0;

        switch (winding)
        {
        case VertexWindingOrder.Clockwise:
            foreach (var group in mesh.Groups)
            {
                for (int count = 0; count < group.Offsets; count++)
                {
                    var success = offsets.MoveNext();
                    Contracts.Assert.That(success);
                    yield return(globalOffset + offsets.Current);
                }

                globalOffset += group.Vertices;
            }

            yield break;

        case VertexWindingOrder.Counterclockwise:
            foreach (var group in mesh.Groups)
            {
                // IDivisibleMesh<TVertex> is always stored in clockwise winding order
                // so the last 2 offsets of each triangle need to be swapped to become counterclockwise
                for (int count = 0; count < group.Triangles; count++)
                {
                    // return the first offset as normal
                    var success = offsets.MoveNext();
                    Contracts.Assert.That(success);
                    yield return(globalOffset + offsets.Current);

                    // temporarily store the second offset
                    success = offsets.MoveNext();
                    Contracts.Assert.That(success);
                    var tempOffset = offsets.Current;

                    // return the third offset as though it were the second offset
                    success = offsets.MoveNext();
                    Contracts.Assert.That(success);
                    yield return(globalOffset + offsets.Current);

                    // return the second offset (stored) as though it were the third offset
                    yield return(globalOffset + tempOffset);
                }

                globalOffset += group.Vertices;
            }

            yield break;

        default: throw InvalidEnumArgument.CreateException(nameof(winding), winding);
        }
    }