Main MRPT website > C++ reference
MRPT logo
gl_utils.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef opengl_glutils_H
29 #define opengl_glutils_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 
34 #ifndef opengl_CRenderizable_H
36 #endif
37 
39 
40 namespace mrpt
41 {
42  namespace opengl
43  {
44  /** A set of auxiliary functions that can be called to render OpenGL primitives from MRPT or user code
45  * \ingroup mrpt_opengl_grp
46  */
47  namespace gl_utils
48  {
49 
50  /** @name Data types for mrpt::opengl::gl_utils
51  @{ */
52 
53  /** Information about the rendering process being issued. \sa See getCurrentRenderingInfo for more details */
55  {
56  int vp_x, vp_y, vp_width, vp_height; //!< Rendering viewport geometry (in pixels)
57  Eigen::Matrix<float,4,4,Eigen::ColMajor> proj_matrix; //!< The 4x4 projection matrix
58  Eigen::Matrix<float,4,4,Eigen::ColMajor> model_matrix; //!< The 4x4 model transformation matrix
59  Eigen::Matrix<float,4,4,Eigen::ColMajor> full_matrix; //!< PROJ * MODEL
60  mrpt::math::TPoint3Df camera_position; //!< The 3D location of the camera
61 
62  /** Computes the normalized coordinates (range=[0,1]) on the current rendering viewport of a
63  * point with local coordinates (wrt to the current model matrix) of (x,y,z).
64  * The output proj_z_depth is the real distance from the eye to the point.
65  */
66  void projectPoint(float x,float y,float z, float &proj_x, float &proj_y, float &proj_z_depth) const
67  {
68  const Eigen::Matrix<float,4,1,Eigen::ColMajor> proj = full_matrix * Eigen::Matrix<float,4,1,Eigen::ColMajor>(x,y,z,1);
69  proj_x = proj[3] ? proj[0]/proj[3] : 0;
70  proj_y = proj[3] ? proj[1]/proj[3] : 0;
71  proj_z_depth = proj[2];
72  }
73 
74  /** Exactly like projectPoint but the (x,y) projected coordinates are given in pixels instead of normalized coordinates. */
75  void projectPointPixels(float x,float y,float z, float &proj_x_px, float &proj_y_px, float &proj_z_depth) const
76  {
77  projectPoint(x,y,z,proj_x_px,proj_y_px,proj_z_depth);
78  proj_x_px = (proj_x_px+1.0f)*(vp_width/2);
79  proj_y_px = (proj_y_px+1.0f)*(vp_height/2);
80  }
81  };
82 
83  /** @} */ // -----------------------------------------------------
84 
85 
86  /** @name Miscellaneous rendering methods
87  @{ */
88 
89  /** For each object in the list:
90  * - checks visibility of each object
91  * - prepare the GL_MODELVIEW matrix according to its coordinates
92  * - call its ::render()
93  * - shows its name (if enabled).
94  *
95  * \note Used by COpenGLViewport, CSetOfObjects
96  */
98 
99  /** Checks glGetError and throws an exception if an error situation is found */
101 
102  /** Can be used by derived classes to draw a triangle with a normal vector computed automatically - to be called within a glBegin()-glEnd() block. */
105 
106  /** Can be used by derived classes to draw a quad with a normal vector computed automatically - to be called within a glBegin()-glEnd() block. */
108 
109 
110  /** Gather useful information on the render parameters.
111  * It can be called from within the render() method of CRenderizable-derived classes, and
112  * the returned matrices can be used to determine whether a given point (lx,ly,lz)
113  * in local coordinates wrt the object being rendered falls within the screen or not:
114  * \code
115  * TRenderInfo ri;
116  * getCurrentRenderingInfo(ri);
117  * Eigen::Matrix<float,4,4> M= ri.proj_matrix * ri.model_matrix * 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 OPENGL_IMPEXP getCurrentRenderingInfo(TRenderInfo &ri);
124 
125 
126  /** Draws a message box with a centered (possibly multi-lined) text.
127  * It consists of a filled rectangle with a frame around and the centered text 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] of the viewport size. (0,0) if the left-bottom corner of the viewport.
132  * \param[in] msg_w, msg_h The width & height, in ratio [0,1] of the viewport size.
133  * \param[in] text The text to display. Multiple lines can be drawn with the '\n' character.
134  * \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.
135  * \param[in] back_col Color of the rectangle background. Alpha can be <255 to enable transparency.
136  * \param[in] border_col Color of the rectangle frame. Alpha can be <255 to enable transparency.
137  * \param[in] text_col Color of the text background. Alpha can be <255 to enable transparency.
138  * \param[in] border_width Width of the border, in pixels
139  * \param[in] text_font, text_style, text_spacing, text_kerning See mrpt::opengl::gl_utils::glDrawText()
140  *
141  * Example (see directory: 'samples/display3D_custom_render'):
142  *
143  * <img src="gl_utils_message_box.jpg" >
144  */
146  const float msg_x, const float msg_y,
147  const float msg_w, const float msg_h,
148  const std::string &text,
149  float text_scale,
150  const mrpt::utils::TColor back_col = mrpt::utils::TColor(0,0,50, 150),
151  const mrpt::utils::TColor border_col = mrpt::utils::TColor(0,0,0, 140),
152  const mrpt::utils::TColor text_col = mrpt::utils::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,
157  const double text_kerning = 0.1
158  );
159 
160  /** @} */ // -----------------------------------------------------
161 
162  /** @name OpenGL bitmapped 2D fonts
163  @{ */
164 
165  /** This method is safe for calling from within ::render() methods \sa renderTextBitmap */
166  void OPENGL_IMPEXP renderTextBitmap( const char *str, void *fontStyle );
167 
168  /** Return the exact width in pixels for a given string, as will be rendered by renderTextBitmap().
169  * \sa renderTextBitmap
170  */
172  const std::string &str,
174 
175  /** @} */ // --------------------------------------------------
176 
177 
178  /** @name OpenGL vector 3D fonts
179  @{ */
180 
181  /// sets the font to use for future font rendering commands. currently "sans", "serif" and "mono" are available.
182  /// @param fontname string containing font name
183  void OPENGL_IMPEXP glSetFont( const std::string & fontname );
184 
185  /// returns the name of the currently active font
186  const OPENGL_IMPEXP 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 glyphs.
190  /// The origin is at the top baseline at the left of the first character. Characters have a maximum size of 1.
191  /// linefeed is interpreted as a new line and the start is offset in -Y direction by @ref spacing . Individual characters
192  /// are separated by @ref kerning + plus their individual with.
193  /// @param text string to be rendered, unknown characters are replaced with '?'
194  /// @param textScale The size of the characters (default=1.0)
195  /// @param style rendering style
196  /// @param spacing distance between individual text lines
197  /// @param kerning distance between characters
198  /// \note This functions comes from libcvd (LGPL, http://www.edwardrosten.com/cvd/ )
199  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);
200 
201  /// returns the size of the bounding box of a text to be rendered, similar to @ref glDrawText but without any visual output
202  /// \note This functions comes from libcvd (LGPL, http://www.edwardrosten.com/cvd/ )
203  mrpt::utils::TPixelCoordf OPENGL_IMPEXP glGetExtends(const std::string & text, const double textScale, double spacing = 1.5, double kerning = 0.1);
204 
205  /** @} */ // --------------------------------------------------
206 
207  }
208  }
209 }
210 
211 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013