Blender V4.3
obj_export_file_writer.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#pragma once
10
11#include "BLI_map.hh"
12#include "BLI_set.hh"
13#include "BLI_vector.hh"
14
15#include "IO_wavefront_obj.hh"
16#include "obj_export_io.hh"
17#include "obj_export_mtl.hh"
18
19#include <iostream>
20
21namespace blender::io::obj {
22
23class OBJCurve;
24class OBJMesh;
34
39 private:
40 const OBJExportParams &export_params_;
41 std::string outfile_path_;
42 FILE *outfile_;
43
44 public:
45 OBJWriter(const char *filepath, const OBJExportParams &export_params) noexcept(false)
46 : export_params_(export_params), outfile_path_(filepath), outfile_(nullptr)
47 {
48 outfile_ = BLI_fopen(filepath, "wb");
49 if (!outfile_) {
50 throw std::system_error(errno, std::system_category(), "Cannot open file " + outfile_path_);
51 }
52 }
54 {
55 if (outfile_ && std::fclose(outfile_)) {
56 std::cerr << "Error: could not close the file '" << outfile_path_
57 << "' properly, it may be corrupted." << std::endl;
58 }
59 }
60
61 FILE *get_outfile() const
62 {
63 return outfile_;
64 }
65
66 void write_header() const;
67
71 void write_object_name(FormatHandler &fh, const OBJMesh &obj_mesh_data) const;
75 void write_mtllib_name(const StringRefNull mtl_filepath) const;
80 const OBJMesh &obj_mesh_data,
81 bool write_colors) const;
86 void write_uv_coords(FormatHandler &fh, OBJMesh &obj_mesh_data) const;
91 void write_normals(FormatHandler &fh, OBJMesh &obj_mesh_data);
100 const IndexOffsets &offsets,
101 const OBJMesh &obj_mesh_data,
102 FunctionRef<const char *(int)> matname_fn);
107 const IndexOffsets &offsets,
108 const OBJMesh &obj_mesh_data) const;
112 void write_nurbs_curve(FormatHandler &fh, const OBJCurve &obj_nurbs_data) const;
113
114 private:
115 using func_vert_uv_normal_indices = void (OBJWriter::*)(FormatHandler &fh,
116 const IndexOffsets &offsets,
117 Span<int> vert_indices,
118 Span<int> uv_indices,
119 Span<int> normal_indices,
120 bool flip) const;
124 func_vert_uv_normal_indices get_face_element_writer(int total_uv_vertices) const;
125
129 void write_vert_uv_normal_indices(FormatHandler &fh,
130 const IndexOffsets &offsets,
131 Span<int> vert_indices,
132 Span<int> uv_indices,
133 Span<int> normal_indices,
134 bool flip) const;
138 void write_vert_normal_indices(FormatHandler &fh,
139 const IndexOffsets &offsets,
140 Span<int> vert_indices,
141 Span<int> /*uv_indices*/,
142 Span<int> normal_indices,
143 bool flip) const;
147 void write_vert_uv_indices(FormatHandler &fh,
148 const IndexOffsets &offsets,
149 Span<int> vert_indices,
150 Span<int> uv_indices,
151 Span<int> /*normal_indices*/,
152 bool flip) const;
156 void write_vert_indices(FormatHandler &fh,
157 const IndexOffsets &offsets,
158 Span<int> vert_indices,
159 Span<int> /*uv_indices*/,
160 Span<int> /*normal_indices*/,
161 bool flip) const;
162};
163
168 private:
169 FormatHandler fmt_handler_;
170 FILE *outfile_ = nullptr;
171 std::string mtl_filepath_;
172 Vector<MTLMaterial> mtlmaterials_;
173 /* Map from a Material* to an index into mtlmaterials_. */
174 Map<const Material *, int> material_map_;
175
176 public:
177 /*
178 * Create the `.MTL` file.
179 */
180 MTLWriter(const char *obj_filepath, bool write_file) noexcept(false);
181 ~MTLWriter();
182
183 void write_header(const char *blen_filepath);
189 void write_materials(const char *blen_filepath,
190 ePathReferenceMode path_mode,
191 const char *dest_dir,
192 bool write_pbr);
202 Vector<int> add_materials(const OBJMesh &mesh_to_export);
203 const char *mtlmaterial_name(int index);
204
205 private:
209 void write_bsdf_properties(const MTLMaterial &mtl_material, bool write_pbr);
213 void write_texture_map(const MTLMaterial &mtl_material,
214 MTLTexMapType texture_key,
215 const MTLTexMap &texture_map,
216 const char *blen_filedir,
217 const char *dest_dir,
219 Set<std::pair<std::string, std::string>> &copy_set);
220};
221} // namespace blender::io::obj
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ePathReferenceMode
MTLWriter(const char *obj_filepath, bool write_file) noexcept(false)
Vector< int > add_materials(const OBJMesh &mesh_to_export)
void write_header(const char *blen_filepath)
void write_materials(const char *blen_filepath, ePathReferenceMode path_mode, const char *dest_dir, bool write_pbr)
const char * mtlmaterial_name(int index)
void write_normals(FormatHandler &fh, OBJMesh &obj_mesh_data)
OBJWriter(const char *filepath, const OBJExportParams &export_params) noexcept(false)
void write_uv_coords(FormatHandler &fh, OBJMesh &obj_mesh_data) const
void write_mtllib_name(const StringRefNull mtl_filepath) const
void write_nurbs_curve(FormatHandler &fh, const OBJCurve &obj_nurbs_data) const
void write_face_elements(FormatHandler &fh, const IndexOffsets &offsets, const OBJMesh &obj_mesh_data, FunctionRef< const char *(int)> matname_fn)
void write_vertex_coords(FormatHandler &fh, const OBJMesh &obj_mesh_data, bool write_colors) const
void write_edges_indices(FormatHandler &fh, const IndexOffsets &offsets, const OBJMesh &obj_mesh_data) const
void write_object_name(FormatHandler &fh, const OBJMesh &obj_mesh_data) const
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int