| 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/CText.h> |
| 14 |
#include <mrpt/opengl/opengl_api.h> |
| 15 |
#include <mrpt/serialization/CArchive.h> |
| 16 |
|
| 17 |
#include "gltext.h" |
| 18 |
|
| 19 |
using namespace mrpt; |
| 20 |
using namespace mrpt::opengl; |
| 21 |
using namespace mrpt::math; |
| 22 |
using namespace std; |
| 23 |
|
| 24 |
IMPLEMENTS_SERIALIZABLE(CText, CRenderizable, mrpt::opengl) |
| 25 |
|
| 26 |
CText::~CText() = default; |
| 27 |
|
| 28 |
constexpr double text_spacing = 1.5; |
| 29 |
constexpr double text_kerning = 0.1; |
| 30 |
|
| 31 |
void CText::onUpdateBuffers_Text() |
| 32 |
{ |
| 33 |
std::unique_lock<std::shared_mutex> writeLock(m_textDataMtx.data); |
| 34 |
|
| 35 |
auto& vbd = CRenderizableShaderText::m_vertex_buffer_data; |
| 36 |
auto& tris = CRenderizableShaderText::m_triangles; |
| 37 |
auto& cbd = CRenderizableShaderText::m_color_buffer_data; |
| 38 |
vbd.clear(); |
| 39 |
tris.clear(); |
| 40 |
cbd.clear(); |
| 41 |
|
| 42 |
mrpt::opengl::internal::glSetFont(m_fontName); |
| 43 |
mrpt::opengl::internal::glDrawText( |
| 44 |
m_str, tris, vbd, mrpt::opengl::FILL, text_spacing, text_kerning); |
| 45 |
|
| 46 |
// All lines & triangles, the same color: |
| 47 |
cbd.assign(vbd.size(), getColor_u8()); |
| 48 |
for (auto& tri : m_triangles) tri.setColor(getColor_u8()); |
| 49 |
} |
| 50 |
|
| 51 |
std::pair<double, double> CText::computeTextExtension() const |
| 52 |
{ |
| 53 |
mrpt::opengl::internal::glSetFont(m_fontName); |
| 54 |
const auto [textW, textH] = |
| 55 |
mrpt::opengl::internal::glGetExtends(m_str, text_spacing, text_kerning); |
| 56 |
return {textW, textH}; |
| 57 |
} |
| 58 |
|
| 59 |
void CText::render(const RenderContext& rc) const |
| 60 |
{ |
| 61 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 62 |
|
| 63 |
// Compute pixel of (0,0,0) for this object: |
| 64 |
// "px" will be given in the range: |
| 65 |
// [-1,-1] (left-bottom) - [+1,+1] (top-right) |
| 66 |
// |
| 67 |
const auto& pmv = rc.state->pmv_matrix; |
| 68 |
if (std::abs(pmv(3, 3)) < 1e-10) return; |
| 69 |
|
| 70 |
const auto px = mrpt::math::TPoint2Df(pmv(0, 3) / pmv(3, 3), pmv(1, 3) / pmv(3, 3)); |
| 71 |
|
| 72 |
// Load matrices in shader: |
| 73 |
const GLint u_pmat = rc.shader->uniformId("p_matrix"); |
| 74 |
const GLint u_mvmat = rc.shader->uniformId("mv_matrix"); |
| 75 |
|
| 76 |
// Projection: identity |
| 77 |
static const auto eye4 = mrpt::math::CMatrixFloat44::Identity(); |
| 78 |
glUniformMatrix4fv(u_pmat, 1, true, eye4.data()); |
| 79 |
|
| 80 |
// Model-view: translate and scale |
| 81 |
auto mv = mrpt::math::CMatrixFloat44::Identity(); |
| 82 |
mv(0, 3) = px.x; |
| 83 |
mv(1, 3) = px.y; |
| 84 |
mv(2, 3) = pmv(2, 3) / pmv(3, 3); // depth |
| 85 |
|
| 86 |
if (rc.state->viewport_height <= 0 || rc.state->viewport_width <= 0) |
| 87 |
{ |
| 88 |
std::cerr << "[CText] Warning: invalid viewport size!\n"; |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Find scale according to font height in pixels: |
| 93 |
const float scale = this->m_fontHeight / static_cast<float>(rc.state->viewport_height); |
| 94 |
|
| 95 |
const float aspect = rc.state->viewport_width / double(rc.state->viewport_height); |
| 96 |
mv(0, 0) *= scale / aspect; |
| 97 |
mv(1, 1) *= scale; |
| 98 |
|
| 99 |
glUniformMatrix4fv(u_mvmat, 1, true, mv.data()); |
| 100 |
|
| 101 |
CRenderizableShaderText::render(rc); |
| 102 |
|
| 103 |
#endif |
| 104 |
} |
| 105 |
|
| 106 |
uint8_t CText::serializeGetVersion() const { return 2; } |
| 107 |
void CText::serializeTo(mrpt::serialization::CArchive& out) const |
| 108 |
{ |
| 109 |
writeToStreamRender(out); |
| 110 |
out << m_str; |
| 111 |
out << m_fontName; |
| 112 |
out << (uint32_t)m_fontHeight; |
| 113 |
} |
| 114 |
|
| 115 |
void CText::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 116 |
{ |
| 117 |
switch (version) |
| 118 |
{ |
| 119 |
case 0: |
| 120 |
case 1: |
| 121 |
case 2: |
| 122 |
{ |
| 123 |
uint32_t i; |
| 124 |
readFromStreamRender(in); |
| 125 |
in >> m_str; |
| 126 |
if (version >= 1) |
| 127 |
{ |
| 128 |
in >> m_fontName; |
| 129 |
in >> i; |
| 130 |
m_fontHeight = i; |
| 131 |
|
| 132 |
if (version < 2) |
| 133 |
{ |
| 134 |
in >> i; |
| 135 |
// dummy, removed in v2: m_fontWidth = i; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
break; |
| 140 |
default: |
| 141 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 142 |
}; |
| 143 |
} |
| 144 |
|
| 145 |
auto CText::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 146 |
{ |
| 147 |
return { |
| 148 |
{0.f, 0.f, 0.f}, |
| 149 |
{0.f, 0.f, 0.f} |
| 150 |
}; |
| 151 |
} |
| 152 |
|
| 153 |
void CText::toYAMLMap(mrpt::containers::yaml& propertiesMap) const |
| 154 |
{ |
| 155 |
CRenderizable::toYAMLMap(propertiesMap); |
| 156 |
propertiesMap["text"] = m_str; |
| 157 |
} |