Advertisement
edward4324

Untitled

May 31st, 2022
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. void parseObjFile(const QString& fileName,
  2.     QStringList& comments,
  3.     QVector<QOpenGLTriangle3D>& triangles)
  4. {
  5.     comments.clear();
  6.     triangles.clear();
  7.  
  8.     QFile file(fileName);
  9.     if (file.exists())
  10.     {
  11.         if (file.open(QFile::ReadOnly | QFile::Text))
  12.         {
  13.             QVector<QVector3D> v, vn;
  14.             QVector<QVector2D> vt;
  15.  
  16.             while (!file.atEnd())
  17.             {
  18.                 QString line = file.readLine().trimmed();
  19.                 QStringList lineParts = line.split(QRegularExpression("\\s+"));
  20.                 if (lineParts.count() > 0)
  21.                 {
  22.  
  23.                     // if it's a comment
  24.                     if (lineParts.at(0).compare("#", Qt::CaseInsensitive) == 0)
  25.                     {
  26.                         comments.append(line.remove(0, 1).trimmed());
  27.                     }
  28.  
  29.                     // if it's a vertex position (v)
  30.                     else if (lineParts.at(0).compare("v", Qt::CaseInsensitive) == 0)
  31.                     {
  32.                         v.append(QVector3D(lineParts.at(1).toFloat(),
  33.                             lineParts.at(2).toFloat(),
  34.                             lineParts.at(3).toFloat()));
  35.                     }
  36.  
  37.                     // if it's a normal (vn)
  38.                     else if (lineParts.at(0).compare("vn", Qt::CaseInsensitive) == 0)
  39.                     {
  40.                         vn.append(QVector3D(lineParts.at(1).toFloat(),
  41.                             lineParts.at(2).toFloat(),
  42.                             lineParts.at(3).toFloat()));
  43.                     }
  44.  
  45.                     // if it's a texture (vt)
  46.                     else if (lineParts.at(0).compare("vt", Qt::CaseInsensitive) == 0)
  47.                     {
  48.                         vt.append(QVector2D(lineParts.at(1).toFloat(),
  49.                             lineParts.at(2).toFloat()));
  50.                     }
  51.  
  52.                     // if it's face data (f)
  53.                     // there's an assumption here that faces are all triangles
  54.                     else if (lineParts.at(0).compare("f", Qt::CaseInsensitive) == 0)
  55.                     {
  56.                         QOpenGLTriangle3D triangle;
  57.  
  58.                         // get points from v array
  59.                         triangle.p1 = v.at(lineParts.at(1).split("/").at(0).toInt() - 1);
  60.                         triangle.p2 = v.at(lineParts.at(2).split("/").at(0).toInt() - 1);
  61.                         triangle.p3 = v.at(lineParts.at(3).split("/").at(0).toInt() - 1);
  62.  
  63.                         if (vt.count() > 0) // check if really there are any UV coords
  64.                         {
  65.                             triangle.p1UV = vt.at(lineParts.at(1).split("/").at(1).toInt() - 1);
  66.                             triangle.p2UV = vt.at(lineParts.at(2).split("/").at(1).toInt() - 1);
  67.                             triangle.p3UV = vt.at(lineParts.at(3).split("/").at(1).toInt() - 1);
  68.                         }
  69.  
  70.                         // get normals from vn array
  71.                         triangle.p1Normal = vn.at(lineParts.at(1).split("/").at(2).toInt() - 1);
  72.                         triangle.p2Normal = vn.at(lineParts.at(2).split("/").at(2).toInt() - 1);
  73.                         triangle.p3Normal = vn.at(lineParts.at(3).split("/").at(2).toInt() - 1);
  74.  
  75.                         triangles.append(triangle);
  76.                     }
  77.  
  78.                 }
  79.             }
  80.  
  81.             file.close();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement