Esempio n. 1
0
        void AppendIndex(int index, ObjLine line)
        {
            string[] indices = line.Tokens[index].Split('/');

            // Required: Position
            int positionIndex = int.Parse(indices[0], CultureInfo.InvariantCulture) - positionsIndexOffset;
            int texCoordIndex = -1;
            int normalIndex   = -1;

            // Optional: Texture coordinate
            if (indices.Length > 1 && indices[1].Equals(string.Empty) == false)
            {
                texCoordIndex = int.Parse(indices[1]) - textureCoordinatesIndexOffset;
            }

            // Optional: Normal
            if (indices.Length > 2 && indices[2].Equals(string.Empty) == false)
            {
                normalIndex = int.Parse(indices[2]) - normalsIndexOffset;
            }

            // Build vertex
            var vertex = new PositionNormalTextured {
                Position = positions[positionIndex]
            };

            if (texCoordIndex >= 0)
            {
                vertex.TextureCoordinates = textureCoordinates[texCoordIndex];
            }

            if (normalIndex >= 0)
            {
                vertex.Normal = normals[normalIndex];
            }

            // check if the vertex does not already exists
            string hash = vertex.ToString();
            int    vertexIndex;

            if (!registeredVertices.TryGetValue(hash, out vertexIndex))
            {
                stagingVertices.Add(vertex);
                vertexIndex = stagingVertices.Count - 1;
                registeredVertices.Add(hash, vertexIndex);
            }

            stagingIndices.Add(vertexIndex);
        }