Ejemplo n.º 1
0
            private static async Task <IReadOnlyList <Primitive> > ReadPrimitives(BufferedReader reader, int materialsCount, int vertexCount)
            {
                var primitives = new List <Primitive>();

                while (true)
                {
                    var token = await reader.ReadInt16().ConfigureAwait(false);

                    if (token == (short)Token.EndMesh)
                    {
                        return(primitives);
                    }

                    var primitiveType = (PrimitiveType)token;
                    if (!Enum.IsDefined(primitiveType))
                    {
                        throw new CmodException("Unknown primitive type");
                    }

                    var materialIndex = await reader.ReadInt32().ConfigureAwait(false);

                    if (materialIndex < 0 || materialIndex >= materialsCount)
                    {
                        throw new CmodException("Material index out of range");
                    }

                    var indexCount = await reader.ReadInt32().ConfigureAwait(false);

                    if (indexCount <= 0)
                    {
                        throw new CmodException("Index count out of range");
                    }

                    var primitive = new Primitive(primitiveType, materialIndex, indexCount);
                    for (var i = 0; i < indexCount; ++i)
                    {
                        var index = await reader.ReadInt32().ConfigureAwait(false);

                        if (index < 0 || index >= vertexCount)
                        {
                            throw new CmodException("Index out of range");
                        }

                        primitive.Indices.Add(index);
                    }

                    primitives.Add(primitive);
                }
            }
Ejemplo n.º 2
0
            private static async Task <int> ReadVertices(BufferedReader reader, IReadOnlyCollection <VertexAttribute> attributes)
            {
                if (await reader.ReadToken().ConfigureAwait(false) != Token.Vertices)
                {
                    throw new CmodException("Expected vertices");
                }

                var vertexCount = await reader.ReadInt32().ConfigureAwait(false);

                if (vertexCount <= 0)
                {
                    throw new CmodException("Vertex count out of range");
                }

                foreach (var attribute in attributes)
                {
                    attribute.Capacity = vertexCount;
                }

                for (var i = 0; i < vertexCount; ++i)
                {
                    foreach (var attribute in attributes)
                    {
                        await attribute.Read(reader).ConfigureAwait(false);
                    }
                }

                return(vertexCount);
            }