Example #1
0
        public OBJLoader(string path, Matrix Transformation)
        {
            this.Transformation = Transformation;
            MTLLoader currentMaterial = new MTLLoader();
            structNewMTL currentMaterialSet = currentMaterial.getMaterialSet("");
            byte[] FileOBJ = ResManager.ListBinaries[ResManager.getBinary(path)].Data;
            int i = 0;
            float x = 1, y = 1, z = 1;
            string type, chaine_Lu;
            ToolBox ToolBox = new ToolBox();
            Debug debug = new Debug();
            List<Vector2> ListeCoordTextures = new List<Vector2>();
            List<Vector4> ListeCoordSommets = new List<Vector4>();
            List<Vector4> ListeNormales = new List<Vector4>();
            List<structVertex> ListeVertex = new List<structVertex>();
            while (i < FileOBJ.Length)
            {
                type = ToolBox.getstring(ref i, ref FileOBJ);
                #region Vertex (OK)
                if (type == "v")
                {
                    i++; // Espace
                    x = ToolBox.getfloat(ref i, ref FileOBJ);
                    i++; // Espace
                    y = ToolBox.getfloat(ref i, ref FileOBJ);
                    i++; // Espace
                    z = ToolBox.getfloat(ref i, ref FileOBJ);

                    ListeCoordSommets.Add(new Vector4(x, y, z, 1));
                    debug.WriteNicely("#", ConsoleColor.Gray, "Nouveau sommet " + x + " " + y + " " + z, 4);
                }
                #endregion
                #region Texture coordinate (OK)
                else if (type == "vt")
                {
                    i++; // Espace
                    x = ToolBox.getfloat(ref i, ref FileOBJ);
                    i++; // Espace
                    y = ToolBox.getfloat(ref i, ref FileOBJ);

                    ListeCoordTextures.Add(new Vector2(x, y));
                    debug.WriteNicely("#", ConsoleColor.Gray, "Nouvelle coordonnée de texture " + x + " " + y + " ", 4);
                }
                #endregion
                #region Normale (OK)
                else if (type == "vn")
                {
                    i++; // Espace
                    x = ToolBox.getfloat(ref i, ref FileOBJ);
                    i++; // Espace
                    y = ToolBox.getfloat(ref i, ref FileOBJ);
                    i++; // Espace
                    z = ToolBox.getfloat(ref i, ref FileOBJ);

                    ListeNormales.Add(new Vector4(x, y, z, 1));
                    debug.WriteNicely("#", ConsoleColor.Gray, "Nouvelle normale " + x + " " + y + " " + z, 4);
                }
                #endregion
                #region Face (OK)
                else if (type == "f")
                {
                    const int nbVerticesPerFace = 3;
                    structVertex[] Vertices3 = new structVertex[3];
                    for (int j = 0; j < nbVerticesPerFace; j++)
                    {
                        i++; // Espace
                        x = ToolBox.getfloat(ref i, ref FileOBJ); // Coord x
                        i++; // slash
                        y = ToolBox.getfloat(ref i, ref FileOBJ); // Coord y
                        i++; // slash
                        z = ToolBox.getfloat(ref i, ref FileOBJ); // Coord z
                        Vertices3[j] = new structVertex(
                            ListeCoordSommets[Convert.ToInt32(x) - 1],
                            ListeCoordTextures[Convert.ToInt32(y) - 1],
                            ListeNormales[Convert.ToInt32(z) - 1],
                            currentMaterialSet.Kd,
                            new Vector4()
                        );
                    }
                    Vector4 Tangent = ToolBox.CalcTangentVector(
                        new Vector3[] { ToolBox.Vec4to3(Vertices3[0].CoordVertex), ToolBox.Vec4to3(Vertices3[1].CoordVertex), ToolBox.Vec4to3(Vertices3[2].CoordVertex) },
                        new Vector2[] { Vertices3[0].CoordTex, Vertices3[1].CoordTex, Vertices3[2].CoordTex },
                        ToolBox.Vec4to3(Vertices3[0].Normale));
                    for (int j = 0; j < nbVerticesPerFace; j++)
                    {
                        Vertices3[j].Tangent = Tangent;
                        ListeVertex.Add(Vertices3[j]);
                    }
                    debug.WriteNicely("#", ConsoleColor.DarkBlue, "Nouvelle face", 3);
                }
                #endregion
                #region Object (OK)
                else if (type == "o")
                {
                    i++;
                    chaine_Lu = ToolBox.getstring(ref i, ref FileOBJ);
                    debug.WriteNicely("#", ConsoleColor.Green, "Nouvel objet : " + chaine_Lu, 3);
                    if (ListeVertex.Count != 0)
                    {
                        ListSubObject.Add(new SubObject(Matrix.Identity, currentMaterialSet, ListeVertex.ToArray()));
                        ListeVertex.Clear();
                    }
                }
                #endregion
                #region Group (OK -- Non implémenté)
                else if (type == "g")
                {
                    i++;
                    chaine_Lu = ToolBox.getstring(ref i, ref FileOBJ);
                    debug.WriteNicely("#", ConsoleColor.Green, "Nouveau groupe : " + chaine_Lu, 3);
                }
                #endregion
                #region MTLLib (OK)
                else if (type == "mtllib")
                {
                    i++;
                    chaine_Lu = ToolBox.getstring(ref i, ref FileOBJ);
                    debug.WriteNicely("#", ConsoleColor.Green, "Nouveau fichier .MTL : " + chaine_Lu, 2);
                    currentMaterial = new MTLLoader(chaine_Lu);
                    currentMaterialSet = currentMaterial.getMaterialSet("");
                }
                #endregion
                #region UseMTL (OK)
                else if (type == "usemtl")
                {
                    if (ListeVertex.Count != 0)
                    {
                        ListSubObject.Add(new SubObject(Matrix.Identity, currentMaterialSet, ListeVertex.ToArray()));
                        ListeVertex.Clear();
                    }
                    i++;
                    chaine_Lu = ToolBox.getstring(ref i, ref FileOBJ);
                    debug.WriteNicely("#", ConsoleColor.Green, "Nouveau Set : " + chaine_Lu, 2);
                    currentMaterialSet = currentMaterial.getMaterialSet(chaine_Lu);
                }
                #endregion
                #region Lissage (OK -- Non implémenté)
                else if (type == "s")
                {
                }
                #endregion
                #region New line (OK)
                else if (type == "") { }
                #endregion
                #region Commentaire (OK)
                else if (type[0] == '#')
                {
                    debug.WriteNicely("#", ConsoleColor.Green, "Commentaire", 3);
                }
                #endregion
                #region Unknown (OK)
                else
                {
                    debug.WriteNicely("!", ConsoleColor.Red, "Type inconnu", 3);
                }
                #endregion
                ToolBox.gotonextline(ref i, ref FileOBJ);
            }
            if (ListeVertex.Count != 0)
            {
                ListSubObject.Add(new SubObject(Matrix.Identity, currentMaterialSet, ListeVertex.ToArray()));
                ListeVertex.Clear();
            }
        }
Example #2
0
 public SubObject(Matrix SubTransformation, structNewMTL materialSet, structVertex[] Vertices)
 {
     this.materialSet = materialSet;
     this.SubTransformation = SubTransformation;
     this.sera_affiche = true;
     this.Vertices = Vertices;
 }