void Object3D::record(const QString &line) { if(line.at(0) == 'v' && line.at(1) == ' ') // v ... ... ... readlVertices(get_corret_line(line)); else if(line.at(0) == 'v' && line.at(1) == 't') // vt ... ... readTexturCord(get_corret_line(line)); else if(line.at(0) == 'v' && line.at(1) == 'n') // vn readNormals(get_corret_line(line)); else if(line.at(0) == 'v' && line.at(1) == 'p') // vp readSpaceVert(get_corret_line(line)); else if(line.at(0) == 'f') // f readFace(get_corret_line(line)); else if(line.at(0) == 'm') // mtl readMtl(get_corret_line(line)); else if(line.at(0) == 'u') readUseMtl(get_corret_line(line)); }
MeshData* LoadObj::readObjFile(std::string pObj) { bool first = true; fstream f(pObj.c_str()); string tmp; if(f) { while(f.eof() != true) { f >> tmp; if(tmp == "v") readVertices(f); if(tmp == "vt") readTexC(f); if(tmp == "vn") readNormal(f); if(tmp == "f") readFaces(f); if(tmp == "mtllib") readMtl(f); if(tmp == "g") readGroup(f); if(f.eof()) break; } } MeshData* data = new MeshData(); data->position = position; data->textureCoordinate = textureCoordinate; data->normal = normal; data->vertices = vertices; data->indices = indices; reset(); return data; }
int openObj (char *filename, objObj * obj) { nameIndex *mtlIndex; nameIndex *texIndex; FILE *obj_fp = NULL; FILE *mtl_fp = NULL; mtlIndex = NULL; texIndex = NULL; obj->nbVertex = 0; obj->nbNormal = 0; obj->nbTexcoord = 0; obj->nbFace = 0; obj->nbMaterial = 0; obj->vertexList = NULL; obj->normalList = NULL; obj->texcoordList = NULL; obj->faceList = NULL; obj->materialList = NULL; if (initObj (filename, obj, &obj_fp, &mtl_fp, &mtlIndex, &texIndex)) return cleanBeforeExit (1, obj, obj_fp, mtl_fp, mtlIndex, texIndex); if (readMtl (mtl_fp, *obj, mtlIndex, texIndex)) return cleanBeforeExit (1, obj, obj_fp, mtl_fp, mtlIndex, texIndex); if (readObj (obj_fp, *obj, mtlIndex)) return cleanBeforeExit (1, obj, obj_fp, mtl_fp, mtlIndex, texIndex); return cleanBeforeExit (0, obj, obj_fp, mtl_fp, mtlIndex, texIndex); }
void MainWindow::on_pushButton_released() { QFile file("rule.file"); if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)){ QTextStream out(&file); out << ui->textEdit->toPlainText(); out.flush(); file.close(); } Model new_m; material_map.clear(); readMtl("default.mtl", true); errors.clear(); parseFile("rule.file", new_m); //printf("Errors : %ld\n", errors.size()); if(errors.size() > 0){ new_m.faces.clear(); std::string all; for(size_t i = 0; i < errors.size(); i++){ all += errors[i]; all += "\n"; } QString str = QString::fromStdString(all); text->setText(str); err->show(); } BBox scope; getBBox(new_m, scope); translate(new_m, -(scope.max.x + scope.min.x)/2.0, -(scope.max.x + scope.min.x)/2.0, -(scope.max.x + scope.min.x)/2.0); model_size = std::max(std::max(scope.max.y - scope.min.y, scope.max.x - scope.min.x), scope.max.z - scope.min.z); //scale(new_m, 1.0/model_size, 1.0/model_size, 1.0/model_size); //scaleTextures(new_m); sortFacesByMaterial(new_m); m = new_m; update_model = true; }
void Reader::readObj(const char* name, Mesh* m){ ifstream in; string buffer; vector<string> tokens; vector<string> indexes; Group* g = new Group(); m->addGroup(g); Vertex v; Face* f; Texts t; string mtl; in.open(name); if(!in.is_open()){ cout<<"Arquivo nao foi aberto com sucesso: "<<name<<endl; return; } while(!in.eof()){ getline(in, buffer); if(buffer.find('\n') != -1){ cout<<buffer<<endl; } tokens = split(buffer, ' ', true); switch(buffer[0]){ case 'v': if(tokens.size() > 3){ v = Vertex( atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str())); }else{ t = Texts( atof(tokens[1].c_str()), atof(tokens[2].c_str())); } switch(buffer[1]){ case 't': m->addTexts(t); break; case 'n': m->addNorms(v); break; default: m->addVerts(v); break; } break; case 'f': f = new Face(); g->addFace(f); for(unsigned int x = 1; x < tokens.size(); ++x){ indexes = split(tokens[x], '/', false); f->addVert(atoi(indexes[0].c_str()) - 1); if(indexes.size() > 1) if(indexes[1].size() > 0) f->addText(atoi(indexes[1].c_str()) - 1); if(indexes.size() > 2) f->addNorm(atoi(indexes[2].c_str()) - 1); } break; case 'g': if(!g->getFaces().empty()){ if(tokens.size() == 1){ g = new Group(); } else { g = new Group(tokens[1]); } m->addGroup(g); } break; case 'm': mtl = tokens[1]; break; case 'u': g->setMtl(tokens[1]); break; } } in.close(); if(mtl.empty()){ for(Group* g1 : m->getGroups()){ g1->setMtl(""); } }else{ string n(name); mtl = n.substr(0, n.rfind('\\')) + '\\' + mtl; if(!readMtl(mtl.c_str(), m)){ for(Group* g1 : m->getGroups()){ g1->setMtl(""); } } } }