// Load a scene from a .dae or a .obj file: void SceneLoader::load(Scene* scene, const char* filename, ElementContainer::Type container_type) { // Determine the file type: string str_filename = string(filename); string extension = str_filename.substr(str_filename.find_last_of('.')); uint len = extension.length(); for(uint i=0 ; i < len ; i++) extension[i] = tolower(extension[i]); if(extension == ".dae") { DAELoader loader; loader.load(scene, filename, container_type); } else if(extension == ".obj") { OBJLoader loader; loader.load(scene, filename, container_type); } else logError("cannot load scene \"", filename, "\": unknown file type"); }
bool MeshManager::loadOBJFile(const string& filename) { #if USE_REEB_GRAPH try { cout << "[VTK] Reading mesh file ..." << endl; vtkSmartPointer<vtkOBJReader> vtkReader = vtkSmartPointer<vtkOBJReader>::New(); vtkReader->SetFileName(filename.c_str()); vtkReader->Update(); vtkMesh = vtkReader->GetOutput(); cout << "[VTK] Creating reeb graph ..." << endl; updateReebGraph(); } catch (exception e) { cerr << e.what() << endl; } #endif OBJLoader loader; if( loader.load(filename) ) { cout << "file " << filename << " loaded." << endl; /// build a half edge mesh here //hds_mesh->printMesh("original"); hds_mesh.reset(buildHalfEdgeMesh(loader.getFaces(), loader.getVerts())); cutted_mesh.reset(); unfolded_mesh.reset(); smoothed_mesh.reset(); /// save the half edge mesh out to a temporary file hds_mesh->save("temp.obj"); /// preprocess the mesh with smoothing const int nsmooth = 10; QScopedPointer<HDS_Mesh> tmp_mesh; vector<string> smoothed_mesh_filenames; tmp_mesh.reset(new HDS_Mesh(*hds_mesh)); hds_mesh_smoothed.push_back(QSharedPointer<HDS_Mesh>(new HDS_Mesh(*tmp_mesh))); for (int i = 0; i < nsmooth; ++i) { const int stepsize = 10; string smesh_filename = filename.substr(0, filename.length() - 4) + "_smoothed_" + std::to_string((i + 1)*stepsize) + ".obj"; smoothed_mesh_filenames.push_back(smesh_filename); if (Utils::exists(smesh_filename)) { // load the mesh directly OBJLoader tmploader; tmploader.load(smesh_filename); tmp_mesh.reset(buildHalfEdgeMesh(tmploader.getFaces(), tmploader.getVerts())); } else { for (int j = 0; j < stepsize; ++j) { MeshSmoother::smoothMesh_Laplacian(tmp_mesh.data()); } tmp_mesh->save(smesh_filename); } hds_mesh_smoothed.push_back(QSharedPointer<HDS_Mesh>(new HDS_Mesh(*tmp_mesh))); } cout << "smoothed meshes computed finished." << endl; /// initialize the sparse graph gcomp.reset(new GeodesicComputer(filename)); gcomp_smoothed.push_back(QSharedPointer<GeodesicComputer>(gcomp.data())); for (int i = 0; i < smoothed_mesh_filenames.size(); ++i) { // compute or load SVG for smoothed meshes gcomp_smoothed.push_back(QSharedPointer<GeodesicComputer>(new GeodesicComputer(smoothed_mesh_filenames[i]))); } cout << "SVGs computed." << endl; return true; } else return false; }
void Initialize(void){ // Create the program for rendering the model OBJLoader loader; bool loadfile = loader.load("bunny.obj"); if(!loadfile) { exit(EXIT_FAILURE); } vertices = loader.getVertices(); normals = loader.getNormals(); indices = loader.getVertexIndices(); // Create and compile our GLSL program from the shaders program = LoadShaders("shader.vs", "shader.fs"); // Use our shader glUseProgram(program); view = lookAt(vec3(0.0f, 0.0f, 2.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f)); projection = mat4(1.0f); // Initialize shader lighting parameters glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(2, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(vec3), &vertices[0], GL_DYNAMIC_DRAW); glVertexAttribPointer(static_cast<GLuint>(0), 3, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexAttribArray(0); // Vertex position glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec3), &normals[0], GL_STATIC_DRAW); glVertexAttribPointer(static_cast<GLuint>(1), 3, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexAttribArray(1); // Vertex normal glGenBuffers(1, &ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(int), &indices[0], GL_STATIC_DRAW); glBindVertexArray(0); vec3 light_intensity(1.0f, 1.0f, 1.0f); vec4 light_position(10.0f, 10.0f, 10.0f, 1.0f); vec3 material_ambient(0.9, 0.5, 0.3); vec3 material_diffuse(0.9, 0.5, 0.3); vec3 material_specular(0.8, 0.8, 0.8); GLfloat shininess = 100.0f; glUniform3fv(glGetUniformLocation(program, "Light.Intensity"), 1, reinterpret_cast<GLfloat*>(&light_intensity)); glUniform4fv(glGetUniformLocation(program, "Light.Position"), 1, reinterpret_cast<GLfloat*>(&light_position)); glUniform3fv(glGetUniformLocation(program, "Material.Ka"), 1, reinterpret_cast<GLfloat*>(&material_ambient)); glUniform3fv(glGetUniformLocation(program, "Material.Kd"), 1, static_cast<GLfloat*>(&material_diffuse[0])); glUniform3fv(glGetUniformLocation(program, "Material.Ks"), 1, static_cast<GLfloat*>(&material_specular[0])); glUniform1f(glGetUniformLocation(program, "Material.Shininess"), shininess); glClearColor(1.0, 1.0, 1.0, 1.0); }