FIX:Relax restrictions on importing obj files

jira: none
Change-Id: I61a0156a8424a5f59922956918d37d38e2c3306a
This commit is contained in:
zhou.xu 2024-04-24 09:43:35 +08:00 committed by Lane.Wei
parent a5fb3ce6c6
commit 38f6d5b4f5
2 changed files with 20 additions and 14 deletions

View File

@ -36,12 +36,25 @@ bool load_obj(const char *path, TriangleMesh *meshptr, ObjInfo& obj_info, std::s
bool exist_mtl = false;
if (data.mtllibs.size() > 0) { // read mtl
for (auto mtl_name : data.mtllibs) {
boost::filesystem::path full_path(path);
std::string dir = full_path.parent_path().string();
auto mtl_file = dir + "/" + mtl_name;
boost::filesystem::path mtl_path(mtl_file);
auto _mtl_path = mtl_path.string().c_str();
if (boost::filesystem::exists(mtl_path)) {
if (mtl_name.size() == 0){
continue;
}
exist_mtl = true;
bool mtl_name_is_path = false;
boost::filesystem::path mtl_abs_path(mtl_name);
if (boost::filesystem::exists(mtl_abs_path)) {
mtl_name_is_path = true;
}
boost::filesystem::path mtl_path;
if (!mtl_name_is_path) {
boost::filesystem::path full_path(path);
std::string dir = full_path.parent_path().string();
auto mtl_file = dir + "/" + mtl_name;
boost::filesystem::path temp_mtl_path(mtl_file);
mtl_path = temp_mtl_path;
}
auto _mtl_path = mtl_name_is_path ? mtl_abs_path.string().c_str() : mtl_path.string().c_str();
if (boost::filesystem::exists(mtl_name_is_path ? mtl_abs_path : mtl_path)) {
if (!ObjParser::mtlparse(_mtl_path, mtl_data)) {
BOOST_LOG_TRIVIAL(error) << "load_obj:load_mtl: failed to parse " << _mtl_path;
message = _L("load mtl in obj: failed to parse");
@ -52,7 +65,6 @@ bool load_obj(const char *path, TriangleMesh *meshptr, ObjInfo& obj_info, std::s
BOOST_LOG_TRIVIAL(error) << "load_obj: failed to load mtl_path:" << _mtl_path;
}
}
exist_mtl = true;
}
// Count the faces and verify, that all faces are triangular.
size_t num_faces = 0;

View File

@ -237,7 +237,7 @@ static bool obj_parseline(const char *line, ObjData &data)
}
}
if (vertex.coordIdx < 0)
vertex.coordIdx += (int)data.coordinates.size() / 4;
vertex.coordIdx += (int) data.coordinates.size() / OBJ_VERTEX_LENGTH;
else
-- vertex.coordIdx;
if (vertex.normalIdx < 0)
@ -604,9 +604,6 @@ bool objparse(const char *path, ObjData &data)
BOOST_LOG_TRIVIAL(error) << "ObjParser: Out of memory";
}
::fclose(pFile);
// printf("vertices: %d\r\n", data.vertices.size() / 4);
// printf("coords: %d\r\n", data.coordinates.size());
return true;
}
@ -646,9 +643,6 @@ bool mtlparse(const char *path, MtlData &data)
BOOST_LOG_TRIVIAL(error) << "MtlParser: Out of memory";
}
::fclose(pFile);
// printf("vertices: %d\r\n", data.vertices.size() / 4);
// printf("coords: %d\r\n", data.coordinates.size());
return true;
}