Skip to content

Commit

Permalink
WIP: new mrpt-viz library
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Jan 3, 2025
1 parent 2270811 commit 836ce33
Show file tree
Hide file tree
Showing 106 changed files with 20,635 additions and 55 deletions.
17 changes: 0 additions & 17 deletions libs/opengl/include/mrpt/opengl/CRenderizableShaderTriangles.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ class CRenderizableShaderTriangles : public virtual CRenderizable
m_vao.destroy();
}

bool isLightEnabled() const { return m_enableLight; }
void enableLight(bool enable = true) { m_enableLight = enable; }

/** Control whether to render the FRONT, BACK, or BOTH (default) set of
* faces. Refer to docs for glCullFace().
* Example: If set to `cullFaces(TCullFace::BACK);`, back faces will not be
* drawn ("culled")
*/
void cullFaces(const TCullFace& cf) { m_cullface = cf; }
TCullFace cullFaces() const { return m_cullface; }

/** @name Raw access to triangle shader buffer data
* @{ */
const auto& shaderTrianglesBuffer() const { return m_triangles; }
Expand All @@ -75,15 +64,9 @@ class CRenderizableShaderTriangles : public virtual CRenderizable
/** Returns the bounding box of m_triangles, or (0,0,0)-(0,0,0) if empty. */
const mrpt::math::TBoundingBoxf trianglesBoundingBox() const;

void params_serialize(mrpt::serialization::CArchive& out) const;
void params_deserialize(mrpt::serialization::CArchive& in);

private:
mutable Buffer m_trianglesBuffer;
mutable VertexArrayObject m_vao;

bool m_enableLight = true;
TCullFace m_cullface = TCullFace::NONE;
};

} // namespace mrpt::opengl
16 changes: 0 additions & 16 deletions libs/opengl/include/mrpt/opengl/CRenderizableShaderWireFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ class CRenderizableShaderWireFrame : public virtual CRenderizable
* to be drawn in "m_*_buffer" fields. */
virtual void onUpdateBuffers_Wireframe() = 0;

void setLineWidth(float w)
{
m_lineWidth = w;
CRenderizable::notifyChange();
}
float getLineWidth() const { return m_lineWidth; }
void enableAntiAliasing(bool enable = true)
{
m_antiAliasing = enable;
CRenderizable::notifyChange();
}
bool isAntiAliasingEnabled() const { return m_antiAliasing; }

// See base docs
void freeOpenGLResources() override
{
Expand All @@ -73,9 +60,6 @@ class CRenderizableShaderWireFrame : public virtual CRenderizable
mutable std::vector<mrpt::img::TColor> m_color_buffer_data;
mutable mrpt::containers::NonCopiableData<std::shared_mutex> m_wireframeMtx;

float m_lineWidth = 1.0f;
bool m_antiAliasing = false;

/** Returns the bounding box of m_vertex_buffer_data, or (0,0,0)-(0,0,0) if
* empty. */
const mrpt::math::TBoundingBox wireframeVerticesBoundingBox() const;
Expand Down
20 changes: 0 additions & 20 deletions libs/opengl/src/CRenderizableShaderTriangles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,3 @@ const math::TBoundingBoxf CRenderizableShaderTriangles::trianglesBoundingBox() c

return bb;
}

void CRenderizableShaderTriangles::params_serialize(mrpt::serialization::CArchive& out) const
{
out.WriteAs<uint8_t>(0); // serialization version
out << m_enableLight << static_cast<uint8_t>(m_cullface);
}
void CRenderizableShaderTriangles::params_deserialize(mrpt::serialization::CArchive& in)
{
const auto version = in.ReadAs<uint8_t>();

switch (version)
{
case 0:
in >> m_enableLight;
m_cullface = static_cast<TCullFace>(in.ReadAs<uint8_t>());
break;
default:
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
};
}
19 changes: 19 additions & 0 deletions libs/viz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Lists of directories with source files:
# See "DeclareMRPTLib.cmake" for explanations
# -------------------------------------------------

#---------------------------------------------
# Macro declared in "DeclareMRPTLib.cmake":
#---------------------------------------------
define_mrpt_lib(
# Lib name
viz
# Dependencies:
mrpt-poses
mrpt-img
)

if(NOT BUILD_mrpt-viz)
return()
endif()

127 changes: 127 additions & 0 deletions libs/viz/include/mrpt/viz/CArrow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2024, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
#pragma once

#include <mrpt/viz/CVisualObject.h>

