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 A compile-time fixed-size numeric matrix container.
const uint8_t & b(size_t i) const
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
#define THROW_EXCEPTION(msg)
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
void onUpdateBuffers_all()
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
void onUpdateBuffers_Triangles() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
const uint8_t & r(size_t i) const
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
const float & x(size_t i) const
std::vector< mrpt::img::TColor > * lines_cbd
std::vector< mrpt::math::TPoint3Df > * pts_vbd
A triangle (float coordinates) with RGBA colors (u8) and UV (texture coordinates) for each vertex...
mrpt::opengl::shader_id_t shader_id
The base class of 3D objects that can be directly rendered through OpenGL.
void onUpdateBuffers_Points() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
const uint8_t & g(size_t i) const
void loadScene(const std::string &file_name)
Loads a scene from a file in any supported file.
mrpt::math::TPoint3D m_bbox_min
Bounding box.
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Simulation of ray-trace, given a pose.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
Context for calls to render()
mrpt::math::TVector3D rotateVector(const mrpt::math::TVector3D &local) const
Rotates a vector (i.e.
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
const uint8_t & a(size_t i) const
std::vector< mrpt::img::TColor > m_color_buffer_data
std::array< Vertex, 3 > vertices
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
This base provides a set of functions for maths stuff.
mrpt::math::TPoint3D m_bbox_max
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
void clear()
Empty the object.
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
std::vector< mrpt::math::TPoint3Df > * lines_vbd
static constexpr shader_id_t WIREFRAME
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
virtual ~CAssimpModel() override
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
static constexpr shader_id_t TRIANGLES
std::vector< mrpt::img::TColor > * pts_cbd
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
std::vector< mrpt::img::TColor > m_color_buffer_data
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::vector< mrpt::opengl::TTriangle > * tris
const float & y(size_t i) const
Virtual base class for "archives": classes abstracting I/O streams.
std::vector< mrpt::opengl::TTriangle > m_triangles
List of triangles.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
mrpt::vision::TStereoCalibResults out
const float & z(size_t i) const
An RGBA color - floats in the range [0,1].
The namespace for 3D scene representation and rendering.
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::optional_ref< mrpt::math::CMatrixDouble33 > out_jacobian_df_dpoint=std::nullopt, mrpt::optional_ref< mrpt::math::CMatrixDouble36 > out_jacobian_df_dpose=std::nullopt, mrpt::optional_ref< mrpt::math::CMatrixDouble36 > out_jacobian_df_dse3=std::nullopt, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
TColor asTColor() const
Returns the 0-255 integer version of this color: RGBA_u8.
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void clear()
Clear the contents of this container.
This class can load & render 3D models in a number of different formats (requires the library assimp)...
static constexpr shader_id_t POINTS
std::shared_ptr< TImplAssimp > m_assimp_scene
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.