18 #if defined(MRPT_ASSIMP_VERSION_MAJOR) && MRPT_ASSIMP_VERSION_MAJOR < 3
19 #include <aiPostProcess.h>
23 #include <assimp/cimport.h>
24 #include <assimp/postprocess.h>
25 #include <assimp/scene.h>
26 #include <assimp/DefaultLogger.hpp>
27 #include <assimp/LogStream.hpp>
44 std::vector<mrpt::math::TPoint3Df>* lines_vbd =
nullptr;
45 std::vector<mrpt::img::TColor>* lines_cbd =
nullptr;
46 std::vector<mrpt::math::TPoint3Df>* pts_vbd =
nullptr;
47 std::vector<mrpt::img::TColor>* pts_cbd =
nullptr;
48 std::vector<mrpt::opengl::TTriangle>* tris =
nullptr;
51 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
54 static void recursive_render(
60 static void get_bounding_box(
61 const aiScene* sc, aiVector3D* min, aiVector3D* max);
62 static void get_bounding_box_for_node(
63 const aiScene* sc,
const aiNode* nd, aiVector3D* min, aiVector3D* max,
65 #endif // MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
108 if (!m_assimp_scene->scene)
return;
117 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
118 auto* scene =
reinterpret_cast<aiScene*
>(m_assimp_scene->scene);
122 recursive_render(scene, scene->mRootNode, transf, re);
134 writeToStreamRender(
out);
136 const bool empty = m_assimp_scene->scene !=
nullptr;
141 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
159 readFromStreamRender(in);
186 #if MRPT_HAS_OPENGL_GLUT
188 if (!m_textureIds.empty())
190 glDeleteTextures(m_textureIds.size(), &m_textureIds[0]);
191 m_textureIds.clear();
193 m_textureIdMap.clear();
200 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
207 filepath.c_str(), aiProcessPreset_TargetRealtime_MaxQuality);
212 aiVector3D scene_min, scene_max;
214 get_bounding_box(scene, &scene_min, &scene_max);
246 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
252 aiReleaseImport((aiScene*)
scene);
264 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
266 static void get_bounding_box_for_node(
267 const aiScene* scene,
const aiNode* nd, aiVector3D* min, aiVector3D* max,
271 unsigned int n = 0, t;
274 aiMultiplyMatrix4(trafo, &nd->mTransformation);
276 for (; n < nd->mNumMeshes; ++n)
278 const aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
279 for (t = 0; t < mesh->mNumVertices; ++t)
281 aiVector3D tmp = mesh->mVertices[t];
282 aiTransformVecByMatrix4(&tmp, trafo);
284 min->x = std::min(min->x, tmp.x);
285 min->y = std::min(min->y, tmp.y);
286 min->z = std::min(min->z, tmp.z);
288 max->x = std::max(max->x, tmp.x);
289 max->y = std::max(max->y, tmp.y);
290 max->z = std::max(max->z, tmp.z);
294 for (n = 0; n < nd->mNumChildren; ++n)
296 get_bounding_box_for_node(scene, nd->mChildren[n], min, max, trafo);
301 static void get_bounding_box(
302 const aiScene* scene, aiVector3D* min, aiVector3D* max)
305 aiIdentityMatrix4(&trafo);
307 min->x = min->y = min->z = 1e10f;
308 max->x = max->y = max->z = -1e10f;
309 get_bounding_box_for_node(scene, scene->mRootNode, min, max, &trafo);
321 aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
323 return color4_to_TColor(diffuse);
328 return {0xa0, 0xa0, 0xa0, 0xff};
359 return {v.
x, v.y, v.z};
362 static void recursive_render(
366 aiMatrix4x4 m = nd->mTransformation;
373 for (
unsigned int n = 0; n < nd->mNumMeshes; ++n)
375 const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]];
378 apply_material(sc->mMaterials[mesh->mMaterialIndex]);
380 for (
unsigned int t = 0; t < mesh->mNumFaces; ++t)
382 const struct aiFace* face = &mesh->mFaces[t];
384 switch (face->mNumIndices)
388 for (
unsigned int i = 0; i < face->mNumIndices; i++)
391 int vertexIndex = face->mIndices[i];
392 if (mesh->mColors[0] !=
nullptr)
394 color4_to_TColor(mesh->mColors[0][vertexIndex]);
397 to_mrpt(mesh->mVertices[vertexIndex])));
398 re.
pts_cbd->emplace_back(color);
404 for (
unsigned int i = 0; i < face->mNumIndices; i++)
407 int vertexIndex = face->mIndices[i];
408 if (mesh->mColors[0] !=
nullptr)
410 color4_to_TColor(mesh->mColors[0][vertexIndex]);
413 to_mrpt(mesh->mVertices[vertexIndex])));
421 const unsigned int nTri = face->mNumIndices / 3;
424 for (
unsigned int iTri = 0; iTri < nTri; iTri++)
427 for (
unsigned int v = 0; v < 3; v++)
429 unsigned int i = iTri * 3 + v;
431 int vertexIndex = face->mIndices[i];
432 if (mesh->mColors[0] !=
nullptr)
433 color = color4_to_TColor(
434 mesh->mColors[0][vertexIndex]);
442 if (mesh->HasTextureCoords(0))
445 mesh->mTextureCoords[0][vertexIndex].x;
447 mesh->mTextureCoords[0][vertexIndex].y;
452 to_mrpt(mesh->mNormals[vertexIndex]));
455 to_mrpt(mesh->mVertices[vertexIndex]));
461 re.
tris->emplace_back(std::move(tri));
474 for (
unsigned int n = 0; n < nd->mNumChildren; ++n)
475 recursive_render(sc, nd->mChildren[n], curTf, re);
478 #endif // MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP