Advertisement
glitchdetector

obj import test for opengl

Feb 4th, 2024
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. struct ObjectFileReturnInfo
  2. {
  3.     bool bHasTextureData = false;
  4.     bool bHasNormalData = false;
  5.     bool bSuccess = false;
  6.     int nVertices = 0;
  7.     int nFaces = 0;
  8.     int nObjects = 0;
  9.     void print()
  10.     {
  11.         std::cout << "Object Properties" << std::endl;
  12.         std::cout << "Loaded: " << bSuccess << std::endl;
  13.         std::cout << "Has Texture Data: " << bHasTextureData << std::endl;
  14.         std::cout << "Has Normal Data: " << bHasNormalData << std::endl;
  15.         std::cout << "Vertices: " << nVertices << std::endl;
  16.         std::cout << "Faces: " << nFaces << std::endl;
  17.         std::cout << "Meshes: " << nObjects << std::endl;
  18.     }
  19. };
  20.  
  21. ObjectFileReturnInfo ReadObjectFile(std::string fileName, std::vector<Vertex>& vertices, std::vector<int>& indices)
  22. {
  23.     std::ifstream in;
  24.     in.open(fileName);
  25.  
  26.     ObjectFileReturnInfo output;
  27.  
  28.     //vertices.push_back(Vertex{ 0,0,0,0,0,0,0,0 });
  29.  
  30.     if (!in.is_open())
  31.     {
  32.         std::cout << "Could not open file " << fileName << std::endl;
  33.         return output;
  34.     }
  35.     std::cout << "Reading file " << fileName << std::endl;
  36.  
  37.     struct TempVertex
  38.     {
  39.         float x, y, z;
  40.         float r, g, b;
  41.     };
  42.     std::vector<TempVertex> vertex_vector;
  43.  
  44.     struct TempUV
  45.     {
  46.         float u, v;
  47.     };
  48.     std::vector<TempUV> uv_vector;
  49.  
  50.     struct TempNormal
  51.     {
  52.         float x, y, z;
  53.     };
  54.     std::vector<TempNormal> normal_vector;
  55.  
  56.     bool bHasUVData = false;
  57.  
  58.     std::string line;
  59.  
  60.     std::string prefix;
  61.     in >> prefix;
  62.     while (!in.eof())
  63.     {
  64.         if (prefix == "#") {
  65.             std::getline(in, line);
  66.             std::cout << line << std::endl;
  67.         } else if (prefix == "vn") {
  68.             // Normal data
  69.             TempNormal normal;
  70.             in
  71.                 >> normal.x
  72.                 >> normal.y
  73.                 >> normal.z;
  74.             normal_vector.push_back(normal);
  75.             output.bHasNormalData = true;
  76.         } else if (prefix == "vt") {
  77.             // Vertex Texture (UV)
  78.             TempUV uv;
  79.             in
  80.                 >> uv.u
  81.                 >> uv.v;
  82.             uv_vector.push_back(uv);
  83.             output.bHasTextureData = true;
  84.         } else if (prefix == "v") {
  85.             // Vertex
  86.             TempVertex v;
  87.             in
  88.                 >> v.x
  89.                 >> v.y
  90.                 >> v.z
  91.                 >> v.r
  92.                 >> v.g
  93.                 >> v.b;
  94.             vertex_vector.push_back(v);
  95.         } else if (prefix == "f") {
  96.             // Go over each point in the face
  97.             for (int i = 0; i < 3; i++)
  98.             {
  99.                 std::string vertex;
  100.                 in >> vertex;
  101.  
  102.                 std::string string;
  103.                 std::istringstream iss(vertex);
  104.  
  105.                 int vertexIndex;
  106.                 iss >> vertexIndex;
  107.  
  108.                 TempVertex t = vertex_vector[vertexIndex - 1];
  109.                 Vertex v{ t.x, t.y, t.z, t.r, t.g, t.b, 0.f, 0.f };
  110.  
  111.                 if (output.bHasTextureData)
  112.                 {
  113.                     std::getline(iss, string, '/');
  114.                     int uvIndex;
  115.                     iss >> uvIndex;
  116.  
  117.                     TempUV uv = uv_vector[uvIndex - 1];
  118.                     v.u = uv.u;
  119.                     v.v = 1.f - uv.v;
  120.                 }
  121.                 if (output.bHasNormalData)
  122.                 {
  123.                     // Unused by our program
  124.                 }
  125.                 vertices.push_back(v);
  126.                 indices.push_back(vertices.size() - 1);
  127.             }
  128.             output.nFaces++;
  129.         } else if (prefix == "o") {
  130.             std::getline(in, line);
  131.             std::cout << "Object " << line << std::endl;
  132.             output.nObjects++;
  133.         } else if (prefix == "s") {
  134.             int shadeSmooth;
  135.             in >> shadeSmooth;
  136.             if (shadeSmooth == 1) {
  137.                 std::cout << "Set smooth shading" << std::endl;
  138.             }
  139.             else {
  140.                 std::cout << "Set flat shading" << std::endl;
  141.             }
  142.         } else if (prefix == "usemtl") {
  143.             std::getline(in, line);
  144.             std::cout << "Using material " << line << std::endl;
  145.         } else {
  146.             std::getline(in, line);
  147.             std::cout << "Unknown line " << line << std::endl;
  148.         }
  149.         in >> prefix;
  150.     }
  151.     output.nVertices = vertex_vector.size();
  152.     output.bSuccess = true;
  153.     std::cout << "Loaded " << fileName << " with " << vertices.size() << " verts and " << (indices.size() / 3) << " tris." << std::endl;
  154.     return output;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement