42 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
44 auto new_geometry = [&]() {
45 r_all_geometries.append(std::make_unique<Geometry>());
46 Geometry *g = r_all_geometries.last().get();
56 return new_geometry();
71 return new_geometry();
79 r_global_vertices.
vertices.append(vert);
86 if (srgb.x >= 0 && srgb.y >= 0 && srgb.z >= 0) {
91 else if (srgb.x > 0) {
105 const int mrgb_length = 8;
106 while (p + mrgb_length <= end) {
108 std::from_chars_result res = std::from_chars(p, p + mrgb_length, value, 16);
109 if (
ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range)) {
113 srgb[0] = (value >> 16) & 0xFF;
114 srgb[1] = (value >> 8) & 0xFF;
115 srgb[2] = value & 0xFF;
120 r_global_vertices.
mrgb_block.append(
float3(linear[0], linear[1], linear[2]));
157 r_index += r_index < 0 ? n_elems : -1;
158 if (r_index < 0 || r_index >= n_elems) {
159 CLOG_WARN(&
LOG,
"Invalid vertex index %i (valid range [0, %zu))", r_index, n_elems);
180 int last_vertex_index;
203 geom->
edges_.append({last_vertex_index, vertex_index});
205 last_vertex_index = vertex_index;
213 const int material_index,
214 const int group_index,
215 const bool shaded_smooth)
220 if (group_index >= 0) {
228 bool face_valid =
true;
230 while (p < end && face_valid) {
232 bool got_uv =
false, got_normal =
false;
242 if (p < end && *p ==
'/') {
245 if (p < end && *p !=
'/') {
250 if (p < end && *p ==
'/') {
260 "Invalid vertex index %i (valid range [0, %zu)), ignoring face",
262 size_t(global_vertices.
vertices.size()));
269 if (got_uv && !global_vertices.
uv_vertices.is_empty()) {
273 "Invalid UV index %i (valid range [0, %zu)), ignoring face",
282 if (got_normal && !global_vertices.
vert_normals.is_empty()) {
290 "Invalid normal index %i (valid range [0, %zu)), ignoring face",
320 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
323 if (!
StringRef(p, end).startswith(
"bspline") && !
StringRef(p, end).startswith(
"rat bspline")) {
324 CLOG_WARN(&
LOG,
"Curve type not supported: '%s'",
string(p, end).c_str());
352 index += index < 0 ? global_vertices.
vertices.size() : -1;
365 CLOG_WARN(&
LOG,
"OBJ curve surfaces are not supported, found '%c'", *p);
385 if (rest_line.
find(
"off") != string::npos || rest_line.
find(
"null") != string::npos ||
386 rest_line.
find(
"default") != string::npos)
392 r_group_name = rest_line;
401 r_state_shaded_smooth =
false;
407 r_state_shaded_smooth = smooth != 0;
412 bool &r_state_shaded_smooth,
413 string &r_state_group_name,
414 int &r_state_material_index,
416 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
418 r_state_shaded_smooth =
false;
419 r_state_group_name =
"";
423 r_state_material_index = -1;
429 : import_params_(import_params), read_buffer_size_(read_buffer_size)
431 obj_file_ =
BLI_fopen(import_params_.filepath,
"rb");
433 CLOG_ERROR(&
LOG,
"Cannot read from OBJ file:'%s'.", import_params_.filepath);
436 "OBJ Import: Cannot open file '%s'",
437 import_params_.filepath);
452 const size_t keyword_len = keyword.
size();
453 if (end - p < keyword_len + 1) {
456 if (memcmp(p, keyword.
data(), keyword_len) != 0) {
461 if (p[keyword_len] >
' ') {
464 p += keyword_len + 1;
471 const Span<std::unique_ptr<Geometry>> all_geometries,
475 if (std::all_of(all_geometries.begin(),
476 all_geometries.end(),
477 [](
const std::unique_ptr<Geometry> &g) { return g->get_vertex_count() == 0; }))
484size_t OBJParser::parse_string_buffer(
StringRef &buffer_str,
485 Vector<std::unique_ptr<Geometry>> &r_all_geometries,
486 GlobalVertices &r_global_vertices,
488 bool &state_shaded_smooth,
489 string &state_group_name,
490 int &state_group_index,
491 string &state_material_name,
492 int &state_material_index)
494 size_t read_lines_num = 0;
497 const char *p = line.
begin(), *end = line.
end();
519 if (state_material_index == -1 && !state_material_name.empty() &&
520 curr_geom->material_indices_.is_empty())
522 curr_geom->material_indices_.add_new(state_material_name, 0);
523 curr_geom->material_order_.append(state_material_name);
524 state_material_index = 0;
531 state_material_index,
533 state_shaded_smooth);
541 if (import_params_.use_split_objects) {
546 state_material_index,
553 if (import_params_.use_split_groups) {
558 state_material_index,
564 int new_index = curr_geom->group_indices_.size();
565 state_group_index = curr_geom->group_indices_.lookup_or_add(state_group_name, new_index);
566 if (new_index == state_group_index) {
567 curr_geom->group_order_.append(state_group_name);
577 state_material_name = StringRef(p, end).trim();
578 int new_mat_index = curr_geom->material_indices_.size();
579 state_material_index = curr_geom->material_indices_.lookup_or_add(state_material_name,
581 if (new_mat_index == state_material_index) {
582 curr_geom->material_order_.append(state_material_name);
586 add_mtl_library(StringRef(p, end).trim());
592 else if (*p ==
'#') {
608 else if (StringRef(p, end).startswith(
"end")) {
612 CLOG_WARN(&
LOG,
"OBJ element not recognized: '%s'",
string(p, end).c_str());
615 return read_lines_num;
634 bool state_shaded_smooth =
false;
635 string state_group_name;
636 int state_group_index = -1;
637 string state_material_name;
638 int state_material_index = -1;
644 size_t buffer_offset = 0;
645 size_t line_number = 0;
648 size_t bytes_read = fread(buffer.
data() + buffer_offset, 1, read_buffer_size_, obj_file_);
649 if (bytes_read == 0 && buffer_offset == 0) {
656 buffer.
data() + buffer_offset + bytes_read);
659 if (bytes_read < read_buffer_size_) {
660 if (bytes_read == 0 || buffer[buffer_offset + bytes_read - 1] !=
'\n') {
661 buffer[buffer_offset + bytes_read] =
'\n';
666 size_t buffer_end = buffer_offset + bytes_read;
667 if (buffer_end == 0) {
672 size_t last_nl = buffer_end;
673 while (last_nl > 0) {
675 if (buffer[last_nl] ==
'\n') {
679 if (buffer[last_nl] !=
'\n') {
682 "OBJ file contains a line #%zu that is too long (max. length %zu)",
692 line_number += OBJParser::parse_string_buffer(buffer_str,
700 state_material_index);
704 size_t left_size = buffer_end - last_nl;
705 memmove(buffer.
data(), buffer.
data() + last_nl, left_size);
706 buffer_offset = left_size;
711 add_default_mtl_library();
788 "Only the 'sphere' MTL projection type is supported, found: '%s'",
789 string(line).c_str());
798 for (
int i = 0;
i < opt.second; ++
i) {
812 const char *mtl_dir_path)
818 if (!is_map && !is_refl && !is_bump) {
824 CLOG_WARN(&
LOG,
"MTL texture map type not supported: '%s'",
string(line).c_str());
840 return mtl_libraries_;
843void OBJParser::add_mtl_library(
StringRef path)
850 if (!mtl_libraries_.
contains(path)) {
851 mtl_libraries_.
append(path);
855void OBJParser::add_default_mtl_library()
868 add_mtl_library(mtl_file_base);
888 if (buffer ==
nullptr) {
889 CLOG_ERROR(&
LOG,
"OBJ import: cannot read from MTL file: '%s'", mtl_file_path_);
898 const char *p = line.
begin(), *end = line.
end();
908 r_materials.lookup_or_add(
string(mat_name), std::make_unique<MTLMaterial>()).get();
910 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
int BLI_path_normalize(char *path) 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,...)
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)
OBJParser(const OBJImportParams &import_params, size_t read_buffer_size)
Span< std::string > mtl_libraries() const
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_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_new_object(const char *p, const char *end, bool &r_state_shaded_smooth, 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_update_group(const StringRef rest_line, 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< FaceCorner > face_corners_
NurbsElement nurbs_element_
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