Move these to a native resource handler class
| 141 | |
| 142 | // Move these to a native resource handler class |
| 143 | std::vector<CarModel> LoadOBJ(std::string obj_path) { |
| 144 | std::vector<CarModel> meshes; |
| 145 | |
| 146 | tinyobj::attrib_t attrib; |
| 147 | std::vector<tinyobj::shape_t> shapes; |
| 148 | std::vector<tinyobj::material_t> materials; |
| 149 | std::string err; |
| 150 | std::string warn; |
| 151 | if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, obj_path.c_str(), nullptr, true, true)) { |
| 152 | LOG(WARNING) << err; |
| 153 | return meshes; |
| 154 | } |
| 155 | // Loop over shapes |
| 156 | for (size_t s = 0; s < shapes.size(); s++) { |
| 157 | std::vector<glm::vec3> verts = std::vector<glm::vec3>(); |
| 158 | std::vector<glm::vec3> norms = std::vector<glm::vec3>(); |
| 159 | std::vector<glm::vec2> uvs = std::vector<glm::vec2>(); |
| 160 | std::vector<unsigned int> indices = std::vector<unsigned int>(); |
| 161 | // Loop over faces(polygon) |
| 162 | size_t index_offset = 0; |
| 163 | for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { |
| 164 | int fv = shapes[s].mesh.num_face_vertices[f]; |
| 165 | // Loop over vertices in the face. |
| 166 | for (size_t v = 0; v < fv; v++) { |
| 167 | // access to vertex |
| 168 | tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; |
| 169 | indices.emplace_back((const unsigned int &) idx.vertex_index); |
| 170 | |
| 171 | verts.emplace_back(glm::vec3(attrib.vertices[3 * idx.vertex_index + 0] * 0.1, |
| 172 | attrib.vertices[3 * idx.vertex_index + 1] * 0.1, |
| 173 | attrib.vertices[3 * idx.vertex_index + 2] * 0.1)); |
| 174 | norms.emplace_back( |
| 175 | glm::vec3(attrib.normals[3 * idx.normal_index + 0], attrib.normals[3 * idx.normal_index + 1], |
| 176 | attrib.normals[3 * idx.normal_index + 2])); |
| 177 | uvs.emplace_back(glm::vec2(attrib.texcoords[2 * idx.texcoord_index + 0], |
| 178 | 1.0f - attrib.texcoords[2 * idx.texcoord_index + 1])); |
| 179 | } |
| 180 | index_offset += fv; |
| 181 | // per-face material |
| 182 | shapes[s].mesh.material_ids[f]; |
| 183 | } |
| 184 | CarModel obj_mesh = CarModel(shapes[s].name + "_obj", verts, uvs, norms, indices, glm::vec3(0, 0, 0), 0.01f, 0.0f, 0.5f); |
| 185 | meshes.emplace_back(obj_mesh); |
| 186 | } |
| 187 | return meshes; |
| 188 | } |
| 189 | |
| 190 | bool ExtractVIV(const std::string &viv_path, const std::string &output_dir) { |
| 191 | LOG(INFO) << "Extracting VIV file: " << viv_path << " to " << output_dir; |