| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "opengl-precomp.h" // Precompiled header |
| 11 |
// |
| 12 |
#include <mrpt/containers/yaml.h> |
| 13 |
#include <mrpt/opengl/CText3D.h> |
| 14 |
#include <mrpt/serialization/CArchive.h> |
| 15 |
|
| 16 |
#include "gltext.h" |
| 17 |
|
| 18 |
using namespace mrpt; |
| 19 |
using namespace mrpt::opengl; |
| 20 |
using namespace std; |
| 21 |
|
| 22 |
IMPLEMENTS_SERIALIZABLE(CText3D, CRenderizable, mrpt::opengl) |
| 23 |
|
| 24 |
CText3D::CText3D( |
| 25 |
const std::string& str, |
| 26 |
const std::string& fontName, |
| 27 |
const float scale, |
| 28 |
const mrpt::opengl::TOpenGLFontStyle text_style, |
| 29 |
const double text_spacing, |
| 30 |
const double text_kerning) : |
| 31 |
m_str(str), |
| 32 |
m_fontName(fontName), |
| 33 |
m_text_style(text_style), |
| 34 |
m_text_spacing(text_spacing), |
| 35 |
m_text_kerning(text_kerning) |
| 36 |
{ |
| 37 |
this->setScale(scale); |
| 38 |
} |
| 39 |
|
| 40 |
CText3D::~CText3D() = default; |
| 41 |
|
| 42 |
void CText3D::onUpdateBuffers_Text() |
| 43 |
{ |
| 44 |
std::unique_lock<std::shared_mutex> writeLock(m_textDataMtx.data); |
| 45 |
|
| 46 |
auto& vbd = CRenderizableShaderText::m_vertex_buffer_data; |
| 47 |
auto& tris = CRenderizableShaderText::m_triangles; |
| 48 |
auto& cbd = CRenderizableShaderText::m_color_buffer_data; |
| 49 |
vbd.clear(); |
| 50 |
tris.clear(); |
| 51 |
cbd.clear(); |
| 52 |
|
| 53 |
mrpt::opengl::internal::glSetFont(m_fontName); |
| 54 |
mrpt::opengl::internal::glDrawText( |
| 55 |
m_str, tris, vbd, m_text_style, m_text_spacing, m_text_kerning); |
| 56 |
|
| 57 |
// All lines & triangles, the same color: |
| 58 |
cbd.assign(vbd.size(), getColor_u8()); |
| 59 |
for (auto& tri : m_triangles) tri.setColor(getColor_u8()); |
| 60 |
} |
| 61 |
|
| 62 |
uint8_t CText3D::serializeGetVersion() const { return 0; } |
| 63 |
void CText3D::serializeTo(mrpt::serialization::CArchive& out) const |
| 64 |
{ |
| 65 |
writeToStreamRender(out); |
| 66 |
out << m_str << m_fontName << (uint32_t)m_text_style << m_text_spacing << m_text_kerning; |
| 67 |
} |
| 68 |
|
| 69 |
void CText3D::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 70 |
{ |
| 71 |
switch (version) |
| 72 |
{ |
| 73 |
case 0: |
| 74 |
{ |
| 75 |
readFromStreamRender(in); |
| 76 |
|
| 77 |
uint32_t i; |
| 78 |
in >> m_str >> m_fontName >> i >> m_text_spacing >> m_text_kerning; |
| 79 |
|
| 80 |
m_text_style = TOpenGLFontStyle(i); |
| 81 |
} |
| 82 |
break; |
| 83 |
default: |
| 84 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 85 |
}; |
| 86 |
CRenderizable::notifyChange(); |
| 87 |
} |
| 88 |
|
| 89 |
auto CText3D::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 90 |
{ |
| 91 |
return mrpt::math::TBoundingBoxf::FromUnsortedPoints( |
| 92 |
{0.f, 0.f, 0.f}, {m_str.size() * getScaleX(), 1.0f * getScaleY(), 0.f}); |
| 93 |
} |
| 94 |
|
| 95 |
void CText3D::toYAMLMap(mrpt::containers::yaml& propertiesMap) const |
| 96 |
{ |
| 97 |
CRenderizable::toYAMLMap(propertiesMap); |
| 98 |
propertiesMap["text"] = m_str; |
| 99 |
} |