| 203 | } |
| 204 | |
| 205 | bool Bundle3D::loadObj(MeshDatas& meshdatas, |
| 206 | MaterialDatas& materialdatas, |
| 207 | NodeDatas& nodedatas, |
| 208 | std::string_view fullPath, |
| 209 | const char* mtl_basepath) |
| 210 | { |
| 211 | meshdatas.resetData(); |
| 212 | materialdatas.resetData(); |
| 213 | nodedatas.resetData(); |
| 214 | |
| 215 | std::string mtlPath = ""; |
| 216 | if (mtl_basepath) |
| 217 | mtlPath = mtl_basepath; |
| 218 | else |
| 219 | mtlPath = fullPath.substr(0, fullPath.find_last_of("\\/") + 1); |
| 220 | |
| 221 | std::vector<tinyobj::shape_t> shapes; |
| 222 | std::vector<tinyobj::material_t> materials; |
| 223 | auto ret = tinyobj::LoadObj(shapes, materials, fullPath.data(), mtlPath.c_str()); |
| 224 | if (ret.empty()) |
| 225 | { |
| 226 | // fill data |
| 227 | // convert material |
| 228 | int i = 0; |
| 229 | char buf[20]; |
| 230 | std::string dir; |
| 231 | auto last = fullPath.rfind('/'); |
| 232 | if (last != std::string::npos) |
| 233 | dir = fullPath.substr(0, last + 1); |
| 234 | for (auto&& material : materials) |
| 235 | { |
| 236 | NMaterialData materialdata; |
| 237 | |
| 238 | NTextureData tex; |
| 239 | tex.filename = material.diffuse_texname.empty() ? material.diffuse_texname : dir + material.diffuse_texname; |
| 240 | tex.type = NTextureData::Usage::Diffuse; |
| 241 | tex.wrapS = backend::SamplerAddressMode::CLAMP_TO_EDGE; |
| 242 | tex.wrapT = backend::SamplerAddressMode::CLAMP_TO_EDGE; |
| 243 | |
| 244 | auto dataId = fmt::format_to_z(buf, "{}", ++i); |
| 245 | materialdata.textures.emplace_back(tex); |
| 246 | materialdata.id = dataId; |
| 247 | material.name = dataId; |
| 248 | materialdatas.materials.emplace_back(materialdata); |
| 249 | } |
| 250 | |
| 251 | // convert mesh |
| 252 | i = 0; |
| 253 | for (auto&& shape : shapes) |
| 254 | { |
| 255 | auto mesh = shape.mesh; |
| 256 | MeshData* meshdata = new MeshData(); |
| 257 | MeshVertexAttrib attrib; |
| 258 | attrib.type = parseGLDataType("GL_FLOAT", 3); |
| 259 | |
| 260 | if (mesh.positions.size()) |
| 261 | { |
| 262 | attrib.vertexAttrib = shaderinfos::VertexKey::VERTEX_ATTRIB_POSITION; |
nothing calls this directly
no test coverage detected