Exemple #1
0
        /// <summary>
        /// Reads only position and color information (No normals, texture, triangles etc. etc)
        /// </summary>
        /// <param name="fileOBJ"></param>
        /// <param name="myNewModel"></param>
        public static PointCloudVertices ReadObjFile_ToPointCloud(string fileOBJ)
        {
            PointCloudVertices myPCL = new PointCloudVertices();
            string             line  = string.Empty;
            int indexInModel         = -1;

            try
            {
                using (StreamReader streamReader = new StreamReader(fileOBJ))
                {
                    //Part p = new Part();
                    Vertex vertex = new Vertex();
                    //myNewModel.Part = new List<Part>();
                    while (!streamReader.EndOfStream)
                    {
                        line = streamReader.ReadLine().Trim();
                        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 in line : " + line);
                            //    }

                            //    myNewModel.GetTexture(strArrayRead[1], fileOBJ);
                            //    break;
                            case "v":    //Vertex
                                vertex = HelperReadVertex(strArrayRead);
                                indexInModel++;
                                vertex.IndexInModel = indexInModel;
                                myPCL.Add(vertex);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                System.Windows.Forms.MessageBox.Show("Error reading obj file - Vertices: " + line + " ; " + err.Message);
            }
            return(myPCL);
        }
Exemple #2
0
        public static Bitmap ReadTexture(string TexFile, string OBJFile)
        {
            Bitmap textureBitmap = null;

            try
            {
                string[] strArray1 = OBJFile.Split('\\');
                string   str       = "";
                TexFile = TexFile.Replace(",", ".");
                for (int index = strArray1.Length - 2; index >= 0; --index)
                {
                    str = strArray1[index] + "\\" + str;
                }
                using (StreamReader streamReader = new StreamReader(str + TexFile))
                {
                    while (!streamReader.EndOfStream)
                    {
                        string line = streamReader.ReadLine().Trim();
                        while (line.EndsWith("\\"))
                        {
                            line = line.Substring(0, line.Length - 1) + streamReader.ReadLine().Trim();
                        }
                        string[] strArray2 = GlobalVariables.TreatLanguageSpecifics(line).Split();
                        if (strArray2[0].ToLower() == "map_kd")
                        {
                            textureBitmap = new System.Drawing.Bitmap(str + strArray2[1].Replace(",", "."));
                        }
                    }
                    streamReader.Close();
                }
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine("Err :  " + err.Message);
            }
            return(textureBitmap);
        }
Exemple #3
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);
        }