Files
rez_demo/GL_STUFF/SOURCES/SCENE/InstancesLoader.cpp

86 lines
3.1 KiB
C++
Raw Normal View History

#include <regex>
#include <iostream>
#include <sstream>
#include "../../HEADERS/SCENE/InstancesLoader.h"
char lineHeader[128];
char* res;
FILE* file;
std::smatch pieces;
Instance3D* parseInstanceData(Model3D* model, string str) {
vector<float> vecComponents = vector<float>();
std::regex regex("-?\\d+.\\d+");
std::regex_search(str, pieces, regex);
float x = stof(pieces[0], NULL); //potevi evitare la regex
str = pieces.suffix().str();
std::regex_search(str, pieces, regex);
float y = stof(pieces[0], NULL); //potevi evitare la regex
str = pieces.suffix().str();
std::regex_search(str, pieces, regex);
float z = stof(pieces[0], NULL); //potevi evitare la regex
str = pieces.suffix().str();
std::regex_search(str, pieces, regex);
float rotx = stof(pieces[0], NULL); //potevi evitare la regex
str = pieces.suffix().str();
std::regex_search(str, pieces, regex);
float roty = stof(pieces[0], NULL); //potevi evitare la regex
str = pieces.suffix().str();
std::regex_search(str, pieces, regex);
float rotz = stof(pieces[0], NULL); //potevi evitare la regex
Instance3D* inst = new Instance3D(model, vec3(x, y, z), vec3(1));
inst->rotate(vec3(1, 0, 0), rotx);
inst->rotate(vec3(0, 1, 0), roty);
inst->rotate(vec3(0, 0, 1), rotz);
return inst;
}
/* TOFIX: broken */
bool loadInstances(std::string path, vector<Instance3D*>& instances, bool smoothNormals) {
bool modelLoaded = false;
std::stringstream fileName;
Model3D* currentModel = NULL;
/*on windows was last of \\ */
string root = path.substr(0, path.find_last_of("/") + 1);
file = fopen(&path[0], "r");
if (file == NULL) {
printf("Impossible to open the scene file ! Are you in the right path ?\n");
getchar();
return false;
}
while (1) {
res = fgets(lineHeader, 128, file); // read the first word of the line
// EOF = End Of File. Store pending curve and quit the loop.
if (res == NULL) {
break;
}
string str = lineHeader;
std::regex regex("\\w+\\n");
//inizia una serie di istanze(la prima o una nuova)
if (std::regex_match(str, pieces, regex)) {
std::regex regex("\\w+");
std::regex_search(str, pieces, regex);
fileName.str(std::string()); //clear
fileName << root << pieces[0].str() << ".obj";
string mdl = fileName.str();
currentModel = new Model3D(&mdl[0], smoothNormals);
modelLoaded = true;
}
//devono essere i dati di trasformazione di un istanza
else {
std::regex regex("\\s*(-?\\d+.\\d+)(( , -?\\d+.\\d+)+)\\s*(\\n)?");
//sono dati di un vertice
if (std::regex_match(str, pieces, regex) && modelLoaded) {
instances.push_back(parseInstanceData(currentModel, str));
}
else {
std::cout << "unrecognized scene line : " << str << std::endl;
}
}
}
std::cout << "trovati " << instances.size() << std::endl;
return true;
}