diff options
author | Aras Pranckevicius <aras@nesnausk.org> | 2022-05-03 14:49:53 +0300 |
---|---|---|
committer | Aras Pranckevicius <aras@nesnausk.org> | 2022-05-03 14:49:53 +0300 |
commit | f586c3ba21b671674f79b8a2a75070b7b6c8d558 (patch) | |
tree | 31a016f94e0030d5ccb470f3a5358154d188d308 /source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc | |
parent | 11d075f07da9c480219d776cea087ba29d867064 (diff) | |
parent | 5962db093f2f1fcf735ceaaa1ba6bbd25efc397f (diff) |
Merge branch 'blender-v3.2-release'
Diffstat (limited to 'source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc')
-rw-r--r-- | source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc | 50 |
1 files changed, 40 insertions, 10 deletions
diff --git a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc index de96dacce0d..6c3dc6e448b 100644 --- a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc +++ b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc @@ -71,7 +71,7 @@ static void geom_add_vertex(Geometry *geom, GlobalVertices &r_global_vertices) { float3 vert; - parse_floats(line, FLT_MAX, vert, 3); + parse_floats(line, 0.0f, vert, 3); r_global_vertices.vertices.append(vert); geom->vertex_count_++; } @@ -81,7 +81,7 @@ static void geom_add_vertex_normal(Geometry *geom, GlobalVertices &r_global_vertices) { float3 normal; - parse_floats(line, FLT_MAX, normal, 3); + parse_floats(line, 0.0f, normal, 3); r_global_vertices.vertex_normals.append(normal); geom->has_vertex_normals_ = true; } @@ -89,7 +89,7 @@ static void geom_add_vertex_normal(Geometry *geom, static void geom_add_uv_vertex(const StringRef line, GlobalVertices &r_global_vertices) { float2 uv; - parse_floats(line, FLT_MAX, uv, 2); + parse_floats(line, 0.0f, uv, 2); r_global_vertices.uv_vertices.append(uv); } @@ -461,7 +461,7 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries, } } else if (parse_keyword(line, "mtllib")) { - mtl_libraries_.append(line.trim()); + add_mtl_library(line.trim()); } /* Comments. */ else if (line.startswith("#")) { @@ -498,6 +498,8 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries, memmove(buffer.data(), buffer.data() + last_nl, left_size); buffer_offset = left_size; } + + add_default_mtl_library(); } static eMTLSyntaxElement mtl_line_start_to_enum(StringRef &line) @@ -556,7 +558,7 @@ static bool parse_texture_option(StringRef &line, MTLMaterial *material, tex_map return true; } if (parse_keyword(line, "-bm")) { - line = parse_float(line, 0.0f, material->map_Bump_strength); + line = parse_float(line, 1.0f, material->map_Bump_strength); return true; } if (parse_keyword(line, "-type")) { @@ -616,6 +618,30 @@ Span<std::string> OBJParser::mtl_libraries() const return mtl_libraries_; } +void OBJParser::add_mtl_library(const std::string &path) +{ + if (!mtl_libraries_.contains(path)) { + mtl_libraries_.append(path); + } +} + +void OBJParser::add_default_mtl_library() +{ + /* Add any existing .mtl file that's with the same base name as the .obj file + * into candidate .mtl files to search through. This is not technically following the + * spec, but the old python importer was doing it, and there are user files out there + * that contain "mtllib bar.mtl" for a foo.obj, and depend on finding materials + * from foo.mtl (see T97757). */ + char mtl_file_path[FILE_MAX]; + BLI_strncpy(mtl_file_path, import_params_.filepath, sizeof(mtl_file_path)); + BLI_path_extension_replace(mtl_file_path, sizeof(mtl_file_path), ".mtl"); + if (BLI_exists(mtl_file_path)) { + char mtl_file_base[FILE_MAX]; + BLI_split_file_part(mtl_file_path, mtl_file_base, sizeof(mtl_file_base)); + add_mtl_library(mtl_file_base); + } +} + MTLParser::MTLParser(StringRef mtl_library, StringRefNull obj_filepath) { char obj_file_dir[FILE_MAXDIR]; @@ -645,11 +671,12 @@ void MTLParser::parse_and_store(Map<string, std::unique_ptr<MTLMaterial>> &r_mat if (parse_keyword(line, "newmtl")) { line = line.trim(); - if (r_materials.remove_as(line)) { - std::cerr << "Duplicate material found:'" << line - << "', using the last encountered Material definition." << std::endl; + if (r_materials.contains(line)) { + material = nullptr; + } + else { + material = r_materials.lookup_or_add(string(line), std::make_unique<MTLMaterial>()).get(); } - material = r_materials.lookup_or_add(string(line), std::make_unique<MTLMaterial>()).get(); } else if (material != nullptr) { if (parse_keyword(line, "Ns")) { @@ -674,7 +701,10 @@ void MTLParser::parse_and_store(Map<string, std::unique_ptr<MTLMaterial>> &r_mat parse_float(line, 1.0f, material->d); } else if (parse_keyword(line, "illum")) { - parse_int(line, 2, material->illum); + /* Some files incorrectly use a float (T60135). */ + float val; + parse_float(line, 1.0f, val); + material->illum = val; } else { parse_texture_map(line, material, mtl_dir_path_); |