Exemplo n.º 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();
            }
        }
Exemplo n.º 2
0
        public MTLLoader(string path)
        {
            // Initialisation
            byte[] fileMTL = ResManager.ListBinaries[ResManager.getBinary(path)].Data;
            string type;
            float x, y, z;
            string chaine_Lu;
            int i = 0;
            List<structNewMTL> ListMaterialSet = new List<structNewMTL>();
            structNewMTL materialSet = new structNewMTL("", "", "", Color.Black, Color.White, Color.White, 1);
            ToolBox ToolBox = new ToolBox();
            Debug debug = new Debug();

            // Boucle du parser
            while (i < fileMTL.Length)
            {
                type = ToolBox.getstring(ref i, ref fileMTL);
                if (type == "newmtl")
                {
                    i++; // Espace
                    ListMaterialSet.Add(materialSet);
                    materialSet = new structNewMTL("", "", "", Color.Black, Color.White, Color.White, 1);
                    chaine_Lu = ToolBox.getstring(ref i, ref fileMTL);
                    materialSet.name = chaine_Lu;

                    debug.WriteNicely("#", ConsoleColor.DarkGreen, "nouveau set " + chaine_Lu, 3);
                }
                else if (type == "Ka")
                {
                    i++; // Espace
                    x = ToolBox.getfloat(ref i, ref fileMTL);
                    i++; // Espace
                    y = ToolBox.getfloat(ref i, ref fileMTL);
                    i++; // Espace
                    z = ToolBox.getfloat(ref i, ref fileMTL);

                    debug.WriteNicely("#", ConsoleColor.DarkGreen, "nouveau Ka " + x.ToString() + " " + y.ToString() + " " + z.ToString(), 3);
                    materialSet.Ka = new Color(x, y, z, 1);
                }
                else if (type == "Kd")
                {
                    i++; // Espace
                    x = ToolBox.getfloat(ref i, ref fileMTL);
                    i++; // Espace
                    y = ToolBox.getfloat(ref i, ref fileMTL);
                    i++; // Espace
                    z = ToolBox.getfloat(ref i, ref fileMTL);
                    materialSet.Kd = new Color(x, y, z, 1);

                    debug.WriteNicely("#", ConsoleColor.DarkGreen, "nouveau Kd " + x.ToString() + " " + y.ToString() + " " + z.ToString(), 3);
                }
                else if (type == "Ks")
                {
                    i++; // Espace
                    x = ToolBox.getfloat(ref i, ref fileMTL);
                    i++; // Espace
                    y = ToolBox.getfloat(ref i, ref fileMTL);
                    i++; // Espace
                    z = ToolBox.getfloat(ref i, ref fileMTL);
                    materialSet.Ks = new Color(x, y, z, 1);

                    debug.WriteNicely("#", ConsoleColor.DarkGreen, "nouveau Ks " + x.ToString() + " " + y.ToString() + " " + z.ToString(), 3);
                }
                else if (type == "map_Kd")
                {
                    i++; // Espace
                    chaine_Lu = ToolBox.getstring(ref i, ref fileMTL);
                    materialSet.map_Kd = chaine_Lu;

                    debug.WriteNicely("#", ConsoleColor.DarkGreen, "nouveau map_Kd " + chaine_Lu, 3);
                }
                else if (type == "map_Ns")
                {
                    i++; // Espace
                    chaine_Lu = ToolBox.getstring(ref i, ref fileMTL);
                    materialSet.map_Ns = chaine_Lu;

                    debug.WriteNicely("#", ConsoleColor.DarkGreen, "nouveau map_Ns " + chaine_Lu, 3);
                }
                else if (type == "") { }
                else if (type[0] == '#')
                {
                    debug.WriteNicely("#", ConsoleColor.Green, "Commentaire", 3);
                }
                else
                {
                    debug.WriteNicely("#", ConsoleColor.Red, "Type inconnu", 2);
                }
                ToolBox.gotonextline(ref i, ref fileMTL);
            }
            ListMaterialSet.Add(materialSet);
            ArrayMaterialSet = ListMaterialSet.ToArray();
        }