Main MRPT website > C++ reference for MRPT 1.9.9
gl_utils.h
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 #pragma once
10 
13 #include <mrpt/img/TPixelCoord.h>
14 
15 namespace mrpt
16 {
17 namespace opengl
18 {
19 /** A set of auxiliary functions that can be called to render OpenGL primitives
20  * from MRPT or user code
21  * \ingroup mrpt_opengl_grp
22  */
23 namespace gl_utils
24 {
25 /** @name Data types for mrpt::opengl::gl_utils
26  @{ */
27 
28 /** Information about the rendering process being issued. \sa See
29  * getCurrentRenderingInfo for more details */
31 {
32  /** Rendering viewport geometry (in pixels) */
34  /** The 4x4 projection matrix */
35  Eigen::Matrix<float, 4, 4, Eigen::ColMajor> proj_matrix;
36  /** The 4x4 model transformation matrix */
37  Eigen::Matrix<float, 4, 4, Eigen::ColMajor> model_matrix;
38  /** PROJ * MODEL */
39  Eigen::Matrix<float, 4, 4, Eigen::ColMajor> full_matrix;
40  /** The 3D location of the camera */
42 
43  /** Computes the normalized coordinates (range=[0,1]) on the current
44  * rendering viewport of a
45  * point with local coordinates (wrt to the current model matrix) of
46  * (x,y,z).
47  * The output proj_z_depth is the real distance from the eye to the point.
48  */
50  float x, float y, float z, float& proj_x, float& proj_y,
51  float& proj_z_depth) const
52  {
53  const Eigen::Matrix<float, 4, 1, Eigen::ColMajor> proj =
54  full_matrix *
55  Eigen::Matrix<float, 4, 1, Eigen::ColMajor>(x, y, z, 1);
56  proj_x = proj[3] ? proj[0] / proj[3] : 0;
57  proj_y = proj[3] ? proj[1] / proj[3] : 0;
58  proj_z_depth = proj[2];
59  }
60 
61  /** Exactly like projectPoint but the (x,y) projected coordinates are given
62  * in pixels instead of normalized coordinates. */
64  float x, float y, float z, float& proj_x_px, float& proj_y_px,
65  float& proj_z_depth) const
66  {
67  projectPoint(x, y, z, proj_x_px, proj_y_px, proj_z_depth);
68  proj_x_px = (proj_x_px + 1.0f) * (vp_width / 2.0f);
69  proj_y_px = (proj_y_px + 1.0f) * (vp_height / 2.0f);
70  }
71 };
72 
73 /** @} */ // -----------------------------------------------------
74 
75 /** @name Miscellaneous rendering methods
76  @{ */
77 
78 /** For each object in the list:
79  * - checks visibility of each object
80  * - prepare the GL_MODELVIEW matrix according to its coordinates
81  * - call its ::render()
82  * - shows its name (if enabled).
83  *
84  * \note Used by COpenGLViewport, CSetOfObjects
85  */
87 
88 /** Checks glGetError and throws an exception if an error situation is found */
89 void checkOpenGLError();
90 
91 /** Can be used by derived classes to draw a triangle with a normal vector
92  * computed automatically - to be called within a glBegin()-glEnd() block. */
94  const mrpt::math::TPoint3D& p1, const mrpt::math::TPoint3D& p2,
95  const mrpt::math::TPoint3D& p3);
97  const mrpt::math::TPoint3Df& p1, const mrpt::math::TPoint3Df& p2,
98  const mrpt::math::TPoint3Df& p3);
99 
100 /** Can be used by derived classes to draw a quad with a normal vector computed
101  * automatically - to be called within a glBegin()-glEnd() block. */
103  const mrpt::math::TPoint3Df& p1, const mrpt::math::TPoint3Df& p2,
104  const mrpt::math::TPoint3Df& p3, const mrpt::math::TPoint3Df& p4);
105 
106 /** Gather useful information on the render parameters.
107  * It can be called from within the render() method of CRenderizable-derived
108  * classes, and
109  * the returned matrices can be used to determine whether a given point
110  * (lx,ly,lz)
111  * in local coordinates wrt the object being rendered falls within the screen
112  * or not:
113  * \code
114  * TRenderInfo ri;
115  * getCurrentRenderingInfo(ri);
116  * Eigen::Matrix<float,4,4> M= ri.proj_matrix * ri.model_matrix *
117  * HomogeneousMatrix(lx,ly,lz);
118  * const float rend_x = M(0,3)/M(3,3);
119  * const float rend_y = M(1,3)/M(3,3);
120  * \endcode
121  * where (rend_x,rend_y) are both in the range [-1,1].
122  */
123 void getCurrentRenderingInfo(TRenderInfo& ri);
124 
125 /** Draws a message box with a centered (possibly multi-lined) text.
126  * It consists of a filled rectangle with a frame around and the centered text
127  * in the middle.
128  *
129  * The appearance of the box is highly configurable via parameters.
130  *
131  * \param[in] msg_x, msg_y The left-lower corner coordinates, in ratio [0,1]
132  * of the viewport size. (0,0) if the left-bottom corner of the viewport.
133  * \param[in] msg_w, msg_h The width & height, in ratio [0,1] of the viewport
134  * size.
135  * \param[in] text The text to display. Multiple lines can be drawn with the
136  * '\n' character.
137  * \param[in] text_scale Size of characters, in ration [0,1] of the viewport
138  * size. Note that this size may be scaled automatically reduced to fit the text
139  * withtin the rectangle of the message box.
140  * \param[in] back_col Color of the rectangle background. Alpha can be <255
141  * to enable transparency.
142  * \param[in] border_col Color of the rectangle frame. Alpha can be <255 to
143  * enable transparency.
144  * \param[in] text_col Color of the text background. Alpha can be <255 to
145  * enable transparency.
146  * \param[in] border_width Width of the border, in pixels
147  * \param[in] text_font, text_style, text_spacing, text_kerning See
148  * mrpt::opengl::gl_utils::glDrawText()
149  *
150  * Example (see directory: 'samples/display3D_custom_render'):
151  *
152  * <img src="gl_utils_message_box.jpg" >
153  */
154 void renderMessageBox(
155  const float msg_x, const float msg_y, const float msg_w, const float msg_h,
156  const std::string& text, float text_scale,
157  const mrpt::img::TColor& back_col = mrpt::img::TColor(0, 0, 50, 150),
158  const mrpt::img::TColor& border_col = mrpt::img::TColor(0, 0, 0, 140),
159  const mrpt::img::TColor& text_col = mrpt::img::TColor(255, 255, 255, 220),
160  const float border_width = 4.0f,
161  const std::string& text_font = std::string("sans"),
163  const double text_spacing = 1.5, const double text_kerning = 0.1);
164 
165 /** @} */ // -----------------------------------------------------
166 
167 /** @name OpenGL bitmapped 2D fonts
168  @{ */
169 
170 /** This method is safe for calling from within ::render() methods \sa
171  * renderTextBitmap */
172 void renderTextBitmap(const char* str, void* fontStyle);
173 
174 /** Return the exact width in pixels for a given string, as will be rendered by
175  * renderTextBitmap().
176  * \sa renderTextBitmap
177  */
178 int textBitmapWidth(
179  const std::string& str, mrpt::opengl::TOpenGLFont font =
181 
182 /** @} */ // --------------------------------------------------
183 
184 /** @name OpenGL vector 3D fonts
185  @{ */
186 
187 /// sets the font to use for future font rendering commands. currently "sans",
188 /// "serif" and "mono" are available.
189 /// @param fontname string containing font name
190 void glSetFont(const std::string& fontname);
191 
192 /// returns the name of the currently active font
193 const std::string& glGetFont();
194 
195 /// renders a string in GL using the current settings.
196 /// Font coordinates are +X along the line and +Y along the up direction of
197 /// glyphs.
198 /// The origin is at the top baseline at the left of the first character.
199 /// Characters have a maximum size of 1.
200 /// linefeed is interpreted as a new line and the start is offset in -Y
201 /// direction by @ref spacing . Individual characters
202 /// are separated by @ref kerning + plus their individual with.
203 /// @param text string to be rendered, unknown characters are replaced with '?'
204 /// @param textScale The size of the characters (default=1.0)
205 /// @param style rendering style
206 /// @param spacing distance between individual text lines
207 /// @param kerning distance between characters
208 /// \note This functions comes from libcvd (BSD,
209 /// http://www.edwardrosten.com/cvd/ )
211  const std::string& text, const double textScale,
212  enum TOpenGLFontStyle style = NICE, double spacing = 1.5,
213  double kerning = 0.1);
214 
215 /// returns the size of the bounding box of a text to be rendered, similar to
216 /// @ref glDrawText but without any visual output
217 /// \note This functions comes from libcvd (BSD,
218 /// http://www.edwardrosten.com/cvd/ )
220  const std::string& text, const double textScale, double spacing = 1.5,
221  double kerning = 0.1);
222 
223 /** @} */ // --------------------------------------------------
224 } // namespace gl_utils
225 } // namespace opengl
226 } // namespace mrpt
void renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
Definition: gl_utils.cpp:258
void renderTriangleWithNormal(const mrpt::math::TPoint3D &p1, const mrpt::math::TPoint3D &p2, const mrpt::math::TPoint3D &p3)
Can be used by derived classes to draw a triangle with a normal vector computed automatically - to be...
Definition: gl_utils.cpp:157
GLdouble GLdouble z
Definition: glext.h:3872
void renderQuadWithNormal(const mrpt::math::TPoint3Df &p1, const mrpt::math::TPoint3Df &p2, const mrpt::math::TPoint3Df &p3, const mrpt::math::TPoint3Df &p4)
Can be used by derived classes to draw a quad with a normal vector computed automatically - to be cal...
Definition: gl_utils.cpp:205
const std::string & glGetFont()
returns the name of the currently active font
Definition: gl_utils.cpp:523
std::deque< CRenderizable::Ptr > CListOpenGLObjects
A list of objects pointers, automatically managing memory free at destructor, and managing copies cor...
A pair (x,y) of pixel coordinates (subpixel resolution).
Definition: TPixelCoord.h:20
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
void glSetFont(const std::string &fontname)
sets the font to use for future font rendering commands.
Definition: gl_utils.cpp:514
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:25
Lightweight 3D point (float version).
TOpenGLFontStyle
Different style for vectorized font rendering.
Definition: opengl_fonts.h:36
mrpt::img::TPixelCoordf glGetExtends(const std::string &text, const double textScale, double spacing=1.5, double kerning=0.1)
returns the size of the bounding box of a text to be rendered, similar to glDrawText but without any ...
Definition: gl_utils.cpp:553
Information about the rendering process being issued.
Definition: gl_utils.h:30
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > model_matrix
The 4x4 model transformation matrix.
Definition: gl_utils.h:37
int vp_x
Rendering viewport geometry (in pixels)
Definition: gl_utils.h:33
GLsizei const GLchar ** string
Definition: glext.h:4101
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > full_matrix
PROJ * MODEL.
Definition: gl_utils.h:39
void renderMessageBox(const float msg_x, const float msg_y, const float msg_w, const float msg_h, const std::string &text, float text_scale, const mrpt::img::TColor &back_col=mrpt::img::TColor(0, 0, 50, 150), const mrpt::img::TColor &border_col=mrpt::img::TColor(0, 0, 0, 140), const mrpt::img::TColor &text_col=mrpt::img::TColor(255, 255, 255, 220), const float border_width=4.0f, const std::string &text_font=std::string("sans"), mrpt::opengl::TOpenGLFontStyle text_style=mrpt::opengl::FILL, const double text_spacing=1.5, const double text_kerning=0.1)
Draws a message box with a centered (possibly multi-lined) text.
Definition: gl_utils.cpp:387
int textBitmapWidth(const std::string &str, mrpt::opengl::TOpenGLFont font=mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_24)
Return the exact width in pixels for a given string, as will be rendered by renderTextBitmap().
Definition: gl_utils.cpp:301
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:143
Eigen::Matrix< float, 4, 4, Eigen::ColMajor > proj_matrix
The 4x4 projection matrix.
Definition: gl_utils.h:35
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:40
void getCurrentRenderingInfo(TRenderInfo &ri)
Gather useful information on the render parameters.
Definition: gl_utils.cpp:216
GLenum GLint GLint y
Definition: glext.h:3538
void projectPointPixels(float x, float y, float z, float &proj_x_px, float &proj_y_px, float &proj_z_depth) const
Exactly like projectPoint but the (x,y) projected coordinates are given in pixels instead of normaliz...
Definition: gl_utils.h:63
A RGB color - 8bit.
Definition: TColor.h:22
GLenum GLint x
Definition: glext.h:3538
Lightweight 3D point.
void renderSetOfObjects(const mrpt::opengl::CListOpenGLObjects &objs)
For each object in the list:
Definition: gl_utils.cpp:35
void projectPoint(float x, float y, float z, float &proj_x, float &proj_y, float &proj_z_depth) const
Computes the normalized coordinates (range=[0,1]) on the current rendering viewport of a point with l...
Definition: gl_utils.h:49
renders glyphs as filled polygons
Definition: opengl_fonts.h:38
mrpt::math::TPoint3Df camera_position
The 3D location of the camera.
Definition: gl_utils.h:41



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019