void File::loadTriangles(vector< vector<Triangle> > &triangles, vector<ustring> &names, uint max_triangles) { Gio::FileType type = _file->query_file_type(); if (type != Gio::FILE_TYPE_REGULAR && type != Gio::FILE_TYPE_SYMBOLIC_LINK) return; ustring name_by_file = _file->get_basename(); size_t found = name_by_file.find_last_of("."); name_by_file = (ustring)name_by_file.substr(0,found); set_locales("C"); if(_type == ASCII_STL) { // multiple shapes per file load_asciiSTL(triangles, names, max_triangles); if (names.size() == 1) // if single shape name by file names[0] = name_by_file; if (triangles.size() == 0) {// if no success, try binary mode _type = BINARY_STL; loadTriangles(triangles, names, max_triangles); return; } } else if (_type == AMF) { // multiple shapes per file load_AMF(triangles, names, max_triangles); if (names.size() == 1) // if single shape name by file names[0] = name_by_file; } else { // single shape per file triangles.resize(1); names.resize(1); names[0] = name_by_file; if (_type == BINARY_STL) { load_binarySTL(triangles[0], max_triangles); } else if (_type == VRML) { load_VRML(triangles[0], max_triangles); } else { cerr << _("Unrecognized file - ") << _file->get_parse_name() << endl; cerr << _("Known extensions: ") << "STL, WRL, AMF." << endl; } } reset_locales(); }
MeshComponent::MeshComponent(const std::vector<Triangle> &triangles, const std::string &texturePath) : Component<MeshComponent>(), m_vertexBuffer(0), m_vertexCount(0), m_colorOffset(0), m_textureOffset(0), m_loadedTriangles(false), vertexArray(0), meshTexture(0), meshSampler(0) { glGenVertexArrays(1, &vertexArray); glGenBuffers(1, &m_vertexBuffer); loadTriangles(triangles); loadTexture(texturePath); }
gwMd2::gwMd2(const char *filePath){ FILE *fp = fopen(filePath,"r"); assert(fp); header = loadHeader(fp); assert(header); skin = loadSkins(fp); assert(skin); texCoord = loadTexCoords(fp); assert(texCoord); triangle = loadTriangles(fp); assert(triangle); frame = loadFrames(fp); assert(frame); glcmds = loadGlCommands(fp); assert(glcmds); tex = new gwTexture(skin[textureId].name); assert(tex); fclose(fp); }
int main(int argc, char **argv) { // read command-line arguments for(int i=1; i<argc; i++) { if(argv[i][0] == '-') { switch(argv[i][1]) { case 'l': useDisplayLists = true; break; case 'z': if(i+1 < argc) { zoom = atof(argv[i+1]); i++; } break; default: syntax(argv[0]); } } else { datafiles.push_back(argv[i]); } } // setup GLUT / OpenGL glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowPosition(0, 0); glutInitWindowSize(1280, 1024); glutCreateWindow("TriangleRender"); // the display function will be called continuously glutDisplayFunc(display); glutIdleFunc(display); glClearColor(0.5, 0.5, 0.5, 1.); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, true); // load triangle data files (and optionally create display lists) for(std::vector<char *>::iterator it=datafiles.begin(); it!=datafiles.end(); it++) { loadTriangles(*it); } // print total number of triangles int totalTriangles = 0; for(std::vector<Triangles>::iterator it=triangles.begin(); it!=triangles.end(); it++) { totalTriangles += (*it).count / 3; } std::cout << "total number of triangles: " << totalTriangles << std::endl; // enter the main loop glutMainLoop(); return 0; }