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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 9b18308f3 Mon Nov 18 23:39:25 2019 +0100 at lun nov 18 23:45:12 CET 2019