Example #1
0
 private int addEl(FaceVertex2 el, int pIndex, Dictionary <FaceVertex2, int> pDict)
 {
     if (!pDict.ContainsKey(el))
     {
         pDict.Add(el, pIndex);
         indexMap.Add(el);
         inds.Add(pIndex);
         pIndex++;
     }
     else
     {
         int value = -1;
         pDict.TryGetValue(el, out value);
         if (value == -1)
         {
             throw new ArgumentException("Something with dictionary in mapIndices method");
         }
         inds.Add(value);
     }
     return(pIndex);
 }
Example #2
0
        public override bool Equals(object obj)
        {
            FaceVertex2 fv = (FaceVertex2)obj;

            if (!Position.Equals(fv.Position))
            {
                return(false);
            }
            if (!Normal.Equals(fv.Normal))
            {
                return(false);
            }
            if (!TextName.Equals(fv.TextName))
            {
                return(false);
            }
            if (!TextureCoord.Equals(fv.TextureCoord))
            {
                return(false);
            }
            return(true);
        }
Example #3
0
        public static MyComplexObjectFactory3 LoadFromString(string obj)
        {
            Dictionary <String, int> materials = new Dictionary <string, int>();
            String curMaterial = "";

            NumberStyles style   = NumberStyles.Float;
            CultureInfo  culture = CultureInfo.CreateSpecificCulture("en-US");

            // Seperate lines from the file
            List <String> lines = new List <string>(obj.Split('\n'));

            // Lists to hold model data
            List <Vector3> verts   = new List <Vector3>();
            List <Vector3> normals = new List <Vector3>();
            List <Vector2> texs    = new List <Vector2>();
            List <Tuple <TempVertex, TempVertex, TempVertex> > faces = new List <Tuple <TempVertex, TempVertex, TempVertex> >();

            // Base values
            verts.Add(new Vector3());
            texs.Add(new Vector2());
            normals.Add(new Vector3());


            // Read file line by line
            foreach (String line in lines)
            {
                if (line.StartsWith("v ")) // Vertex definition
                {
                    // Cut off beginning of line
                    String temp = line.Substring(2);

                    Vector3 vec = new Vector3();

                    if (temp.Trim().Count((char c) => c == ' ') == 2) // Check if there's enough elements for a vertex
                    {
                        String[] vertparts = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                        // Attempt to parse each part of the vertice
                        bool success = float.TryParse(vertparts[0], style, culture, out vec.X);
                        success |= float.TryParse(vertparts[1], style, culture, out vec.Y);
                        success |= float.TryParse(vertparts[2], style, culture, out vec.Z);

                        // If any of the parses failed, report the error
                        if (!success)
                        {
                            Console.WriteLine("Error parsing vertex: {0}", line);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error parsing vertex: {0}", line);
                    }

                    verts.Add(vec);
                }
                else if (line.StartsWith("vt ")) // Texture coordinate
                {
                    // Cut off beginning of line
                    String temp = line.Substring(2);

                    Vector2 vec = new Vector2();

                    if (temp.Trim().Count((char c) => c == ' ') > 0) // Check if there's enough elements for a vertex
                    {
                        String[] texcoordparts = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                        // Attempt to parse each part of the vertice
                        bool success = float.TryParse(texcoordparts[0], style, culture, out vec.X);
                        success |= float.TryParse(texcoordparts[1], style, culture, out vec.Y);

                        // If any of the parses failed, report the error
                        if (!success)
                        {
                            Console.WriteLine("Error parsing texture coordinate: {0}", line);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error parsing texture coordinate: {0}", line);
                    }

                    texs.Add(vec);
                }
                else if (line.StartsWith("vn ")) // Normal vector
                {
                    // Cut off beginning of line
                    String temp = line.Substring(2);

                    Vector3 vec = new Vector3();

                    if (temp.Trim().Count((char c) => c == ' ') == 2) // Check if there's enough elements for a normal
                    {
                        String[] vertparts = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                        // Attempt to parse each part of the vertice
                        bool success = float.TryParse(vertparts[0], style, culture, out vec.X);
                        success |= float.TryParse(vertparts[1], style, culture, out vec.Y);
                        success |= float.TryParse(vertparts[2], style, culture, out vec.Z);

                        // If any of the parses failed, report the error
                        if (!success)
                        {
                            Console.WriteLine("Error parsing normal: {0}", line);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error parsing normal: {0}", line);
                    }

                    normals.Add(vec);
                }
                else if (line.StartsWith("f ")) // Face definition
                {
                    // Cut off beginning of line
                    String temp = line.Substring(2);

                    Tuple <TempVertex, TempVertex, TempVertex> face = new Tuple <TempVertex, TempVertex, TempVertex>(new TempVertex(), new TempVertex(), new TempVertex());

                    if (temp.Trim().Count((char c) => c == ' ') >= 2) // Check if there's enough elements for a face
                    {
                        String[] faceparts = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        bool     success   = true;
                        if (temp.Trim().Count((char c) => c == ' ') == 3)
                        {
                            int v1, v2, v3;
                            int v11, v22, v33;
                            int t1, t2, t3;
                            int t11, t22, t33;
                            int n1, n2, n3;
                            int n11, n22, n33;

                            // Attempt to parse each part of the face
                            success  = int.TryParse(faceparts[0].Split('/')[0], out v1);
                            success |= int.TryParse(faceparts[1].Split('/')[0], out v2);
                            success |= int.TryParse(faceparts[2].Split('/')[0], out v3);
                            success |= int.TryParse(faceparts[0].Split('/')[0], out v11);
                            success |= int.TryParse(faceparts[2].Split('/')[0], out v22);
                            success |= int.TryParse(faceparts[3].Split('/')[0], out v33);
                            if (faceparts[0].Count((char c) => c == '/') >= 2)
                            {
                                success |= int.TryParse(faceparts[0].Split('/')[1], out t1);
                                success |= int.TryParse(faceparts[1].Split('/')[1], out t2);
                                success |= int.TryParse(faceparts[2].Split('/')[1], out t3);
                                success |= int.TryParse(faceparts[0].Split('/')[1], out t11);
                                success |= int.TryParse(faceparts[2].Split('/')[1], out t22);
                                success |= int.TryParse(faceparts[3].Split('/')[1], out t33);
                                success |= int.TryParse(faceparts[0].Split('/')[2], out n1);
                                success |= int.TryParse(faceparts[1].Split('/')[2], out n2);
                                success |= int.TryParse(faceparts[2].Split('/')[2], out n3);
                                success |= int.TryParse(faceparts[0].Split('/')[2], out n11);
                                success |= int.TryParse(faceparts[2].Split('/')[2], out n22);
                                success |= int.TryParse(faceparts[3].Split('/')[2], out n33);
                            }
                            else
                            {
                                t1 = t2 = t3 = t11 = t22 = t33 = n1 = n2 = n3 = n11 = n22 = n33 = 0;
                            }
                            // If any of the parses failed, report the error
                            if (!success)
                            {
                                Console.WriteLine("Error parsing face: {0}", line);
                            }
                            else
                            {
                                TempVertex tv1  = new TempVertex(v1, n1, t1, curMaterial);
                                TempVertex tv2  = new TempVertex(v2, n2, t2, curMaterial);
                                TempVertex tv3  = new TempVertex(v3, n3, t3, curMaterial);
                                TempVertex tv11 = new TempVertex(v11, n11, t11, curMaterial);
                                TempVertex tv22 = new TempVertex(v22, n22, t22, curMaterial);
                                TempVertex tv33 = new TempVertex(v33, n33, t33, curMaterial);
                                face = new Tuple <TempVertex, TempVertex, TempVertex>(tv1, tv2, tv3);
                                faces.Add(face);
                                face = new Tuple <TempVertex, TempVertex, TempVertex>(tv11, tv22, tv33);
                                faces.Add(face);
                            }
                        }
                        else
                        {
                            int v1, v2, v3;
                            int t1, t2, t3;
                            int n1, n2, n3;

                            // Attempt to parse each part of the face
                            success  = int.TryParse(faceparts[0].Split('/')[0], out v1);
                            success |= int.TryParse(faceparts[1].Split('/')[0], out v2);
                            success |= int.TryParse(faceparts[2].Split('/')[0], out v3);

                            if (faceparts[0].Count((char c) => c == '/') >= 2)
                            {
                                success |= int.TryParse(faceparts[0].Split('/')[1], out t1);
                                success |= int.TryParse(faceparts[1].Split('/')[1], out t2);
                                success |= int.TryParse(faceparts[2].Split('/')[1], out t3);
                                success |= int.TryParse(faceparts[0].Split('/')[2], out n1);
                                success |= int.TryParse(faceparts[1].Split('/')[2], out n2);
                                success |= int.TryParse(faceparts[2].Split('/')[2], out n3);
                            }
                            else
                            {
                                if (texs.Count > v1 && texs.Count > v2 && texs.Count > v3)
                                {
                                    t1 = v1;
                                    t2 = v2;
                                    t3 = v3;
                                }
                                else
                                {
                                    t1 = 0;
                                    t2 = 0;
                                    t3 = 0;
                                }


                                if (normals.Count > v1 && normals.Count > v2 && normals.Count > v3)
                                {
                                    n1 = v1;
                                    n2 = v2;
                                    n3 = v3;
                                }
                                else
                                {
                                    n1 = 0;
                                    n2 = 0;
                                    n3 = 0;
                                }
                            }
                            // If any of the parses failed, report the error
                            if (!success)
                            {
                                Console.WriteLine("Error parsing face: {0}", line);
                            }
                            else
                            {
                                TempVertex tv1 = new TempVertex(v1, n1, t1, curMaterial);
                                TempVertex tv2 = new TempVertex(v2, n2, t2, curMaterial);
                                TempVertex tv3 = new TempVertex(v3, n3, t3, curMaterial);
                                face = new Tuple <TempVertex, TempVertex, TempVertex>(tv1, tv2, tv3);
                                faces.Add(face);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error parsing face: {0}", line);
                    }
                }
                else if (line.StartsWith("usemtl "))
                {
                    String temp = line.Substring(7);
                    curMaterial = temp;
                }
            }

            // Create the ObjVolume
            MyComplexObjectFactory3 vol = new MyComplexObjectFactory3();

            foreach (var face in faces)
            {
                FaceVertex2 v1 = new FaceVertex2(verts[face.Item1.Vertex], normals[face.Item1.Normal], texs[face.Item1.Texcoord], face.Item1.matName);
                FaceVertex2 v2 = new FaceVertex2(verts[face.Item2.Vertex], normals[face.Item2.Normal], texs[face.Item2.Texcoord], face.Item2.matName);
                FaceVertex2 v3 = new FaceVertex2(verts[face.Item3.Vertex], normals[face.Item3.Normal], texs[face.Item3.Texcoord], face.Item3.matName);

                vol.faces.Add(new Tuple <FaceVertex2, FaceVertex2, FaceVertex2>(v1, v2, v3));
            }
            vol.mapIndices(vol.faces);
            return(vol);
        }