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