Example #1
0
        private void ReadObjFile(string fileOBJ)
        {
            this.FileNameLong = fileOBJ;
            IOUtils.ExtractDirectoryAndNameFromFileName(this.FileNameLong, ref this.FileNameShort, ref this.Path);

            string line = string.Empty;

            Vector3 vector;
            Vector3 color;

            List <Vector3> vectors        = new List <Vector3>();
            List <Vector3> colors         = new List <Vector3>();
            List <Vector3> normals        = new List <Vector3>();
            List <uint>    indices        = new List <uint>();
            List <uint>    indicesNormals = new List <uint>();
            List <uint>    indicesTexture = new List <uint>();

            try
            {
                using (StreamReader streamReader = new StreamReader(fileOBJ))
                {
                    while (!streamReader.EndOfStream)
                    {
                        line = streamReader.ReadLine().Trim();

                        if (!line.StartsWith("#"))
                        {
                            while (line.EndsWith("\\"))
                            {
                                line = line.Substring(0, line.Length - 1) + streamReader.ReadLine().Trim();
                            }
                            string   str1         = GlobalVariables.TreatLanguageSpecifics(line);
                            string[] strArrayRead = str1.Split();
                            if (strArrayRead.Length >= 0)
                            {
                                switch (strArrayRead[0].ToLower())
                                {
                                case "mtllib":
                                    if (strArrayRead.Length < 2)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Error reading obj file (mtllib) in line : " + line);
                                    }

                                    this.Texture = IOUtils.ReadTexture(strArrayRead[1], fileOBJ);
                                    break;

                                case "v":    //Vertex
                                    IOUtils.HelperReadVector3dAndColor(strArrayRead, out vector, out color);
                                    vectors.Add(vector);
                                    colors.Add(color);


                                    break;

                                case "vt":    //Texture
                                    if (strArrayRead.Length < 3)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Error reading obj file (Texture) in line : " + line);
                                    }
                                    Vector3d vector1 = new Vector3d(0, 0, 0);
                                    double.TryParse(strArrayRead[1], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector1.X);
                                    double.TryParse(strArrayRead[2], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector1.Y);
                                    this.TextureCoords.Add(new double[2] {
                                        (double)vector1.X, (double)vector1.Y
                                    });
                                    break;

                                case "vn":    //Normals
                                    if (strArrayRead.Length < 4)
                                    {
                                        System.Windows.Forms.MessageBox.Show("Error reading obj file (Normals) in line : " + line);
                                    }
                                    Vector3 vector2 = new Vector3(0, 0, 0);
                                    float.TryParse(strArrayRead[1], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector2.X);
                                    float.TryParse(strArrayRead[2], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector2.Y);
                                    float.TryParse(strArrayRead[3], NumberStyles.Float | NumberStyles.AllowThousands, (IFormatProvider)null, out vector2.Z);
                                    //vector2.NormalizeNew();
                                    normals.Add(vector2);
                                    break;

                                case "f":
                                    IOUtils.ReadIndicesLine(strArrayRead, indices, indicesNormals, indicesTexture);
                                    break;

                                case "g":
                                    //if (myNewModel.Triangles.Count > 0)
                                    //{
                                    //    if (myNewModel.TextureBitmap != null)
                                    //    {
                                    //        p.ColorOverall = System.Drawing.Color.FromArgb(1, 1, 1);
                                    //    }
                                    //    else
                                    //    {
                                    //        double r = Convert.ToSingle(0.3 * Math.Cos((double)(23 * myNewModel.Parts.Count)) + 0.5);
                                    //        double g = Convert.ToSingle(0.5f * Math.Cos((double)(17 * myNewModel.Parts.Count + 1)) + 0.5);
                                    //        double b = Convert.ToSingle(0.5f * Math.Cos((double)myNewModel.Parts.Count) + 0.5);


                                    //        p.ColorOverall = System.Drawing.Color.FromArgb(Convert.ToInt32(r * byte.MaxValue), Convert.ToInt32(g * byte.MaxValue), Convert.ToInt32(b * byte.MaxValue));
                                    //    }
                                    //    //p.ColorOverall = myNewModel.TextureBitmap != null ? new Vector3d(1, 1, 1) : new Vector3d(0.3 * Math.Cos((double)(23 * myNewModel.Parts.Count)) + 0.5, 0.5f * Math.Cos((double)(17 * myNewModel.Parts.Count + 1)) + 0.5, 0.5f * Math.Cos((double)myNewModel.Parts.Count) + 0.5);
                                    //    myNewModel.Parts.Add(new Part(p));
                                    //}
                                    //if (strArrayRead.Length > 1)
                                    //    p.Name = str1.Replace(strArrayRead[1], "");
                                    //myNewModel.Triangles.Clear();
                                    break;
                                }
                            }
                        }
                    }

                    streamReader.Close();
                }
            }
            catch (Exception err)
            {
                System.Windows.Forms.MessageBox.Show("Error reading obj file (general): " + line + " ; " + err.Message);
            }
            if (indices.Count != vectors.Count)
            {
                for (uint i = Convert.ToUInt32(indices.Count); i < vectors.Count; i++)
                {
                    indices.Add(i);
                }
            }
            AssignData(vectors, colors, normals, indices, indicesNormals, indicesTexture);
        }