Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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 
22 /** Renders the messages to the current opengl rendering context (to be called 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 (std::map<size_t,mrpt::opengl::T2DTextData>::const_iterator it=m_2D_texts.begin();it!=m_2D_texts.end();++it)
43  {
44  // If (x,y) \in [0,1[, it's interpreted as a ratio, otherwise, as an actual coordinate in pixels
45  const int x = it->second.x>=1 ? int(it->second.x) : (it->second.x<0 ? int(w+it->second.x) : int(it->second.x * w));
46  const int y = it->second.y>=1 ? int(it->second.y) : (it->second.y<0 ? int(h+it->second.y) : int(it->second.y * h));
47 
48  // Font size and family:
49  double font_size=10;
50  string font_name="sans";
52  double font_spacing = 1.5;
53  double font_kerning = 0.1;
54 
55  switch(it->second.font)
56  {
57  case MRPT_GLUT_BITMAP_TIMES_ROMAN_10: font_size=10; font_name="sans"; break;
58  case MRPT_GLUT_BITMAP_TIMES_ROMAN_24: font_size=24; font_name="sans"; break;
59  case MRPT_GLUT_BITMAP_HELVETICA_10: font_size=10; font_name="mono"; break;
60  case MRPT_GLUT_BITMAP_HELVETICA_12: font_size=12; font_name="mono"; break;
61  case MRPT_GLUT_BITMAP_HELVETICA_18: font_size=18; font_name="mono"; break;
62 
63  // This means this is a vectorized font, so just copy the parameters set by the user:
65  font_size = it->second.vfont_scale;
66  font_name = it->second.vfont_name;
67  font_style = it->second.vfont_style;
68  font_spacing = it->second.vfont_spacing;
69  font_kerning = it->second.vfont_kerning;
70  break;
71 
72  default:
73  std::cerr << "[CTextMessageCapable::render_text_messages] Invalid value for TOpenGLFont\n";
74  break;
75  };
76 
77  if (it->second.draw_shadow)
78  {
79  // Draw shadow:
80  glPushMatrix();
81 
82  glTranslatef(x+1, y-1, 0.0);
83  glColor3f(it->second.shadow_color.R,it->second.shadow_color.G,it->second.shadow_color.B);
85  mrpt::opengl::gl_utils::glDrawText(it->second.text, font_size, font_style, font_spacing, font_kerning );
86 
87  glPopMatrix();
88  }
89 
90  // Draw text:
91  glPushMatrix();
92 
93  glTranslatef(x, y, 0.0);
94  glColor3f(it->second.color.R,it->second.color.G,it->second.color.B);
96  mrpt::opengl::gl_utils::glDrawText(it->second.text, font_size, font_style, font_spacing, font_kerning );
97 
98  glPopMatrix();
99  }
100 
102 
104  glPopMatrix();
105 
106  if (old_matMode!=GL_PROJECTION)
107  glMatrixMode(old_matMode);
108 
109 #else
111 #endif
112 }
113 
114 void CTextMessageCapable::clearTextMessages()
115 {
116  m_2D_texts.clear();
117 }
118 
119 
120 void CTextMessageCapable::addTextMessage(
121  const double x_frac,
122  const double y_frac,
123  const std::string &text,
125  const size_t unique_index ,
126  const mrpt::opengl::TOpenGLFont font
127  )
128 {
130  d.text = text;
131  d.color = color;
132  d.x = x_frac;
133  d.y = y_frac;
134  d.font = font;
135 
136  m_2D_texts[unique_index] = d;
137 }
138 
139 /** Just updates the text of a given text message, without touching the other parameters.
140  * \return false if given ID doesn't exist.
141  */
142 bool CTextMessageCapable::updateTextMessage(const size_t unique_index, const std::string &text)
143 {
144  std::map<size_t,mrpt::opengl::T2DTextData>::iterator it = m_2D_texts.find(unique_index);
145  if (it == m_2D_texts.end())
146  return false;
147  else
148  {
149  it->second.text = text;
150  return true;
151  }
152 }
153 
154 
155 /// overload with more font parameters - refer to mrpt::opengl::gl_utils::glDrawText()
156 void CTextMessageCapable::addTextMessage(
157  const double x_frac,
158  const double y_frac,
159  const std::string &text,
161  const std::string &font_name,
162  const double font_size,
163  const mrpt::opengl::TOpenGLFontStyle font_style,
164  const size_t unique_index,
165  const double font_spacing,
166  const double font_kerning,
167  const bool has_shadow,
168  const mrpt::utils::TColorf &shadow_color
169  )
170 {
172  d.text = text;
173  d.color = color;
174  d.draw_shadow = has_shadow;
175  d.shadow_color = shadow_color;
176  d.x = x_frac;
177  d.y = y_frac;
178  d.font = MRPT_GLUT_BITMAP_NONE; // It's not a bitmapped font
179  d.vfont_name = font_name;
180  d.vfont_scale = font_size;
181  d.vfont_style = font_style;
182  d.vfont_spacing = font_spacing;
183  d.vfont_kerning = font_kerning;
184 
185  m_2D_texts[unique_index] = d;
186 }
mrpt::utils::TPixelCoordf OPENGL_IMPEXP 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:600
mrpt::opengl::TOpenGLFont font
Definition: opengl_fonts.h:68
mrpt::utils::TColorf shadow_color
Definition: opengl_fonts.h:64
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define GL_MODELVIEW
Definition: glew.h:606
GLAPI void GLAPIENTRY glPopMatrix(void)
Scalar * iterator
Definition: eigen_plugins.h:23
#define GL_MATRIX_MODE
Definition: glew.h:411
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
#define GL_DEPTH_TEST
Definition: glew.h:397
void OPENGL_IMPEXP glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gl_utils.cpp:583
GLAPI void GLAPIENTRY glLoadIdentity(void)
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3962
std::string vfont_name
Vectorized font name ("sans","mono","serif")
Definition: opengl_fonts.h:73
GLuint color
Definition: glext.h:7093
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:26
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
TOpenGLFontStyle
Different style for vectorized font rendering.
Definition: opengl_fonts.h:37
double vfont_kerning
(default: 0.1) Refer to mrpt::opengl::gl_utils::glDrawText
Definition: opengl_fonts.h:77
#define GL_PROJECTION
Definition: glew.h:607
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:86
GLsizei const GLchar ** string
Definition: glext.h:3919
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::utils::TColorf color
Definition: opengl_fonts.h:61
double vfont_spacing
(default: 1.5) Refer to mrpt::opengl::gl_utils::glDrawText
Definition: opengl_fonts.h:76
GLAPI void GLAPIENTRY glGetIntegerv(GLenum pname, GLint *params)
The namespace for 3D scene representation and rendering.
A RGB color - floats in the range [0,1].
Definition: TColor.h:80
GLenum GLint GLint y
Definition: glext.h:3516
int GLint
Definition: glew.h:205
GLAPI void GLAPIENTRY glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
GLenum GLint x
Definition: glext.h:3516
GLAPI void GLAPIENTRY glPushMatrix(void)
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
GLAPI void GLAPIENTRY glDisable(GLenum cap)
double vfont_scale
Size of characters.
Definition: opengl_fonts.h:74
TOpenGLFontStyle vfont_style
(default: NICE) See TOpenGLFontStyle.
Definition: opengl_fonts.h:75



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019