Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. struct Vertex
  2. {
  3.     int n;
  4.     float x, y, z;
  5. };
  6. struct Face
  7. {
  8.     int n, group, mtl, a, b, c, ab, bc, ca;
  9. };
  10. struct Vector
  11. {
  12.     float x, y, z;
  13. };
  14. struct GeomObject
  15. {
  16.     vector<Face> faces;
  17.     vector<Vertex> vertices;
  18.     char *name;
  19.     Vector *Location, *Rotation, *Scale;
  20.     int nFaces, nVerts;
  21. };
  22.  
  23. class Model
  24. {
  25. public:
  26.     Model(char *filename);
  27.     ~Model();
  28.     char *Filename;
  29.     GeomObject *Objects;
  30.     Vector *Location, *Rotation, *Scale;
  31.     int getTotalObjects();
  32.     int getTotalFaces();
  33.     int getTotalVertices();
  34. private:
  35.     int Size;
  36.     void Parse();
  37.     char *buffer;
  38.     int Count(char *string,char *substr);
  39.     void Process();
  40. };
  41.  
  42. Model::Model(char *filename)
  43. {
  44.     Filename = filename;
  45.     Process();
  46.     return;
  47. }
  48. Model::~Model()
  49. {
  50.     delete [] buffer;
  51.     buffer = NULL;
  52. }
  53. void Model::Process()
  54. {
  55.     FILE *file = fopen(Filename, "r");
  56.     if(file != NULL)
  57.     {
  58.         fseek(file, 0, SEEK_END);
  59.         Size = ftell(file);
  60.         buffer = new char[Size + 1];
  61.         for(int i = 0; i < Size; i++)
  62.             buffer[i] = 0;
  63.         fseek(file, 0, SEEK_SET);
  64.         fread(buffer, 1, Size, file);
  65.         fclose(file);
  66.         Parse();
  67.     }
  68.     return;
  69. }
  70. void Model::Parse()
  71. {
  72.     Objects = new GeomObject[getTotalObjects()];
  73.     vector<size_t> positions;   // Indices of object declarations
  74.     string data = buffer;
  75.  
  76.     // Parses where object declarations occur...
  77.     size_t pos = data.find("*GEOMOBJECT", 0);
  78.     while(pos != string::npos)
  79.     {
  80.         positions.push_back(pos);
  81.         pos = data.find("*GEOMOBJECT", pos + 1);
  82.     }
  83.     for(unsigned int i = 0; i < getTotalObjects(); i++)
  84.     {
  85.         // OBJECTS
  86.         Objects[i].name = new char[255];
  87.         sscanf(buffer + positions[i], "*GEOMOBJECT {\n\t*NODE_NAME \"%s\"", Objects[i].name);
  88.  
  89.         // VERTICES
  90.         pos = data.find("\t*MESH_VERTEX ", positions[i]);
  91.         while(pos != string::npos)
  92.         {
  93.             if(i < positions.size() - 1)
  94.                 if(pos >= positions[i + 1])
  95.                     break;
  96.             int n;
  97.             float x, y, z;
  98.             sscanf(buffer + pos, "\t*MESH_VERTEX %d\t%f\t%f\t%f\n",
  99.                 &n, &x, &y, &z);
  100.             Vertex vert = {n, x, y, z};
  101.             Objects[i].vertices.push_back(vert);
  102.             pos = data.find("\t*MESH_VERTEX ", pos + 1);
  103.         }
  104.  
  105.         // FACES
  106.         pos = data.find("\t*MESH_FACE ", positions[i]);
  107.         while(pos != string::npos)
  108.         {
  109.             if(i < positions.size() - 1)
  110.                 if(pos >= positions[i + 1])
  111.                     break;
  112.             int n, a, b, c, group, mtl;
  113.             sscanf(buffer + pos, "\t*MESH_FACE %d: A: %d B: %d C: %d %*s %*d %*s %*d %*s %*d *MESH_SMOOTHING %d *MESH_MTLID %d",
  114.                 &n, &a, &b, &c, &group, &mtl);
  115.             Face f = {n, a, b, c, group, mtl};
  116.             Objects[i].faces.push_back(f);
  117.             pos = data.find("\t*MESH_FACE ", pos + 1);
  118.         }
  119.     }
  120.     return;
  121. }
  122. int Model::getTotalObjects()
  123. {
  124.     return Count(buffer, "*GEOMOBJECT ");
  125. }
  126. int Model::getTotalFaces()
  127. {
  128.     return Count(buffer, "\t*MESH_FACE ");
  129. }
  130. int Model::getTotalVertices()
  131. {
  132.     return Count(buffer, "\t*MESH_VERTEX ");
  133. }
  134. int Model::Count(char *string,char *substr)
  135. {
  136.     int count = 0;
  137.     char *token = strstr(string,substr);
  138.     while(token != NULL)
  139.     {
  140.         count++;
  141.         token = strstr(token + 1,substr);
  142.     }
  143.     return count;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement