Advertisement
Guest User

Load OBJ

a guest
Jan 4th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. std::vector<std::string> Object::split(std::string str, char splitter){
  2.  
  3.     char charText[1000];
  4.     int number = 0;
  5.     int numberOfWords = 0;
  6.     char current[10][100];
  7.  
  8.     std::vector<std::string> tokens;
  9.  
  10.     strcpy(charText, str.c_str());
  11.  
  12.     int size = sizeof(charText) / sizeof(*charText);
  13.     //std::cout << size << " size\n";
  14.     for (int i = 0; i < splitter; i++){
  15.         if (charText[i] == splitter){
  16.             tokens.push_back(current[numberOfWords]);
  17.             numberOfWords++;
  18.             number = 0;
  19.         }
  20.         else{
  21.             current[numberOfWords][number] = charText[i];
  22.             number++;
  23.         }
  24.     }
  25.  
  26.     return tokens;
  27. }
  28.  
  29. Object::Object(std::string fileName, Vector3f pos, Vector3f rot, float Size, Vector3f color){
  30.     Position = pos;
  31.     Rotation = rot;
  32.     size = Size;
  33.  
  34.    
  35.  
  36.     colors = color;
  37.  
  38.  
  39.     std::string data;
  40.     std::ifstream file(fileName);
  41.     std::string line;
  42.  
  43.  
  44.     Vector3f _verts[1024];
  45.     int noVerts = 0;
  46.    
  47.     unsigned int _inds[1024];
  48.     int noIndices = 0;
  49.  
  50.     while (getline(file, line))  // same as: while (getline( myfile, line ).good())
  51.     {
  52.    
  53.         char charText[1024];
  54.         strcpy(charText, line.c_str());
  55.  
  56.  
  57.  
  58.         if (charText[0] == 'v' && charText[1] != 'n' && charText[1] != 't'){
  59.  
  60.             std::vector<std::string> splitLine = split(line, ' ');
  61.             _verts[noVerts] = Vector3f(atoi(splitLine[1].c_str()), atoi(splitLine[2].c_str()), atoi(splitLine[3].c_str()));
  62.  
  63.             //std::cout << "Vertices Number:" << noVerts << " x:" << _verts[noVerts].getX() << " y:" << _verts[noVerts].getY() << " z:" << _verts[noVerts].getZ() << "\n";
  64.             noVerts++;
  65.         }
  66.  
  67.         if (charText[0] == 'f'){
  68.             std::vector<std::string> splitLine = split(line, ' ');
  69.            
  70.             for (int i = 0; i < 3; i++){
  71.                
  72.                 std::vector<std::string> ssl = split(splitLine[i+1], '/');
  73.                
  74.                 //std::cout << atoi(splitLine[i].c_str()) << "\n";
  75.  
  76.                 std::cout << i << ": " << atoi(ssl[0].c_str()) << " " << atoi(ssl[1].c_str()) << " " << atoi(ssl[2].c_str()) << "\n";
  77.  
  78.                 _inds[noIndices] = atoi(ssl[0].c_str());
  79.                 noIndices++;
  80.                 _inds[noIndices] = atoi(ssl[1].c_str());
  81.                 noIndices++;
  82.                 _inds[noIndices] = atoi(ssl[2].c_str());
  83.                 noIndices++;
  84.  
  85.                 //std::cout << "number of indices:" << noIndices<< "\n";
  86.             }
  87.        
  88.         }
  89.  
  90.    
  91.     }
  92.  
  93.     verts = _verts;
  94.     inds = _inds;
  95.  
  96.     SizeV = sizeof(verts);
  97.     SizeI = sizeof(inds);
  98.  
  99.    
  100.  
  101.     noPoints = SizeI / sizeof(*inds);
  102.     length = SizeV / sizeof(*verts);
  103.  
  104.  
  105.     Matrix4f posM, rotM, sizeM;
  106.     sizeM.InitScaleTransform(size, size, size);
  107.     rotM.InitRotateTransform(rot.getX(), rot.getY(), rot.getZ());
  108.     posM.InitTranslationTransform(pos.getX(), pos.getY(), pos.getZ());
  109.  
  110.     M = posM * rotM * sizeM;
  111.  
  112.     for (unsigned int i = 0; i < length; i++){
  113.         verts[i] = _verts[i];
  114.     }
  115.  
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement