MRPT  1.9.9
CTextMessageCapable.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-2019, 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 
13 #include <mrpt/opengl/gl_utils.h>
14 
15 #include "opengl_internals.h"
16 
17 using namespace std;
18 using namespace mrpt;
19 using namespace mrpt::opengl;
20 
21 /** Renders the messages to the current opengl rendering context (to be called
22  * OUT of MRPT mrpt::opengl render() methods ).
23  * (w,h) are the dimensions of the rendering area in pixels.
24  */
25 void CTextMessageCapable::render_text_messages(const int w, const int h) const
26 {
27 #if MRPT_HAS_OPENGL_GLUT
28  // Render text labels as opengl primitives (much faster):
29  GLint old_matMode = 0;
30  glGetIntegerv(GL_MATRIX_MODE, &old_matMode);
31 
33  glPushMatrix();
34 
36  glOrtho(0, w, 0, h, -1, 1);
37 
39 
41 
42  for (const auto& m_2D_text : m_2D_texts)
43  {
44  // If (x,y) \in [0,1[, it's interpreted as a ratio, otherwise, as an
45  // actual coordinate in pixels
46  const int x =
47  m_2D_text.second.x >= 1
48  ? int(m_2D_text.second.x)
49  : (m_2D_text.second.x < 0 ? int(w + m_2D_text.second.x)
50  : int(m_2D_text.second.x * w));
51  const int y =
52  m_2D_text.second.y >= 1
53  ? int(m_2D_text.second.y)
54  : (m_2D_text.second.y < 0 ? int(h + m_2D_text.second.y)
55  : int(m_2D_text.second.y * h));
56 
57  // Font size and family:
58  double font_size = 10;
59  string font_name = "sans";
61  double font_spacing = 1.5;
62  double font_kerning = 0.1;
63 
64  switch (m_2D_text.second.font)
65  {
67  font_size = 10;
68  font_name = "sans";
69  break;
71  font_size = 24;
72  font_name = "sans";
73  break;
75  font_size = 10;
76  font_name = "mono";
77  break;
79  font_size = 12;
80  font_name = "mono";
81  break;
83  font_size = 18;
84  font_name = "mono";
85  break;
86 
87  // This means this is a vectorized font, so just copy the parameters
88  // set by the user:
90  font_size = m_2D_text.second.vfont_scale;
91  font_name = m_2D_text.second.vfont_name;
92  font_style = m_2D_text.second.vfont_style;
93  font_spacing = m_2D_text.second.vfont_spacing;
94  font_kerning = m_2D_text.second.vfont_kerning;
95  break;
96 
97  default:
98  std::cerr << "[CTextMessageCapable::render_text_messages] "
99  "Invalid value for TOpenGLFont\n";
100  break;
101  };
102 
103  if (m_2D_text.second.draw_shadow)
104  {
105  // Draw shadow:
106  glPushMatrix();
107 
108  glTranslatef(x + 1, y - 1, 0.0);
109  glColor3f(
110  m_2D_text.second.shadow_color.R,
111  m_2D_text.second.shadow_color.G,
112  m_2D_text.second.shadow_color.B);
115  m_2D_text.second.text, font_size, font_style, font_spacing,
116  font_kerning);
117 
118  glPopMatrix();
119  }
120 
121  // Draw text:
122  glPushMatrix();
123 
124  glTranslatef(x, y, 0.0);
125  glColor3f(
126  m_2D_text.second.color.R, m_2D_text.second.color.G,
127  m_2D_text.second.color.B);
130  m_2D_text.second.text, font_size, font_style, font_spacing,
131  font_kerning);
132 
133  glPopMatrix();
134  }
135 
137 
139  glPopMatrix();
140 
141  if (old_matMode != GL_PROJECTION) glMatrixMode(old_matMode);
142 
143 #else
146 #endif
147 }
148 
149 void CTextMessageCapable::clearTextMessages() { m_2D_texts.clear(); }
150 void CTextMessageCapable::addTextMessage(
151  const double x_frac, const double y_frac, const std::string& text,
152  const mrpt::img::TColorf& color, const size_t unique_index,
153  const mrpt::opengl::TOpenGLFont font)
154 {
156  d.text = text;
157  d.color = color;
158  d.x = x_frac;
159  d.y = y_frac;
160  d.font = font;
161 
162  m_2D_texts[unique_index] = d;
163 }
164 
165 /** Just updates the text of a given text message, without touching the other
166  * parameters.
167  * \return false if given ID doesn't exist.
168  */
169 bool CTextMessageCapable::updateTextMessage(
170  const size_t unique_index, const std::string& text)
171 {
172  auto it = m_2D_texts.find(unique_index);
173  if (it == m_2D_texts.end())
174  return false;
175  else
176  {
177  it->second.text = text;
178  return true;
179  }
180 }
181 
182 /// overload with more font parameters - refer to
183 /// mrpt::opengl::gl_utils::glDrawText()
184 void CTextMessageCapable::addTextMessage(
185  const double x_frac, const double y_frac, const std::string& text,
186  const mrpt::img::TColorf& color, const std::string& font_name,
187  const double font_size, const mrpt::opengl::TOpenGLFontStyle font_style,
188  const size_t unique_index, const double font_spacing,
189  const double font_kerning, const bool has_shadow,
190  const mrpt::img::TColorf& shadow_color)
191 {
193  d.text = text;
194  d.color = color;
195  d.draw_shadow = has_shadow;
196  d.shadow_color = shadow_color;
197  d.x = x_frac;
198  d.y = y_frac;
199  d.font = MRPT_GLUT_BITMAP_NONE; // It's not a bitmapped font
200  d.vfont_name = font_name;
201  d.vfont_scale = font_size;
202  d.vfont_style = font_style;
203  d.vfont_spacing = font_spacing;
204  d.vfont_kerning = font_kerning;
205 
206  m_2D_texts[unique_index] = d;
207 }
mrpt::opengl::TOpenGLFont font
Definition: opengl_fonts.h:56
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define GL_MODELVIEW
Definition: glew.h:611
GLAPI void GLAPIENTRY glPopMatrix(void)
#define GL_MATRIX_MODE
Definition: glew.h:416
STL namespace.
mrpt::img::TPixelCoordf glDrawText(const std::string &text, const double textScale, enum TOpenGLFontStyle style=NICE, double spacing=1.5, double kerning=0.1)
renders a string in GL using the current settings.
Definition: gl_utils.cpp:545
#define GL_DEPTH_TEST
Definition: glew.h:402
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gl_utils.cpp:527
GLAPI void GLAPIENTRY glLoadIdentity(void)
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4199
std::string vfont_name
Vectorized font name ("sans","mono","serif")
Definition: opengl_fonts.h:63
GLuint color
Definition: glext.h:8459
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:22
TOpenGLFontStyle
Different style for vectorized font rendering.
Definition: opengl_fonts.h:33
double vfont_kerning
(default: 0.1) Refer to mrpt::opengl::gl_utils::glDrawText
Definition: opengl_fonts.h:71
#define GL_PROJECTION
Definition: glew.h:612
GLAPI void GLAPIENTRY glColor3f(GLfloat red, GLfloat green, GLfloat blue)
An auxiliary struct for holding a list of text messages in some mrpt::opengl & mrpt::gui classes The ...
Definition: opengl_fonts.h:81
GLsizei const GLchar ** string
Definition: glext.h:4116
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double vfont_spacing
(default: 1.5) Refer to mrpt::opengl::gl_utils::glDrawText
Definition: opengl_fonts.h:69
GLAPI void GLAPIENTRY glGetIntegerv(GLenum pname, GLint *params)
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLenum GLint GLint y
Definition: glext.h:3542
mrpt::img::TColorf color
Definition: opengl_fonts.h:49
int GLint
Definition: glew.h:210
GLAPI void GLAPIENTRY glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
GLenum GLint x
Definition: glext.h:3542
GLAPI void GLAPIENTRY glPushMatrix(void)
renders glyphs as filled polygons
Definition: opengl_fonts.h:35
GLAPI void GLAPIENTRY glDisable(GLenum cap)
mrpt::img::TColorf shadow_color
Definition: opengl_fonts.h:52
double vfont_scale
Size of characters.
Definition: opengl_fonts.h:65
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
TOpenGLFontStyle vfont_style
(default: NICE) See TOpenGLFontStyle.
Definition: opengl_fonts.h:67



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019