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
109 if (!m_assimp_scene->scene)
return;
118 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
119 auto* scene =
reinterpret_cast<aiScene*
>(m_assimp_scene->scene);
123 recursive_render(scene, scene->mRootNode, transf, re);
135 writeToStreamRender(
out);
137 const bool empty = m_assimp_scene->scene !=
nullptr;
142 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
160 readFromStreamRender(in);
187 #if MRPT_HAS_OPENGL_GLUT
189 if (!m_textureIds.empty())
191 glDeleteTextures(m_textureIds.size(), &m_textureIds[0]);
192 m_textureIds.clear();
194 m_textureIdMap.clear();
201 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
208 filepath.c_str(), aiProcessPreset_TargetRealtime_MaxQuality);
213 aiVector3D scene_min, scene_max;
215 get_bounding_box(scene, &scene_min, &scene_max);
243 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
249 aiReleaseImport((aiScene*)
scene);
261 #if MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP
263 static void get_bounding_box_for_node(
264 const aiScene* scene,
const aiNode* nd, aiVector3D* min, aiVector3D* max,
268 unsigned int n = 0, t;
271 aiMultiplyMatrix4(trafo, &nd->mTransformation);
273 for (; n < nd->mNumMeshes; ++n)
275 const aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
276 for (t = 0; t < mesh->mNumVertices; ++t)
278 aiVector3D tmp = mesh->mVertices[t];
279 aiTransformVecByMatrix4(&tmp, trafo);
281 min->x = std::min(min->x, tmp.x);
282 min->y = std::min(min->y, tmp.y);
283 min->z = std::min(min->z, tmp.z);
285 max->x = std::max(max->x, tmp.x);
286 max->y = std::max(max->y, tmp.y);
287 max->z = std::max(max->z, tmp.z);
291 for (n = 0; n < nd->mNumChildren; ++n)
293 get_bounding_box_for_node(scene, nd->mChildren[n], min, max, trafo);
298 static void get_bounding_box(
299 const aiScene* scene, aiVector3D* min, aiVector3D* max)
302 aiIdentityMatrix4(&trafo);
304 min->x = min->y = min->z = 1e10f;
305 max->x = max->y = max->z = -1e10f;
306 get_bounding_box_for_node(scene, scene->mRootNode, min, max, &trafo);
318 aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
320 return color4_to_TColor(diffuse);
325 return {0xa0, 0xa0, 0xa0, 0xff};
356 return {v.
x, v.y, v.z};
359 static void recursive_render(
363 aiMatrix4x4 m = nd->mTransformation;
370 for (
unsigned int n = 0; n < nd->mNumMeshes; ++n)
372 const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]];
375 apply_material(sc->mMaterials[mesh->mMaterialIndex]);
377 for (
unsigned int t = 0; t < mesh->mNumFaces; ++t)
379 const struct aiFace* face = &mesh->mFaces[t];
381 switch (face->mNumIndices)
385 for (
unsigned int i = 0; i < face->mNumIndices; i++)
388 int vertexIndex = face->mIndices[i];
389 if (mesh->mColors[0] !=
nullptr)
391 color4_to_TColor(mesh->mColors[0][vertexIndex]);
394 to_mrpt(mesh->mVertices[vertexIndex])));
395 re.
pts_cbd->emplace_back(color);
401 for (
unsigned int i = 0; i < face->mNumIndices; i++)
404 int vertexIndex = face->mIndices[i];
405 if (mesh->mColors[0] !=
nullptr)
407 color4_to_TColor(mesh->mColors[0][vertexIndex]);
410 to_mrpt(mesh->mVertices[vertexIndex])));
418 const unsigned int nTri = face->mNumIndices / 3;
421 for (
unsigned int iTri = 0; iTri < nTri; iTri++)
424 for (
unsigned int v = 0; v < 3; v++)
426 unsigned int i = iTri * 3 + v;
428 int vertexIndex = face->mIndices[i];
429 if (mesh->mColors[0] !=
nullptr)
430 color = color4_to_TColor(
431 mesh->mColors[0][vertexIndex]);
439 if (mesh->HasTextureCoords(0))
442 mesh->mTextureCoords[0][vertexIndex].x;
444 mesh->mTextureCoords[0][vertexIndex].y;
449 to_mrpt(mesh->mNormals[vertexIndex]));
452 to_mrpt(mesh->mVertices[vertexIndex]));
458 re.
tris->emplace_back(std::move(tri));
471 for (
unsigned int n = 0; n < nd->mNumChildren; ++n)
472 recursive_render(sc, nd->mChildren[n], curTf, re);
475 #endif // MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP