コード例 #1
0
ファイル: OFFLoader.cs プロジェクト: segafult/SCSRaytracer
        public override void ParseFaces(Mesh parent, bool smooth)
        {
            int pastLastIndex = indexOfFaces + countFaces;
            int[] indexs = new int[5];
            MeshTriangle triangle;

            for (int i = indexOfFaces; i < pastLastIndex; i++)
            {
                string[] tokens = fileLines[i].Split(' ');
                int numVerts = Convert.ToInt32(tokens[0]);

                switch (numVerts)
                {
                    case 3: //Face is a triangle
                        for (int j = 1; j < 4; j++)
                        {
                            indexs[j] = Convert.ToInt32(tokens[j]);
                        }
                        //Create new triangle
                        if (smooth)
                            triangle = new SmoothMeshTriangle(parent);
                        else
                            triangle = new FlatMeshTriangle(parent);
                        triangle.SetVertexIndices(indexs[1], indexs[2], indexs[3]);
                        //Update list of faces for given vertexs using current index
                        parent.vertexFaces[indexs[1]].Add(parent.countTriangles);
                        parent.vertexFaces[indexs[2]].Add(parent.countTriangles);
                        parent.vertexFaces[indexs[3]].Add(parent.countTriangles);
                        parent.countTriangles++;
                        parent.AddObject(triangle);
                        break;

                    case 4: //Face is a quad
                        for (int j = 1; j < 5; j++)
                        {
                            indexs[j] = Convert.ToInt32(tokens[j]);
                        }
                        //Create new triangle
                        if (smooth)
                            triangle = new SmoothMeshTriangle(parent);
                        else
                            triangle = new FlatMeshTriangle(parent);
                        triangle.SetVertexIndices(indexs[1], indexs[2], indexs[3]);
                        //Update list of faces for given vertexs using current index
                        parent.vertexFaces[indexs[1]].Add(parent.countTriangles);
                        parent.vertexFaces[indexs[2]].Add(parent.countTriangles);
                        parent.vertexFaces[indexs[3]].Add(parent.countTriangles);
                        parent.countTriangles++;
                        parent.AddObject(triangle);
                        //Create new triangle
                        if (smooth)
                            triangle = new SmoothMeshTriangle(parent);
                        else
                            triangle = new FlatMeshTriangle(parent);
                        triangle.SetVertexIndices(indexs[3], indexs[4], indexs[1]);
                        //Update list of faces for given vertexs using current index
                        parent.vertexFaces[indexs[3]].Add(parent.countTriangles);
                        parent.vertexFaces[indexs[4]].Add(parent.countTriangles);
                        parent.vertexFaces[indexs[1]].Add(parent.countTriangles);
                        parent.countTriangles++;
                        parent.AddObject(triangle);
                        break;

                    default: //What the f**k kind of input are you feeding this poor parser?
                        throw new System.FormatException("Invalid token at line " + (i + 1) +
                            ": " + numVerts + "is not a valid number of vertices for a face.");
                }
            }
            CalculateNormals(parent);
        }