#include "LoaderOBJ.h" string ObjLoader::file = ""; string ObjLoader::fileMTL = ""; string ObjLoader::name = ""; bool ObjLoader::tit_acces = true; obj_info ObjLoader::info; ObjLoader::ObjLoader(string fichier) { clock_t debut, fin; //initialise deux variable pour le temps debut = clock(); //Met le temps dans une variable file = fichier.c_str(); //Initialisation du Struct info.v = vertex(); info.vn = normal(); info.vt = texture(); info.f = face(); Vx = new float[info.v]; Vy = new float[info.v]; Vz = new float[info.v]; VNx = new float[info.vn]; VNy = new float[info.vn]; VNz = new float[info.vn]; VTx = new float[info.vt]; VTy = new float[info.vt]; VTz = new float[info.vt]; try { points = new Points_obj(info.f); normals = new Points_obj(info.f); textures = new Points_obj(info.f); /* Ici "PX, PY, PZ" --> s'applique au groupe de points P Ici "SX, SY, SZ" --> s'applique au groupe de points S Ici "TX, TY, TZ" --> s'applique au groupe de points T ATTENTION : POUR LES COOR. DE TEXTURE ON N'UTILISERA PAS 'PZ, SZ, TZ' CAR ON N'AS QUE 2 PTS. */ } catch ( const std::exception & Exp ) { cerr<<"Erreur : "<> first; if(first == "v") { fichier >> Vx[nbr_v] >> Vy[nbr_v] >> Vz[nbr_v]; nbr_v++; } if(first == "vn") { fichier >> VNx[nbr_vn] >> VNy[nbr_vn] >> VNz[nbr_vn]; nbr_vn++; } if(first == "vt") { fichier >> VTx[nbr_vt] >> VTy[nbr_vt]; nbr_vt++; } JUMP_LINE ch++; charge::chargement(ceil(((ch*100)/(info.v+info.vn))), (name+"[vertex]"), true); } BEGIN_FILE int i_struct = 0; while(fichier.eof() != true) { Refx = 0; Refy = 0; Refz = 0; Norx = 0; Nory = 0; Norz = 0; Texx = 0; Texy = 0; Texz = 0; iboucle = 0; boucle = true; fichier >> first; if(first == "f") { //system("PAUSE"); fichier >> Argx >> Argy >> Argz; //cout<<"Args : "<value(i_struct, PX, VTx[Texx-1]); textures->value(i_struct, PY, VTy[Texx-1]); normals->value(i_struct, PX, VNx[Norx-1]); normals->value(i_struct, PY, VNy[Norx-1]); normals->value(i_struct, PZ, VNz[Norx-1]); points->value(i_struct, PX, Vx[Refx-1]); points->value(i_struct, PY, Vy[Refx-1]); points->value(i_struct, PZ, Vz[Refx-1]); //glNormal3f(VNx[Norx-1], VNy[Norx-1], VNz[Norx-1]); //glVertex3f(Vx[Refx-1], Vy[Refx-1], Vz[Refx-1]); //========= Y AND NORMAL========= //Vertex boucle = true; iboucle = 0; while(boucle) { if(Argy[iboucle] == '/') { boucle = false; } else { Refy = (Refy*10)+toint(Argy[iboucle]); } iboucle++; } //Texture boucle = true; while(boucle) { if(Argy[iboucle] == '/') { boucle = false; } else { Texy = (Texy*10)+toint(Argy[iboucle]); } iboucle++; } //Normale boucle = true; while(boucle) { if(Argy[iboucle] == '\0') { boucle = false; } else { Nory = (Nory*10)+toint(Argy[iboucle]); } iboucle++; } //Synthèse Y : textures->value(i_struct, SX, VTx[Texy-1]); textures->value(i_struct, SY, VTy[Texy-1]); normals->value(i_struct, SX, VNx[Nory-1]); normals->value(i_struct, SY, VNy[Nory-1]); normals->value(i_struct, SZ, VNz[Nory-1]); points->value(i_struct, SX, Vx[Refy-1]); points->value(i_struct, SY, Vy[Refy-1]); points->value(i_struct, SZ, Vz[Refy-1]); //glNormal3f(VNx[Nory-1], VNy[Nory-1], VNz[Nory-1]); //glVertex3f(Vx[Refy-1], Vy[Refy-1], Vz[Refy-1]); //========= Z AND NORMAL========= //vertex boucle = true; iboucle = 0; while(boucle) { if(Argz[iboucle] == '/') { boucle = false; } else { Refz = (Refz*10)+toint(Argz[iboucle]); } iboucle++; } //Texture boucle = true; while(boucle) { if(Argz[iboucle] == '/') { boucle = false; } else { Texz = (Texz*10)+toint(Argz[iboucle]); } iboucle++; } //Normale boucle = true; while(boucle) { if(Argz[iboucle] == '\0') { boucle = false; } else { Norz = (Norz*10)+toint(Argz[iboucle]); } iboucle++; } //Synthèse Z : textures->value(i_struct, TX, VTx[Texz-1]); textures->value(i_struct, TY, VTy[Texz-1]); normals->value(i_struct, TX, VNx[Norz-1]); normals->value(i_struct, TY, VNy[Norz-1]); normals->value(i_struct, TZ, VNz[Norz-1]); points->value(i_struct, TX, Vx[Refz-1]); points->value(i_struct, TY, Vy[Refz-1]); points->value(i_struct, TZ, Vz[Refz-1]); //glNormal3f(VNx[Norz-1], VNy[Norz-1], VNz[Norz-1]); //glVertex3f(Vx[Refz-1], Vy[Refz-1], Vz[Refz-1]); //===========END VERTEX AND NORMAL========= i_struct++; charge::chargement(ceil(((i_struct*100)/info.f)), (name+"[Faces]"), true); } JUMP_LINE } } } } fichier.close(); } int ObjLoader::toint(char str) { switch(str) { case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default : case '0': return 0; } } int ObjLoader::vertex() { string lettre; int vertex = 0; //Ouverture du fichier pour obtenir le nombre de ligne Vertex ifstream fichier(file.c_str(), ios::in); int len = (file.length()-1); if(fichier == NULL) //Si le fichier ne peut être ouvert et/ou lu. { cout<<"+=========================================+"<> lettre; if(lettre == "v") { vertex++; } JUMP_LINE } fichier.close(); return vertex; } } } int ObjLoader::normal() { string lettre; int normal = 0; //Ouverture du fichier pour obtenir le nombre de normal ifstream fichier(file.c_str(), ios::in); int len = (file.length()-1); if(fichier == NULL) //Si le fichier ne peut être ouvert et/ou lu. { cout<<"+=========================================+"<> lettre; if(lettre == "vn") { normal++; } JUMP_LINE } fichier.close(); return normal; } } } int ObjLoader::texture() { string lettre; int texture = 0; //Ouverture du fichier pour obtenir le nombre de normal ifstream fichier(file.c_str(), ios::in); int len = (file.length()-1); if(fichier == NULL) //Si le fichier ne peut être ouvert et/ou lu. { cout<<"+=========================================+"<> lettre; if(lettre == "vt") { texture++; } JUMP_LINE } fichier.close(); return texture; } } } int ObjLoader::face() { string lettre; int face = 0; //Ouverture du fichier pour obtenir le nombre de normal ifstream fichier(file.c_str(), ios::in); int len = (file.length()-1); if(fichier == NULL) //Si le fichier ne peut être ouvert et/ou lu. { cout<<"+=========================================+"<> lettre; if(lettre == "f") { face++; } JUMP_LINE } fichier.close(); return face; } } } void ObjLoader::draw() { for(int d = 0; d<=info.f; d++) { glBegin(GL_TRIANGLES); glNormal3f(normals->getValue(d, PX), normals->getValue(d, PY), normals->getValue(d, PZ)); glTexCoord2f(textures->getValue(d, PX), textures->getValue(d, PY)); glVertex3f(points->getValue(d, PX), points->getValue(d, PY), points->getValue(d, PZ)); glNormal3f(normals->getValue(d, SX), normals->getValue(d, SY), normals->getValue(d, SZ)); glTexCoord2f(textures->getValue(d, SX), textures->getValue(d, SY)); glVertex3f(points->getValue(d, SX), points->getValue(d, SY), points->getValue(d, SZ)); glNormal3f(normals->getValue(d, TX), normals->getValue(d, TY), normals->getValue(d, TZ)); glTexCoord2f(textures->getValue(d, TX), textures->getValue(d, TY)); glVertex3f(points->getValue(d, TX), points->getValue(d, TY), points->getValue(d, TZ)); glEnd(); //charge::chargement(ceil((d*100)/info.f), name, true); } } void ObjLoader::mtl() { //Récupération du nom du fichier MTL par celui de l'OBJ fileMTL = file.substr(0, (file.length() - 3)); fileMTL += "mtl"; ifstream fichier(fileMTL.c_str(), ios::in); string pngFile = ""; string pngBump = ""; if(fichier == NULL) //Si le fichier ne peut être ouvert et/ou lu. { cout<<"+============================================================+"<> word; if(word == "map_Kd") { fichier>>pngFile; } JUMP_LINE } BEGIN_FILE while(!fichier.eof()) { fichier >> word; if(word == "bump") { fichier>>pngBump; } JUMP_LINE } } fichier.close(); }