Exemple #1
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);
            }
Exemple #2
0
            private static async ValueTask <Material> ReadMaterial(BufferedReader reader)
            {
                var material = new Material();

                while (true)
                {
                    switch (await reader.ReadToken().ConfigureAwait(false))
                    {
                    case Token.Diffuse:
                        material.Diffuse = await reader.ReadColor().ConfigureAwait(false);

                        break;

                    case Token.Specular:
                        material.Specular = await reader.ReadColor().ConfigureAwait(false);

                        break;

                    case Token.Emissive:
                        material.Emissive = await reader.ReadColor().ConfigureAwait(false);

                        break;

                    case Token.SpecularPower:
                        material.SpecularPower = await reader.ReadFloat1().ConfigureAwait(false);

                        break;

                    case Token.Opacity:
                        material.Opacity = await reader.ReadFloat1().ConfigureAwait(false);

                        break;

                    case Token.Blend:
                        material.BlendMode = await reader.ReadBlendMode().ConfigureAwait(false);

                        break;

                    case Token.Texture:
                        var textureSemantic = await reader.ReadTextureSemantic().ConfigureAwait(false);

                        var textureFile = await reader.ReadCmodString().ConfigureAwait(false);

                        material.Textures.Add(textureSemantic, textureFile);
                        break;

                    case Token.EndMaterial:
                        return(material);

                    default:
                        throw new CmodException("Unexpected token in material");
                    }
                }
            }
Exemple #3
0
            private static async Task <IReadOnlyList <VertexAttribute> > ReadVertexDescription(BufferedReader reader)
            {
                if (await reader.ReadToken().ConfigureAwait(false) != Token.VertexDesc)
                {
                    throw new CmodException("Expected vertex description");
                }

                var attributes = new List <VertexAttribute>();

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

                    if (token == (short)Token.EndVertexDesc)
                    {
                        return(attributes);
                    }

                    var attribute = (AttributeType)token;
                    if (!Enum.IsDefined(attribute))
                    {
                        throw new CmodException("Unknown vertex attribute");
                    }

                    if (attributes.Any(a => a.AttributeType == attribute))
                    {
                        throw new CmodException("Duplicate vertex attribute");
                    }

                    var format = await reader.ReadAttributeFormat().ConfigureAwait(false);

                    if (!Enum.IsDefined(format))
                    {
                        throw new CmodException("Invalid attribute format");
                    }

                    attributes.Add(VertexAttribute.Create(attribute, format));
                }
            }