void ProcessTokens(string[] tokens)
        {
            switch (tokens[0].ToLower())
            {
            case "o":
                meshName = tokens[1];
                break;

            case "v":
                coords.Add(ParseCoord(tokens));
                break;

            case "vt":
            {
                uvs.Add(ParseUVCoord(tokens));
                break;
            }

            case "vn":
                normals.Add(ParseCoord(tokens));
                break;

            case "g":

                MakePrimFace();

                break;

            case "s":
                break;

            case "f":

                int[] vertIndices = new int[3];

                for (int vertexIndex = 1; vertexIndex <= 3; vertexIndex++)
                {
                    string[] indices = tokens[vertexIndex].Split('/');

                    int positionIndex = int.Parse(indices[0],
                                                  CultureInfo.InvariantCulture) - 1;

                    int texCoordIndex = -1;
                    int normalIndex   = -1;

                    if (indices.Length > 1)
                    {
                        if (int.TryParse(indices[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out texCoordIndex))
                        {
                            texCoordIndex--;
                        }
                        else
                        {
                            texCoordIndex = -1;
                        }
                    }

                    if (indices.Length > 2)
                    {
                        if (int.TryParse(indices[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out normalIndex))
                        {
                            normalIndex--;
                        }
                        else
                        {
                            normalIndex = -1;
                        }
                    }

                    int hash = hashInts(positionIndex, texCoordIndex, normalIndex);

                    if (viewerVertexLookup.ContainsKey(hash))
                    {
                        vertIndices[vertexIndex - 1] = viewerVertexLookup[hash];
                    }
                    else
                    {
                        ViewerVertex vv = new ViewerVertex();
                        vv.v = coords[positionIndex];
                        if (normalIndex > -1)
                        {
                            vv.n = normals[normalIndex];
                        }
                        if (texCoordIndex > -1)
                        {
                            vv.uv = uvs[texCoordIndex];
                        }
                        faceVertices.Add(vv);
                        vertIndices[vertexIndex - 1] = viewerVertexLookup[hash] = faceVertices.Count - 1;
                    }
                }

                facePolygons.Add(new ViewerPolygon(vertIndices[0], vertIndices[1], vertIndices[2]));
                break;

            case "mtllib":
                break;

            case "usemtl":
                break;
            }
        }
Пример #2
0
        private void ProcessTokens(string[] tokens)
        {
            switch (tokens[0].ToLower())
            {
                case "o":
                    meshName = tokens[1];
                    break;

                case "v":
                    coords.Add(ParseCoord(tokens));
                    break;

                case "vt":
                    {
                        uvs.Add(ParseUVCoord(tokens));
                        break;
                    }

                case "vn":
                    normals.Add(ParseCoord(tokens));
                    break;

                case "g":

                    MakePrimFace();

                    break;

                case "s":
                    break;

                case "f":

                    int[] vertIndices = new int[3];

                    for (int vertexIndex = 1; vertexIndex <= 3; vertexIndex++)
                    {
                        string[] indices = tokens[vertexIndex].Split('/');

                        int positionIndex = int.Parse(indices[0],
                            CultureInfo.InvariantCulture) - 1;

                        int texCoordIndex = -1;
                        int normalIndex = -1;

                        if (indices.Length > 1)
                        {

                            if (int.TryParse(indices[1], System.Globalization.NumberStyles.Integer, CultureInfo.InvariantCulture, out texCoordIndex))
                                texCoordIndex--;
                            else texCoordIndex = -1;

                        }

                        if (indices.Length > 2)
                        {
                            if (int.TryParse(indices[1], System.Globalization.NumberStyles.Integer, CultureInfo.InvariantCulture, out normalIndex))
                                normalIndex--;
                            else normalIndex = -1;
                        }

                        int hash = hashInts(positionIndex, texCoordIndex, normalIndex);

                        if (viewerVertexLookup.ContainsKey(hash))
                            vertIndices[vertexIndex - 1] = viewerVertexLookup[hash];
                        else
                        {
                            ViewerVertex vv = new ViewerVertex();
                            vv.v = coords[positionIndex];
                            if (normalIndex > -1)
                                vv.n = normals[normalIndex];
                            if (texCoordIndex > -1)
                                vv.uv = uvs[texCoordIndex];
                            faceVertices.Add(vv);
                            vertIndices[vertexIndex - 1] = viewerVertexLookup[hash] = faceVertices.Count - 1;
                        }
                    }

                    facePolygons.Add(new ViewerPolygon(vertIndices[0], vertIndices[1], vertIndices[2]));
                    break;

                case "mtllib":
                    break;

                case "usemtl":
                    break;

                default:
                    break;
            }
        }