Main MRPT website > C++ reference for 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 }
mrpt::opengl::TOpenGLFont
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:25
mrpt::opengl::T2DTextData::x
double x
Definition: opengl_fonts.h:98
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
glPopMatrix
GLAPI void GLAPIENTRY glPopMatrix(void)
mrpt::opengl::TOpenGLFontStyle
TOpenGLFontStyle
Different style for vectorized font rendering.
Definition: opengl_fonts.h:36
GL_MATRIX_MODE
#define GL_MATRIX_MODE
Definition: glew.h:415
mrpt::opengl::gl_utils::glDrawText
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
CTextMessageCapable.h
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_10
@ MRPT_GLUT_BITMAP_HELVETICA_10
Definition: opengl_fonts.h:30
mrpt::opengl::MRPT_GLUT_BITMAP_NONE
@ MRPT_GLUT_BITMAP_NONE
Definition: opengl_fonts.h:27
mrpt::opengl::gl_utils::glSetFont
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gl_utils.cpp:514
glDisable
GLAPI void GLAPIENTRY glDisable(GLenum cap)
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
glEnable
GLAPI void GLAPIENTRY glEnable(GLenum cap)
glTranslatef
GLAPI void GLAPIENTRY glTranslatef(GLfloat x, GLfloat y, GLfloat z)
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::opengl::TFontParams::color
mrpt::img::TColorf color
Definition: opengl_fonts.h:62
w
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
mrpt::opengl::TFontParams::vfont_style
TOpenGLFontStyle vfont_style
(default: NICE) See TOpenGLFontStyle.
Definition: opengl_fonts.h:80
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_12
@ MRPT_GLUT_BITMAP_HELVETICA_12
Definition: opengl_fonts.h:31
glColor3f
GLAPI void GLAPIENTRY glColor3f(GLfloat red, GLfloat green, GLfloat blue)
mrpt::opengl::T2DTextData::text
std::string text
Definition: opengl_fonts.h:97
glPushMatrix
GLAPI void GLAPIENTRY glPushMatrix(void)
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_18
@ MRPT_GLUT_BITMAP_HELVETICA_18
Definition: opengl_fonts.h:32
mrpt::opengl::TFontParams::shadow_color
mrpt::img::TColorf shadow_color
Definition: opengl_fonts.h:65
mrpt::opengl::TFontParams::vfont_scale
double vfont_scale
Size of characters.
Definition: opengl_fonts.h:78
mrpt::opengl::T2DTextData
An auxiliary struct for holding a list of text messages in some mrpt::opengl & mrpt::gui classes The ...
Definition: opengl_fonts.h:94
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
mrpt::opengl::TFontParams::vfont_spacing
double vfont_spacing
(default: 1.5) Refer to mrpt::opengl::gl_utils::glDrawText
Definition: opengl_fonts.h:82
color
GLuint color
Definition: glext.h:8300
mrpt::opengl::TFontParams::vfont_kerning
double vfont_kerning
(default: 0.1) Refer to mrpt::opengl::gl_utils::glDrawText
Definition: opengl_fonts.h:84
GL_DEPTH_TEST
#define GL_DEPTH_TEST
Definition: glew.h:401
glOrtho
GLAPI void GLAPIENTRY glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
GL_MODELVIEW
#define GL_MODELVIEW
Definition: glew.h:610
mrpt::opengl::FILL
@ FILL
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
glGetIntegerv
GLAPI void GLAPIENTRY glGetIntegerv(GLenum pname, GLint *params)
gl_utils.h
glLoadIdentity
GLAPI void GLAPIENTRY glLoadIdentity(void)
opengl-precomp.h
mrpt::opengl::T2DTextData::y
double y
Definition: opengl_fonts.h:98
mrpt::opengl::TFontParams::font
mrpt::opengl::TOpenGLFont font
Definition: opengl_fonts.h:69
mrpt::opengl::TFontParams::draw_shadow
bool draw_shadow
Definition: opengl_fonts.h:64
mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_10
@ MRPT_GLUT_BITMAP_TIMES_ROMAN_10
Definition: opengl_fonts.h:28
opengl_internals.h
glMatrixMode
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
mrpt::opengl::TFontParams::vfont_name
std::string vfont_name
Vectorized font name ("sans","mono","serif")
Definition: opengl_fonts.h:76
string
GLsizei const GLchar ** string
Definition: glext.h:4101
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
GLint
int GLint
Definition: glew.h:209
GL_PROJECTION
#define GL_PROJECTION
Definition: glew.h:611
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_24
@ MRPT_GLUT_BITMAP_TIMES_ROMAN_24
Definition: opengl_fonts.h:29
x
GLenum GLint x
Definition: glext.h:3538



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST