41 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
43 auto new_geometry = [&]() {
44 r_all_geometries.append(std::make_unique<Geometry>());
45 Geometry *g = r_all_geometries.last().get();
55 return new_geometry();
70 return new_geometry();
78 r_global_vertices.
vertices.append(vert);
85 if (srgb.x >= 0 && srgb.y >= 0 && srgb.z >= 0) {
90 else if (srgb.x > 0) {
104 const int mrgb_length = 8;
105 while (p + mrgb_length <= end) {
107 std::from_chars_result res = std::from_chars(p, p + mrgb_length, value, 16);
108 if (
ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range)) {
112 srgb[0] = (value >> 16) & 0xFF;
113 srgb[1] = (value >> 8) & 0xFF;
114 srgb[2] = value & 0xFF;
119 r_global_vertices.
mrgb_block.append(
float3(linear[0], linear[1], linear[2]));
156 r_index += r_index < 0 ? n_elems : -1;
157 if (r_index < 0 || r_index >= n_elems) {
158 CLOG_WARN(&
LOG,
"Invalid vertex index %i (valid range [0, %zu))", r_index, n_elems);
179 int last_vertex_index;
202 geom->
edges_.append({last_vertex_index, vertex_index});
204 last_vertex_index = vertex_index;
212 const int material_index,
213 const int group_index,
214 const bool shaded_smooth)
219 if (group_index >= 0) {
227 bool face_valid =
true;
229 while (p < end && face_valid) {
231 bool got_uv =
false, got_normal =
false;
241 if (p < end && *p ==
'/') {
244 if (p < end && *p !=
'/') {
249 if (p < end && *p ==
'/') {
259 "Invalid vertex index %i (valid range [0, %zu)), ignoring face",
261 size_t(global_vertices.
vertices.size()));
268 if (got_uv && !global_vertices.
uv_vertices.is_empty()) {
272 "Invalid UV index %i (valid range [0, %zu)), ignoring face",
281 if (got_normal && !global_vertices.
vert_normals.is_empty()) {
289 "Invalid normal index %i (valid range [0, %zu)), ignoring face",
319 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
322 if (!
StringRef(p, end).startswith(
"bspline") && !
StringRef(p, end).startswith(
"rat bspline")) {
323 CLOG_WARN(&
LOG,
"Curve type not supported: '%s'", std::string(p, end).c_str());
351 index += index < 0 ? global_vertices.
vertices.size() : -1;
364 CLOG_WARN(&
LOG,
"OBJ curve surfaces are not supported, found '%c'", *p);
384 if (rest_line.
find(
"off") != string::npos || rest_line.
find(
"null") != string::npos ||
385 rest_line.
find(
"default") != string::npos)
391 r_group_name = rest_line;
400 r_state_shaded_smooth =
false;
406 r_state_shaded_smooth = smooth != 0;
411 bool &r_state_shaded_smooth,
412 std::string &r_state_group_name,
413 int &r_state_material_index,
415 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
417 r_state_shaded_smooth =
false;
418 r_state_group_name =
"";
422 r_state_material_index = -1;
428 : import_params_(import_params), read_buffer_size_(read_buffer_size)
430 obj_file_ =
BLI_fopen(import_params_.filepath,
"rb");
432 CLOG_ERROR(&
LOG,
"Cannot read from OBJ file:'%s'.", import_params_.filepath);
435 "OBJ Import: Cannot open file '%s'",
436 import_params_.filepath);
451 const size_t keyword_len = keyword.
size();
452 if (end - p < keyword_len + 1) {
455 if (memcmp(p, keyword.
data(), keyword_len) != 0) {
460 if (p[keyword_len] >
' ') {
463 p += keyword_len + 1;
470 const Span<std::unique_ptr<Geometry>> all_geometries,
474 if (std::all_of(all_geometries.begin(),
475 all_geometries.end(),
476 [](
const std::unique_ptr<Geometry> &g) { return g->get_vertex_count() == 0; }))
499 bool state_shaded_smooth =
false;
500 string state_group_name;
501 int state_group_index = -1;
502 string state_material_name;
503 int state_material_index = -1;
509 size_t buffer_offset = 0;
510 size_t line_number = 0;
513 size_t bytes_read = fread(buffer.
data() + buffer_offset, 1, read_buffer_size_, obj_file_);
514 if (bytes_read == 0 && buffer_offset == 0) {
521 buffer.
data() + buffer_offset + bytes_read);
524 if (bytes_read < read_buffer_size_) {
525 if (bytes_read == 0 || buffer[buffer_offset + bytes_read - 1] !=
'\n') {
526 buffer[buffer_offset + bytes_read] =
'\n';
531 size_t buffer_end = buffer_offset + bytes_read;
532 if (buffer_end == 0) {
537 size_t last_nl = buffer_end;
538 while (last_nl > 0) {
540 if (buffer[last_nl] ==
'\n') {
544 if (buffer[last_nl] !=
'\n') {
547 "OBJ file contains a line #%zu that is too long (max. length %zu)",
559 const char *p = line.
begin(), *end = line.
end();
581 if (state_material_index == -1 && !state_material_name.empty() &&
586 state_material_index = 0;
593 state_material_index,
595 state_shaded_smooth);
603 if (import_params_.use_split_objects) {
608 state_material_index,
615 if (import_params_.use_split_groups) {
620 state_material_index,
628 if (new_index == state_group_index) {
643 if (new_mat_index == state_material_index) {
648 add_mtl_library(
StringRef(p, end).trim());
654 else if (*p ==
'#') {
670 else if (
StringRef(p, end).startswith(
"end")) {
674 CLOG_WARN(&
LOG,
"OBJ element not recognized: '%s'", std::string(p, end).c_str());
680 size_t left_size = buffer_end - last_nl;
681 memmove(buffer.
data(), buffer.
data() + last_nl, left_size);
682 buffer_offset = left_size;
687 add_default_mtl_library();
764 "Only the 'sphere' MTL projection type is supported, found: '%s'",
765 std::string(line).c_str());
774 for (
int i = 0;
i < opt.second; ++
i) {
788 const char *mtl_dir_path)
794 if (!is_map && !is_refl && !is_bump) {
800 CLOG_WARN(&
LOG,
"MTL texture map type not supported: '%s'", std::string(line).c_str());
816 return mtl_libraries_;
819void OBJParser::add_mtl_library(
StringRef path)
826 if (!mtl_libraries_.
contains(path)) {
827 mtl_libraries_.
append(path);
831void OBJParser::add_default_mtl_library()
844 add_mtl_library(mtl_file_base);
860 if (buffer ==
nullptr) {
861 CLOG_ERROR(&
LOG,
"OBJ import: cannot read from MTL file: '%s'", mtl_file_path_);
870 const char *p = line.
begin(), *end = line.
end();
878 if (r_materials.contains(mat_name)) {
883 r_materials.lookup_or_add(
string(mat_name), std::make_unique<MTLMaterial>()).get();
886 else if (material !=
nullptr) {
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
File and directory operations.
MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
MINLINE float normalize_v3(float n[3])
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
#define BLI_path_join(...)
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
#define CLOG_ERROR(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
void add_new(const Key &key, const Value &value)
Value & lookup_or_add(const Key &key, const Value &value)
bool contains(const T &value) const
void append(const T &value)
constexpr int64_t find(char c, int64_t pos=0) const
constexpr const char * begin() const
constexpr const char * end() const
constexpr bool is_empty() const
constexpr bool startswith(StringRef prefix) const
constexpr bool endswith(StringRef suffix) const
constexpr int64_t size() const
constexpr StringRef trim() const
constexpr const char * data() const
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef drop_suffix(int64_t n) const
void append(const T &value)
void parse_and_store(Map< std::string, std::unique_ptr< MTLMaterial > > &r_materials)
MTLParser(StringRefNull mtl_library_, StringRefNull obj_filepath)
Span< std::string > mtl_libraries() const
OBJParser(const OBJImportParams &import_params, size_t read_buffer_size)
void parse(Vector< std::unique_ptr< Geometry > > &r_all_geometries, GlobalVertices &r_global_vertices)
void MEM_freeN(void *vmemh)
static void use_all_vertices_if_no_faces(Geometry *geom, const Span< std::unique_ptr< Geometry > > all_geometries, const GlobalVertices &global_vertices)
static void geom_new_object(const char *p, const char *end, bool &r_state_shaded_smooth, std::string &r_state_group_name, int &r_state_material_index, Geometry *&r_curr_geom, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
static void geom_add_curve_vertex_indices(Geometry *geom, const char *p, const char *end, const GlobalVertices &global_vertices)
static void geom_add_vertex(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void geom_add_mrgb_colors(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void parse_texture_map(const char *p, const char *end, MTLMaterial *material, const char *mtl_dir_path)
static void geom_add_uv_vertex(const char *p, const char *end, GlobalVertices &r_global_vertices)
static const char * parse_vertex_index(const char *p, const char *end, size_t n_elems, int &r_index)
static MTLTexMapType mtl_line_start_to_texture_type(const char *&p, const char *end)
static void geom_add_polyline(Geometry *geom, const char *p, const char *end, GlobalVertices &r_global_vertices)
static bool parse_keyword(const char *&p, const char *end, StringRef keyword)
static bool parse_texture_option(const char *&p, const char *end, MTLMaterial *material, MTLTexMap &tex_map)
static void geom_add_vertex_normal(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void geom_add_curve_parameters(Geometry *geom, const char *p, const char *end)
static void geom_set_curve_degree(Geometry *geom, const char *p, const char *end)
static void geom_update_smooth_group(const char *p, const char *end, bool &r_state_shaded_smooth)
static void geom_update_group(const StringRef rest_line, std::string &r_group_name)
static Geometry * geom_set_curve_type(Geometry *geom, const char *p, const char *end, const StringRef group_name, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
static const std::pair< StringRef, int > unsupported_texture_options[]
static void geom_add_polygon(Geometry *geom, const char *p, const char *end, const GlobalVertices &global_vertices, const int material_index, const int group_index, const bool shaded_smooth)
static Geometry * create_geometry(Geometry *const prev_geometry, const eGeometryType new_type, StringRef name, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
const char * parse_float(const char *p, const char *end, float fallback, float &dst, bool skip_space, bool require_trailing_space)
const char * parse_floats(const char *p, const char *end, float fallback, float *dst, int count, bool require_trailing_space)
const char * drop_non_whitespace(const char *p, const char *end)
StringRef read_next_line(StringRef &buffer)
const char * parse_int(const char *p, const char *end, int fallback, int &dst, bool skip_space)
void fixup_line_continuations(char *p, char *end)
const char * drop_whitespace(const char *p, const char *end)
VecBase< float, 2 > float2
VecBase< float, 3 > float3
void track_all_vertices(int count)
Vector< std::string > material_order_
Vector< FaceCorner > face_corners_
Map< std::string, int > group_indices_
NurbsElement nurbs_element_
Vector< std::string > group_order_
Map< std::string, int > material_indices_
void track_vertex_index(int index)
std::string geometry_name_
Vector< FaceElem > face_elements_
void set_vertex_weight(size_t index, float weight)
void set_vertex_color(size_t index, float3 color)
Vector< float3 > vertices
Vector< float2 > uv_vertices
Vector< float3 > vert_normals
Vector< float3 > mrgb_block
const MTLTexMap & tex_map_of_type(MTLTexMapType key) const
Vector< int > curv_indices