MRPT  1.9.9
CTextMessageCapable.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://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 
43  m_2D_texts.begin();
44  it != m_2D_texts.end(); ++it)
45  {
46  // If (x,y) \in [0,1[, it's interpreted as a ratio, otherwise, as an
47  // actual coordinate in pixels
48  const int x = it->second.x >= 1
49  ? int(it->second.x)
50  : (it->second.x < 0 ? int(w + it->second.x)
51  : int(it->second.x * w));
52  const int y = it->second.y >= 1
53  ? int(it->second.y)
54  : (it->second.y < 0 ? int(h + it->second.y)
55  : int(it->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 (it->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 = it->second.vfont_scale;
91  font_name = it->second.vfont_name;
92  font_style = it->second.vfont_style;
93  font_spacing = it->second.vfont_spacing;
94  font_kerning = it->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 (it->second.draw_shadow)
104  {
105  // Draw shadow:
106  glPushMatrix();
107 
108  glTranslatef(x + 1, y - 1, 0.0);
109  glColor3f(
110  it->second.shadow_color.R, it->second.shadow_color.G,
111  it->second.shadow_color.B);
114  it->second.text, font_size, font_style, font_spacing,
115  font_kerning);
116 
117  glPopMatrix();
118  }
119 
120  // Draw text:
121  glPushMatrix();
122 
123  glTranslatef(x, y, 0.0);
124  glColor3f(it->second.color.R, it->second.color.G, it->second.color.B);
127  it->second.text, font_size, font_style, font_spacing, font_kerning);
128 
129  glPopMatrix();
130  }
131 
133 
135  glPopMatrix();
136 
137  if (old_matMode != GL_PROJECTION) glMatrixMode(old_matMode);
138 
139 #else
142 #endif
143 }
144 
145 void CTextMessageCapable::clearTextMessages() { m_2D_texts.clear(); }
146 void CTextMessageCapable::addTextMessage(
147  const double x_frac, const double y_frac, const std::string& text,
148  const mrpt::img::TColorf& color, const size_t unique_index,
149  const mrpt::opengl::TOpenGLFont font)
150 {
152  d.text = text;
153  d.color = color;
154  d.x = x_frac;
155  d.y = y_frac;
156  d.font = font;
157 
158  m_2D_texts[unique_index] = d;
159 }
160 
161 /** Just updates the text of a given text message, without touching the other
162  * parameters.
163  * \return false if given ID doesn't exist.
164  */
165 bool CTextMessageCapable::updateTextMessage(
166  const size_t unique_index, const std::string& text)
167 {
169  m_2D_texts.find(unique_index);
170  if (it == m_2D_texts.end())
171  return false;
172  else
173  {
174  it->second.text = text;
175  return true;
176  }
177 }
178 
179 /// overload with more font parameters - refer to
180 /// mrpt::opengl::gl_utils::glDrawText()
181 void CTextMessageCapable::addTextMessage(
182  const double x_frac, const double y_frac, const std::string& text,
183  const mrpt::img::TColorf& color, const std::string& font_name,
184  const double font_size, const mrpt::opengl::TOpenGLFontStyle font_style,
185  const size_t unique_index, const double font_spacing,
186  const double font_kerning, const bool has_shadow,
187  const mrpt::img::TColorf& shadow_color)
188 {
190  d.text = text;
191  d.color = color;
192  d.draw_shadow = has_shadow;
193  d.shadow_color = shadow_color;
194  d.x = x_frac;
195  d.y = y_frac;
196  d.font = MRPT_GLUT_BITMAP_NONE; // It's not a bitmapped font
197  d.vfont_name = font_name;
198  d.vfont_scale = font_size;
199  d.vfont_style = font_style;
200  d.vfont_spacing = font_spacing;
201  d.vfont_kerning = font_kerning;
202 
203  m_2D_texts[unique_index] = d;
204 }
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::opengl::TOpenGLFont font
Definition: opengl_fonts.h:67
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define GL_MODELVIEW
Definition: glew.h:610
GLAPI void GLAPIENTRY glPopMatrix(void)
#define GL_MATRIX_MODE
Definition: glew.h:415
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:532
#define GL_DEPTH_TEST
Definition: glew.h:401
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gl_utils.cpp:514
GLAPI void GLAPIENTRY glLoadIdentity(void)
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
std::string vfont_name
Vectorized font name ("sans","mono","serif")
Definition: opengl_fonts.h:74
GLuint color
Definition: glext.h:8300
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:23
TOpenGLFontStyle
Different style for vectorized font rendering.
Definition: opengl_fonts.h:34
double vfont_kerning
(default: 0.1) Refer to mrpt::opengl::gl_utils::glDrawText
Definition: opengl_fonts.h:82
#define GL_PROJECTION
Definition: glew.h:611
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:92
GLsizei const GLchar ** string
Definition: glext.h:4101
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:80
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:3538
mrpt::img::TColorf color
Definition: opengl_fonts.h:60
int GLint
Definition: glew.h:209
GLAPI void GLAPIENTRY glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
GLenum GLint x
Definition: glext.h:3538
GLAPI void GLAPIENTRY glPushMatrix(void)
renders glyphs as filled polygons
Definition: opengl_fonts.h:36
GLAPI void GLAPIENTRY glDisable(GLenum cap)
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::img::TColorf shadow_color
Definition: opengl_fonts.h:63
double vfont_scale
Size of characters.
Definition: opengl_fonts.h:76
#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:78



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020