MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ write()

bool MayaFlux::IO::AssimpModelWriter::write ( const std::string & filepath,
const std::vector< Kakshya::MeshData > & meshes,
const ModelWriteOptions & options = {} )
overridevirtual

Write one or more meshes to disk as a single scene.

Parameters
filepathDestination path.
meshesOne or more meshes. Must each satisfy MeshData::is_valid().
optionsFormat-specific options.
Returns
true on success. On failure call get_last_error().

Implements MayaFlux::IO::ModelWriter.

Definition at line 101 of file AssimpModelWriter.cpp.

105{
106 m_last_error.clear();
107
108 if (meshes.empty()) {
109 set_error("No meshes to write");
110 return false;
111 }
112
113 for (const auto& m : meshes) {
114 if (!m.is_valid()) {
115 set_error("MeshData failed is_valid()");
116 return false;
117 }
118 if (m.layout.stride_bytes != sizeof(Kakshya::MeshVertex)) {
119 set_error("MeshData vertex stride is not the canonical MeshVertex layout");
120 return false;
121 }
122 }
123
124 const auto ext = extension_of(filepath);
125 std::string format_id = options.format_id;
126 if (format_id.empty()) {
127 format_id = default_format_id(ext);
128 }
129 if (format_id.empty()) {
130 set_error("No format_id resolved for extension '" + ext
131 + "', pass ModelWriteOptions::format_id explicitly");
132 return false;
133 }
134
135 // NOLINTBEGIN(cppcoreguidelines-owning-memory)
136 // aiScene is a C-style struct: every array is a raw owning pointer freed
137 // by aiScene's own destructor (and aiMesh's/aiMaterial's for their own
138 // sub-arrays). A smart pointer cannot be substituted at this boundary.
139 aiScene scene;
140 scene.mRootNode = new aiNode();
141
142 scene.mNumMaterials = static_cast<unsigned int>(meshes.size());
143 scene.mMaterials = new aiMaterial*[scene.mNumMaterials];
144
145 scene.mNumMeshes = static_cast<unsigned int>(meshes.size());
146 scene.mMeshes = new aiMesh*[scene.mNumMeshes];
147
148 scene.mRootNode->mNumMeshes = scene.mNumMeshes;
149 scene.mRootNode->mMeshes = new unsigned int[scene.mNumMeshes];
150
151 for (unsigned int mi = 0; mi < scene.mNumMeshes; ++mi) {
152 const auto& mesh_data = meshes[mi];
153 const auto info = submesh_info_for(mesh_data);
154
155 auto* mat = new aiMaterial();
156 const std::string mat_name = !options.material_name_override.empty()
157 ? options.material_name_override
158 : (!info.material_name.empty() ? info.material_name : "material_" + std::to_string(mi));
159 aiString ai_mat_name(mat_name);
160 mat->AddProperty(&ai_mat_name, AI_MATKEY_NAME);
161 if (!info.diffuse_path.empty()) {
162 aiString tex_path(info.diffuse_path);
163 mat->AddProperty(&tex_path, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
164 }
165 scene.mMaterials[mi] = mat;
166
167 auto* mesh = new aiMesh();
168 mesh->mMaterialIndex = mi;
169 mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
170 mesh->mName = aiString(!info.name.empty() ? info.name : "mesh_" + std::to_string(mi));
171
172 const auto* vb = std::get_if<std::vector<uint8_t>>(&mesh_data.vertex_variant);
173 const auto* ib = std::get_if<std::vector<uint32_t>>(&mesh_data.index_variant);
174
175 const auto* verts = reinterpret_cast<const Kakshya::MeshVertex*>(vb->data());
176 const size_t vertex_count = vb->size() / sizeof(Kakshya::MeshVertex);
177
178 mesh->mNumVertices = static_cast<unsigned int>(vertex_count);
179 mesh->mVertices = new aiVector3D[vertex_count];
180 mesh->mNormals = new aiVector3D[vertex_count];
181 mesh->mTangents = new aiVector3D[vertex_count];
182 mesh->mBitangents = new aiVector3D[vertex_count];
183 mesh->mTextureCoords[0] = new aiVector3D[vertex_count];
184 mesh->mNumUVComponents[0] = 2;
185 mesh->mColors[0] = new aiColor4D[vertex_count];
186
187 for (size_t v = 0; v < vertex_count; ++v) {
188 const auto& mv = verts[v];
189 mesh->mVertices[v] = aiVector3D(mv.position.x, mv.position.y, mv.position.z);
190 mesh->mNormals[v] = aiVector3D(mv.normal.x, mv.normal.y, mv.normal.z);
191 mesh->mTangents[v] = aiVector3D(mv.tangent.x, mv.tangent.y, mv.tangent.z);
192 const glm::vec3 bitangent = glm::cross(mv.normal, glm::vec3(mv.tangent)) * mv.tangent.w;
193 mesh->mBitangents[v] = aiVector3D(bitangent.x, bitangent.y, bitangent.z);
194 mesh->mTextureCoords[0][v] = aiVector3D(mv.uv.x, mv.uv.y, 0.0F);
195 mesh->mColors[0][v] = aiColor4D(mv.color.x, mv.color.y, mv.color.z, mv.color.w);
196 }
197
198 const size_t face_count = ib->size() / 3;
199 mesh->mNumFaces = static_cast<unsigned int>(face_count);
200 mesh->mFaces = new aiFace[face_count];
201 for (size_t f = 0; f < face_count; ++f) {
202 auto& face = mesh->mFaces[f];
203 face.mNumIndices = 3;
204 face.mIndices = new unsigned int[3] {
205 (*ib)[(f * 3) + 0],
206 (*ib)[(f * 3) + 1],
207 (*ib)[(f * 3) + 2],
208 };
209 }
210
211 scene.mMeshes[mi] = mesh;
212 scene.mRootNode->mMeshes[mi] = mi;
213 }
214 // NOLINTEND(cppcoreguidelines-owning-memory)
215
216 const auto resolved = resolve_write_path(filepath);
217
218 Assimp::Exporter exporter;
219 const auto ret = exporter.Export(&scene, format_id, resolved);
220
221 if (ret != AI_SUCCESS) {
222 set_error(exporter.GetErrorString());
224 "AssimpModelWriter: export failed for '{}' (format '{}'): {}",
225 resolved, format_id, m_last_error);
226 return false;
227 }
228
230 "AssimpModelWriter: wrote '{}', {} mesh(es), format '{}'",
231 resolved, meshes.size(), format_id);
232 return true;
233}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
void set_error(std::string msg) const
std::string resolve_write_path(const std::string &filepath, std::string_view default_directory=Config::OUT_DIR)
Resolve a writable path against the configured output directory.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.

References MayaFlux::Journal::FileIO, MayaFlux::IO::ModelWriteOptions::format_id, MayaFlux::Journal::IO, m_last_error, MayaFlux::IO::ModelWriteOptions::material_name_override, MF_ERROR, MF_INFO, MayaFlux::IO::resolve_write_path(), and set_error().

Referenced by ~AssimpModelWriter().

Here is the call graph for this function:
Here is the caller graph for this function: