Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Vertex
- {
- int n;
- float x, y, z;
- };
- struct Face
- {
- int n, group, mtl, a, b, c, ab, bc, ca;
- };
- struct Vector
- {
- float x, y, z;
- };
- struct GeomObject
- {
- vector<Face> faces;
- vector<Vertex> vertices;
- char *name;
- Vector *Location, *Rotation, *Scale;
- int nFaces, nVerts;
- };
- class Model
- {
- public:
- Model(char *filename);
- ~Model();
- char *Filename;
- GeomObject *Objects;
- Vector *Location, *Rotation, *Scale;
- int getTotalObjects();
- int getTotalFaces();
- int getTotalVertices();
- private:
- int Size;
- void Parse();
- char *buffer;
- int Count(char *string,char *substr);
- void Process();
- };
- Model::Model(char *filename)
- {
- Filename = filename;
- Process();
- return;
- }
- Model::~Model()
- {
- delete [] buffer;
- buffer = NULL;
- }
- void Model::Process()
- {
- FILE *file = fopen(Filename, "r");
- if(file != NULL)
- {
- fseek(file, 0, SEEK_END);
- Size = ftell(file);
- buffer = new char[Size + 1];
- for(int i = 0; i < Size; i++)
- buffer[i] = 0;
- fseek(file, 0, SEEK_SET);
- fread(buffer, 1, Size, file);
- fclose(file);
- Parse();
- }
- return;
- }
- void Model::Parse()
- {
- Objects = new GeomObject[getTotalObjects()];
- vector<size_t> positions; // Indices of object declarations
- string data = buffer;
- // Parses where object declarations occur...
- size_t pos = data.find("*GEOMOBJECT", 0);
- while(pos != string::npos)
- {
- positions.push_back(pos);
- pos = data.find("*GEOMOBJECT", pos + 1);
- }
- for(unsigned int i = 0; i < getTotalObjects(); i++)
- {
- // OBJECTS
- Objects[i].name = new char[255];
- sscanf(buffer + positions[i], "*GEOMOBJECT {\n\t*NODE_NAME \"%s\"", Objects[i].name);
- // VERTICES
- pos = data.find("\t*MESH_VERTEX ", positions[i]);
- while(pos != string::npos)
- {
- if(i < positions.size() - 1)
- if(pos >= positions[i + 1])
- break;
- int n;
- float x, y, z;
- sscanf(buffer + pos, "\t*MESH_VERTEX %d\t%f\t%f\t%f\n",
- &n, &x, &y, &z);
- Vertex vert = {n, x, y, z};
- Objects[i].vertices.push_back(vert);
- pos = data.find("\t*MESH_VERTEX ", pos + 1);
- }
- // FACES
- pos = data.find("\t*MESH_FACE ", positions[i]);
- while(pos != string::npos)
- {
- if(i < positions.size() - 1)
- if(pos >= positions[i + 1])
- break;
- int n, a, b, c, group, mtl;
- 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",
- &n, &a, &b, &c, &group, &mtl);
- Face f = {n, a, b, c, group, mtl};
- Objects[i].faces.push_back(f);
- pos = data.find("\t*MESH_FACE ", pos + 1);
- }
- }
- return;
- }
- int Model::getTotalObjects()
- {
- return Count(buffer, "*GEOMOBJECT ");
- }
- int Model::getTotalFaces()
- {
- return Count(buffer, "\t*MESH_FACE ");
- }
- int Model::getTotalVertices()
- {
- return Count(buffer, "\t*MESH_VERTEX ");
- }
- int Model::Count(char *string,char *substr)
- {
- int count = 0;
- char *token = strstr(string,substr);
- while(token != NULL)
- {
- count++;
- token = strstr(token + 1,substr);
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement