MRPT  2.0.5
CText.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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/opengl/CText.h>
14 
15 #include <mrpt/opengl/opengl_api.h>
16 #include "gltext.h"
17 
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 using namespace mrpt::math;
21 using namespace std;
22 
24 
25 CText::~CText() = default;
26 
28 {
29  const double text_spacing = 1.5;
30  const double text_kerning = 0.1;
31 
35  vbd.clear();
36  tris.clear();
37  cbd.clear();
38 
41  m_str, tris, vbd, mrpt::opengl::FILL, text_spacing, text_kerning);
42 
43  // All lines & triangles, the same color:
44  cbd.assign(vbd.size(), m_color);
45  for (auto& tri : m_triangles) tri.setColor(m_color);
46 }
47 
48 void CText::render(const RenderContext& rc) const
49 {
50 #if MRPT_HAS_OPENGL_GLUT
51 
52  // Compute pixel of (0,0,0) for this object:
53  // "px" will be given in the range:
54  // [-1,-1] (left-bottom) - [+1,+1] (top-right)
55  //
56  const auto& pmv = rc.state->pmv_matrix;
57  if (std::abs(pmv(3, 3)) < 1e-10) return;
58 
59  const auto px =
60  mrpt::math::TPoint2Df(pmv(0, 3) / pmv(3, 3), pmv(1, 3) / pmv(3, 3));
61 
62  // Load matrices in shader:
63  const GLint u_pmat = rc.shader->uniformId("p_matrix");
64  const GLint u_mvmat = rc.shader->uniformId("mv_matrix");
65 
66  // Projection: identity
67  static const auto eye4 = mrpt::math::CMatrixFloat44::Identity();
68  glUniformMatrix4fv(u_pmat, 1, true, eye4.data());
69 
70  // Model-view: translate and scale
72  mv(0, 3) = px.x;
73  mv(1, 3) = px.y;
74  mv(2, 3) = pmv(2, 3) / pmv(3, 3); // depth
75 
76  if (rc.state->viewport_height <= 0 || rc.state->viewport_width <= 0)
77  {
78  std::cerr << "[CText] Warning: invalid viewport size!\n";
79  return;
80  }
81 
82  // Find scale according to font height in pixels:
83  const float scale =
84  this->m_fontHeight / static_cast<float>(rc.state->viewport_height);
85 
86  const float aspect =
87  rc.state->viewport_width / double(rc.state->viewport_height);
88  mv(0, 0) *= scale / aspect;
89  mv(1, 1) *= scale;
90 
91  glUniformMatrix4fv(u_mvmat, 1, true, mv.data());
92 
94 
95 #endif
96 }
97 
98 uint8_t CText::serializeGetVersion() const { return 1; }
100 {
101  writeToStreamRender(out);
102  out << m_str;
103  out << m_fontName;
104  out << (uint32_t)m_fontHeight << (uint32_t)m_fontWidth;
105 }
106 
108 {
109  switch (version)
110  {
111  case 0:
112  case 1:
113  {
114  uint32_t i;
115  readFromStreamRender(in);
116  in >> m_str;
117  if (version >= 1)
118  {
119  in >> m_fontName;
120  in >> i;
121  m_fontHeight = i;
122  in >> i;
123  m_fontWidth = i;
124  }
125  }
126  break;
127  default:
129  };
130 }
131 
134 {
135  bb_min.x = 0;
136  bb_min.y = 0;
137  bb_min.z = 0;
138 
139  bb_max = bb_min;
140 
141  // Convert to coordinates of my parent:
142  m_pose.composePoint(bb_min, bb_min);
143  m_pose.composePoint(bb_max, bb_max);
144 }
A 2D text (bitmap rendering): it always "faces the observer" despite it&#39;s at some 3D location...
Definition: CText.h:35
const mrpt::opengl::Program * shader
virtual ~CText() override
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CText.cpp:98
const mrpt::opengl::TRenderMatrices * state
static Derived Identity()
Definition: MatrixBase.h:64
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CText.cpp:48
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
STL namespace.
Context for calls to render()
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
This base provides a set of functions for maths stuff.
mrpt::math::CMatrixFloat44 pmv_matrix
Result of p_matrix * mv_matrix.
void onUpdateBuffers_Text() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CText.cpp:27
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
std::pair< double, double > glDrawText(const std::string &text, std::vector< mrpt::opengl::TTriangle > &tris, std::vector< mrpt::math::TPoint3Df > &render_lines, TEXT_STYLE style, double spacing, double kerning)
renders a string in GL using the current settings.
Definition: gltext.cpp:153
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CText.cpp:99
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
List of lines.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
size_t viewport_width
In pixels.
mrpt::vision::TStereoCalibResults out
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gltext.cpp:146
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
const auto bb_max
std::vector< mrpt::opengl::TTriangle > m_triangles
List of triangles.
int uniformId(const char *name) const
Definition: Shader.h:113
const auto bb_min
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CText.cpp:107
renders glyphs as filled polygons
Definition: opengl_fonts.h:21
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...
Definition: CText.cpp:132
TPoint2D_< float > TPoint2Df
Definition: TPoint2D.h:214
std::vector< mrpt::img::TColor > m_color_buffer_data



Page generated by Doxygen 1.8.14 for MRPT 2.0.5 Git: 40e60e732 Thu Jul 9 08:38:35 2020 +0200 at jue jul 9 08:45:11 CEST 2020