Blender V5.0
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
8
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_mesh.hh"
18#include "obj_export_mtl.hh"
19
20namespace blender::io::obj {
21
22class IOBJCurve;
23class OBJMesh;
33
38 private:
39 const OBJExportParams &export_params_;
40 std::string outfile_path_;
41 FILE *outfile_;
42
43 public:
44 OBJWriter(const char *filepath, const OBJExportParams &export_params) noexcept(false);
45 ~OBJWriter();
46
47 FILE *get_outfile() const
48 {
49 return outfile_;
50 }
51
52 void write_header() const;
53
57 void write_object_name(FormatHandler &fh, const OBJMesh &obj_mesh_data) const;
61 void write_mtllib_name(StringRefNull mtl_filepath) const;
66 const OBJMesh &obj_mesh_data,
67 bool write_colors) const;
72 void write_uv_coords(FormatHandler &fh, OBJMesh &obj_mesh_data) const;
77 void write_normals(FormatHandler &fh, OBJMesh &obj_mesh_data);
86 const IndexOffsets &offsets,
87 const OBJMesh &obj_mesh_data,
88 FunctionRef<const char *(int)> matname_fn);
93 const IndexOffsets &offsets,
94 const OBJMesh &obj_mesh_data) const;
98 void write_nurbs_curve(FormatHandler &fh, const IOBJCurve &obj_nurbs_data) const;
99
100 private:
101 using func_vert_uv_normal_indices = void (OBJWriter::*)(FormatHandler &fh,
102 const IndexOffsets &offsets,
103 Span<int> vert_indices,
104 Span<int> uv_indices,
105 Span<int> normal_indices,
106 bool flip) const;
110 func_vert_uv_normal_indices get_face_element_writer(int total_uv_vertices) const;
111
115 void write_vert_uv_normal_indices(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 void write_vert_normal_indices(FormatHandler &fh,
125 const IndexOffsets &offsets,
126 Span<int> vert_indices,
127 Span<int> /*uv_indices*/,
128 Span<int> normal_indices,
129 bool flip) const;
133 void write_vert_uv_indices(FormatHandler &fh,
134 const IndexOffsets &offsets,
135 Span<int> vert_indices,
136 Span<int> uv_indices,
137 Span<int> /*normal_indices*/,
138 bool flip) const;
142 void write_vert_indices(FormatHandler &fh,
143 const IndexOffsets &offsets,
144 Span<int> vert_indices,
145 Span<int> /*uv_indices*/,
146 Span<int> /*normal_indices*/,
147 bool flip) const;
148};
149
154 private:
155 FormatHandler fmt_handler_;
156 FILE *outfile_ = nullptr;
157 std::string mtl_filepath_;
158 Vector<MTLMaterial> mtlmaterials_;
159 /* Map from a Material* to an index into mtlmaterials_. */
160 Map<const Material *, int> material_map_;
161
162 public:
163 /*
164 * Create the `.MTL` file.
165 */
166 MTLWriter(const char *obj_filepath, bool write_file) noexcept(false);
167 ~MTLWriter();
168
169 void write_header(const char *blen_filepath);
175 void write_materials(const char *blen_filepath,
176 ePathReferenceMode path_mode,
177 const char *dest_dir,
178 bool write_pbr);
188 Vector<int> add_materials(const OBJMesh &mesh_to_export);
189 const char *mtlmaterial_name(int index);
190
191 private:
195 void write_bsdf_properties(const MTLMaterial &mtl_material, bool write_pbr);
199 void write_texture_map(const MTLMaterial &mtl_material,
200 MTLTexMapType texture_key,
201 const MTLTexMap &texture_map,
202 const char *blen_filedir,
203 const char *dest_dir,
205 Set<std::pair<std::string, std::string>> &copy_set);
206};
207} // namespace blender::io::obj
ePathReferenceMode
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
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_mtllib_name(StringRefNull mtl_filepath) const
void write_uv_coords(FormatHandler &fh, OBJMesh &obj_mesh_data) const
void write_nurbs_curve(FormatHandler &fh, const IOBJCurve &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