MCPcopy Create free account
hub / github.com/ddiakopoulos/polymer / import_obj_model

Method import_obj_model

libraries/lib-model-io/src/model-io.cpp:66–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64}
65
66std::unordered_map<std::string, runtime_mesh> polymer::import_obj_model(const std::string & path)
67{
68 std::unordered_map<std::string, runtime_mesh> meshes;
69
70 tinyobj::attrib_t attrib;
71 std::vector<tinyobj::shape_t> shapes;
72 std::vector<tinyobj::material_t> materials;
73
74 std::string parentDir = parent_directory_from_filepath(path) + "/";
75
76 std::string err;
77 bool status = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, path.c_str(), parentDir.c_str());
78
79 // if (status && !err.empty()) std::cerr << "tinyobj warning: " << err << std::endl;
80
81 // Append `default` material
82 materials.push_back(tinyobj::material_t());
83
84 // std::cout << "# of shapes : " << shapes.size() << std::endl;
85 // std::cout << "# of materials : " << materials.size() << std::endl;
86
87 // Parse tinyobj data into geometry struct
88 for (unsigned int i = 0; i < shapes.size(); i++)
89 {
90 tinyobj::shape_t * shape = &shapes[i];
91 tinyobj::mesh_t * mesh = &shapes[i].mesh;
92
93 runtime_mesh & g = meshes[shape->name];
94
95 // std::cout << "Submesh Name: " << shape->name << std::endl;
96 // std::cout << "Num Indices: " << mesh->indices.size() << std::endl;
97 // std::cout << "Num TexCoords: " << attrib.texcoords.size() << std::endl;
98
99 size_t indexOffset = 0;
100
101 // de-duplicate vertices
102 unordered_map_generator<unique_vertex, uint32_t>::Type uniqueVertexMap;
103 bool shouldGenerateNormals = false;
104
105 for (size_t f = 0; f < mesh->num_face_vertices.size(); f++)
106 {
107 assert(mesh->num_face_vertices[f] == 3);
108
109 uint3 indices;
110 for (int v = 0; v < 3; v++)
111 {
112 const tinyobj::index_t idx = mesh->indices[indexOffset + v];
113
114 unique_vertex vertex;
115 vertex.position = { attrib.vertices[3 * idx.vertex_index + 0], attrib.vertices[3 * idx.vertex_index + 1], attrib.vertices[3 * idx.vertex_index + 2] };
116 if (attrib.normals.size()) vertex.normal = { attrib.normals[3 * idx.normal_index + 0], attrib.normals[3 * idx.normal_index + 1], attrib.normals[3 * idx.normal_index + 2] };
117 else shouldGenerateNormals = true;
118
119 if (idx.texcoord_index != -1) vertex.texcoord = { attrib.texcoords[2 * idx.texcoord_index + 0], attrib.texcoords[2 * idx.texcoord_index + 1] };
120
121 auto it = uniqueVertexMap.find(vertex);
122 if (it != uniqueVertexMap.end())
123 {

Callers

nothing calls this directly

Calls 6

LoadObjFunction · 0.85
material_tClass · 0.85
compute_normalsFunction · 0.85
sizeMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected