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



Page generated by Doxygen 1.9.7 for MRPT 1.4.0 SVN: at Tue Jun 13 13:45:58 UTC 2023