namespace mrpt::viz
{
/** A 3D arrow
*
* ![mrpt::viz::CArrow](preview_CArrow.png)
*
* \sa opengl::Scene
* \ingroup mrpt_viz_grp
*/
class CArrow : virtual public CVisualObject, public VisualObjectParams_Triangles
{
DEFINE_SERIALIZABLE(CArrow, mrpt::viz)
DEFINE_SCHEMA_SERIALIZABLE()

public:
void setArrowEnds(float x0, float y0, float z0, float x1, float y1, float z1)
{
m_x0 = x0;
m_y0 = y0;
m_z0 = z0;
m_x1 = x1;
m_y1 = y1;
m_z1 = z1;
CVisualObject::notifyChange();
}
template <typename Vector3Like>
void setArrowEnds(const Vector3Like& start, const Vector3Like& end)
{
m_x0 = start[0];
m_y0 = start[1];
m_z0 = start[2];
m_x1 = end[0];
m_y1 = end[1];
m_z1 = end[2];
CVisualObject::notifyChange();
}
void setHeadRatio(float rat)
{
m_headRatio = rat;
CVisualObject::notifyChange();
}
void setSmallRadius(float rat)
{
m_smallRadius = rat;
CVisualObject::notifyChange();
}
void setLargeRadius(float rat)
{
m_largeRadius = rat;
CVisualObject::notifyChange();
}
/** Number of radial divisions */
void setSlicesCount(uint32_t slices)
{
m_slices = slices;
CVisualObject::notifyChange();
}

/** Number of radial divisions */
uint32_t getSlicesCount() const { return m_slices; }

mrpt::math::TBoundingBoxf internalBoundingBoxLocal() const override;

/** Constructor */
CArrow(
float x0 = 0,
float y0 = 0,
float z0 = 0,
float x1 = 1,
float y1 = 1,
float z1 = 1,
float headRatio = 0.2f,
float smallRadius = 0.05f,
float largeRadius = 0.2f) :
m_x0(x0),
m_y0(y0),
m_z0(z0),
m_x1(x1),
m_y1(y1),
m_z1(z1),
m_headRatio(headRatio),
m_smallRadius(smallRadius),
m_largeRadius(largeRadius)
{
}

CArrow(
const mrpt::math::TPoint3Df& from,
const mrpt::math::TPoint3Df& to,
float headRatio = 0.2f,
float smallRadius = 0.05f,
float largeRadius = 0.2f) :
m_x0(from.x),
m_y0(from.y),
m_z0(from.z),
m_x1(to.x),
m_y1(to.y),
m_z1(to.z),
m_headRatio(headRatio),
m_smallRadius(smallRadius),
m_largeRadius(largeRadius)
{
}
~CArrow() override = default;

protected:
mutable float m_x0, m_y0, m_z0;
mutable float m_x1, m_y1, m_z1;
float m_headRatio;
float m_smallRadius, m_largeRadius;
/** Number of radial divisions. */
uint32_t m_slices = 10;
};

} // namespace mrpt::viz
106 changes: 106 additions & 0 deletions libs/viz/include/mrpt/viz/CAssimpModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* +------------------------------------------------------------------------+
| Mobile Robot Programming Toolkit (MRPT) |
| https://www.mrpt.org/ |
| |
| Copyright (c) 2005-2024, Individual contributors, see AUTHORS file |
| See: https://www.mrpt.org/Authors - All rights reserved. |
| Released under BSD License. See: https://www.mrpt.org/License |
+------------------------------------------------------------------------+ */
#pragma once

#include <mrpt/img/CImage.h>
#include <mrpt/viz/CVisualObject.h>

#include <map>
#include <optional>

namespace mrpt::viz
{
/** This class can load & render 3D models in a number of different formats
* (requires the library assimp).
* - All supported formats:
* http://assimp.sourceforge.net/main_features_formats.html
* - Most common ones: AutoCAD DXF ( .dxf ), Collada ( .dae ), Blender 3D (
* .blend ), 3ds Max 3DS ( .3ds ), 3ds Max ASE ( .ase ), Quake I ( .mdl ), Quake
* II ( .md2 ), Quake III Mesh ( .md3 ), etc.
*
* Models are loaded via CAssimpModel::loadScene()
*
* ![mrpt::viz::CAssimpModel](preview_CAssimpModel.png)
*
* \sa opengl::Scene
* \ingroup mrpt_viz_grp
*/
class CAssimpModel : public CVisualObject
{
DEFINE_SERIALIZABLE(CAssimpModel, mrpt::viz)

public:
CAssimpModel();
virtual ~CAssimpModel() override;

/** Import flags for loadScene
* \note Not defined as ``enum class`` to allow C++-valid or-wise combinations
*/
struct LoadFlags
{
enum flags_t : uint32_t
{
/** See: aiProcessPreset_TargetRealtime_Fast */
RealTimeFast = 0x0001,

/** See: aiProcessPreset_TargetRealtime_Quality */
RealTimeQuality = 0x0002,

/** See: aiProcessPreset_TargetRealtime_MaxQuality */
RealTimeMaxQuality = 0x0004,

/** See: aiProcess_FlipUVs */
FlipUVs = 0x0010,

/** MRPT-specific: ignore materials and replace by the base class
CRenderizable uniform color that was defined before calling
loadScene(). \note (New in MRPT 2.5.0) */
IgnoreMaterialColor = 0x0100,

/** Displays messages on loaded textures, etc. */
Verbose = 0x1000
};
};

using filepath_t = std::string;

/** Loads a scene from a file in any supported file.
* \exception std::runtime_error On any error during loading or importing
* the file.
*/
void loadScene(
const std::string& file_name,
const int flags = LoadFlags::RealTimeMaxQuality | LoadFlags::FlipUVs | LoadFlags::Verbose);

/** Empty the object */
void clear();

/* Simulation of ray-trace. */
bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const override;

mrpt::math::TBoundingBoxf internalBoundingBoxLocal() const override;

/** Enable (or disable if set to .0f) a feature in which textured triangles
* are split into different renderizable smaller objects.
* This is required only for semitransparent objects with overlaping regions.
*/
void split_triangles_rendering_bbox(const float bbox_size);

[[nodiscard]] float split_triangles_rendering_bbox() const
{
return m_split_triangles_rendering_bbox;
}

private:
filepath_t m_modelPath;
uint32_t m_modelLoadFlags = 0;
float m_split_triangles_rendering_bbox = .0f;
};

} // namespace mrpt::viz
Loading

0 comments on commit 836ce33

Please sign in to comment.