Yawin

Simple loadObj

Apr 27th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. int RF_3D::loadObj(string file)
  2. {
  3.     float aux[4];
  4.     RF_3D_Object* tmpObj = new RF_3D_Object();
  5.  
  6.     FILE* pf = fopen(file.c_str(),"r");
  7.     if(NULL == pf)
  8.     {
  9.         RF_Engine::instance->Debug("[RF_3D::loadObj] No se ha podido abrir el fichero");
  10.         return -1;
  11.     }
  12.  
  13.     while(!feof(pf))
  14.     {
  15.         char t[1000];
  16.         fgets(t,1000,pf);
  17.  
  18.         int j=0; string tAux;
  19.         for(int i = 0; i < 1000; i++)
  20.         {
  21.             switch(t[i])
  22.             {
  23.                 case ' ':
  24.                     aux[j] = atof(tAux.c_str());
  25.                     j++;
  26.                     tAux = "";
  27.                     break;
  28.  
  29.                 case 'v': i++; break;
  30.                 case 'f': i++; break;
  31.                 default: tAux+=t[i]; break;
  32.             }
  33.         }
  34.  
  35.         aux[j] = atof(tAux.c_str());
  36.  
  37.         switch(t[0])
  38.         {
  39.             case 'v':
  40.                 tmpObj->vertex.push_back(Vector3<float>(aux[0],aux[1],aux[2]));
  41.                 break;
  42.             case 'f':
  43.                 tmpObj->faces.push_back(Faces((int)aux[0],(int)aux[1],(int)aux[2],(int)aux[3]-1));
  44.                 break;
  45.         }
  46.     }
  47.  
  48.     RF_Engine::instance->Debug("LoadOBJ");
  49.     int siz = tmpObj->vertex.size();
  50.     RF_Engine::instance->Debug(siz);
  51.     RF_Engine::instance->Debug("");
  52.  
  53.     tmpObj->finalizeCreation();
  54.     RF_3D::objectList.push_back(tmpObj);
  55.     return RF_3D::objectList.size()-1;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment