Main MRPT website > C++ reference
MRPT logo
gl_utils.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef opengl_glutils_H
00029 #define opengl_glutils_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/opengl/opengl_fonts.h>
00033 
00034 #ifndef opengl_CRenderizable_H
00035 #       include <mrpt/opengl/CRenderizable.h>
00036 #endif
00037 
00038 #include <mrpt/opengl/link_pragmas.h>
00039 
00040 namespace mrpt
00041 {
00042         namespace opengl
00043         {
00044                 /** A set of auxiliary functions that can be called to render OpenGL primitives from MRPT or user code
00045                   * \ingroup mrpt_opengl_grp
00046                   */
00047                 namespace gl_utils
00048                 {
00049 
00050                         /** @name Data types for mrpt::opengl::gl_utils
00051                             @{ */
00052 
00053                         /** Information about the rendering process being issued. \sa See getCurrentRenderingInfo for more details */
00054                         struct OPENGL_IMPEXP TRenderInfo
00055                         {
00056                                 int vp_x, vp_y, vp_width, vp_height;    //!< Rendering viewport geometry (in pixels)
00057                                 Eigen::Matrix<float,4,4,Eigen::ColMajor>  proj_matrix;  //!< The 4x4 projection matrix
00058                                 Eigen::Matrix<float,4,4,Eigen::ColMajor>  model_matrix;  //!< The 4x4 model transformation matrix
00059                                 Eigen::Matrix<float,4,4,Eigen::ColMajor>  full_matrix;  //!< PROJ * MODEL
00060                                 mrpt::math::TPoint3Df   camera_position;  //!< The 3D location of the camera
00061 
00062                                 /** Computes the normalized coordinates (range=[0,1]) on the current rendering viewport of a
00063                                   * point with local coordinates (wrt to the current model matrix) of (x,y,z).
00064                                   *  The output proj_z_depth is the real distance from the eye to the point.
00065                                   */
00066                                 void projectPoint(float x,float y,float z, float &proj_x, float &proj_y, float &proj_z_depth) const
00067                                 {
00068                                         const Eigen::Matrix<float,4,1,Eigen::ColMajor> proj = full_matrix * Eigen::Matrix<float,4,1,Eigen::ColMajor>(x,y,z,1);
00069                                         proj_x = proj[3] ? proj[0]/proj[3] : 0;
00070                                         proj_y = proj[3] ? proj[1]/proj[3] : 0;
00071                                         proj_z_depth = proj[2];
00072                                 }
00073 
00074                                 /** Exactly like projectPoint but the (x,y) projected coordinates are given in pixels instead of normalized coordinates. */
00075                                 void projectPointPixels(float x,float y,float z, float &proj_x_px, float &proj_y_px, float &proj_z_depth) const
00076                                 {
00077                                         projectPoint(x,y,z,proj_x_px,proj_y_px,proj_z_depth);
00078                                         proj_x_px = (proj_x_px+1.0f)*(vp_width/2);
00079                                         proj_y_px = (proj_y_px+1.0f)*(vp_height/2);
00080                                 }
00081                         };
00082 
00083                         /** @} */  // -----------------------------------------------------
00084 
00085 
00086                         /** @name Miscellaneous rendering methods
00087                             @{ */
00088 
00089                         /** For each object in the list:
00090                           *   - checks visibility of each object
00091                           *   - prepare the GL_MODELVIEW matrix according to its coordinates
00092                           *   - call its ::render()
00093                           *   - shows its name (if enabled).
00094                           *
00095                           *  \note Used by  COpenGLViewport, CSetOfObjects
00096                           */
00097                         void OPENGL_IMPEXP renderSetOfObjects(const mrpt::opengl::CListOpenGLObjects &objs);
00098 
00099                         /** Checks glGetError and throws an exception if an error situation is found */
00100                         void OPENGL_IMPEXP checkOpenGLError();
00101 
00102                         /** Can be used by derived classes to draw a triangle with a normal vector computed automatically - to be called within a glBegin()-glEnd() block. */
00103                         void OPENGL_IMPEXP renderTriangleWithNormal( const mrpt::math::TPoint3D &p1,const mrpt::math::TPoint3D &p2,const mrpt::math::TPoint3D &p3 );
00104 
00105 
00106                         /** Gather useful information on the render parameters.
00107                           *  It can be called from within the render() method of CRenderizable-derived classes, and
00108                           *   the returned matrices can be used to determine whether a given point (lx,ly,lz)
00109                           *   in local coordinates wrt the object being rendered falls within the screen or not:
00110                           * \code
00111                           *  TRenderInfo ri;
00112                           *  getCurrentRenderingInfo(ri);
00113                           *  Eigen::Matrix<float,4,4> M= ri.proj_matrix * ri.model_matrix * HomogeneousMatrix(lx,ly,lz);
00114                           *  const float rend_x = M(0,3)/M(3,3);
00115                           *  const float rend_y = M(1,3)/M(3,3);
00116                           * \endcode
00117                           *  where (rend_x,rend_y) are both in the range [-1,1].
00118                           */
00119                         void OPENGL_IMPEXP getCurrentRenderingInfo(TRenderInfo &ri);
00120 
00121 
00122                         /** Draws a message box with a centered (possibly multi-lined) text.
00123                           *  It consists of a filled rectangle with a frame around and the centered text in the middle.
00124                           *
00125                           *  The appearance of the box is highly configurable via parameters.
00126                           *
00127                           *   \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.
00128                           *   \param[in] msg_w, msg_h The width & height, in ratio [0,1] of the viewport size.
00129                           *   \param[in] text  The text to display. Multiple lines can be drawn with the '\n' character.
00130                           *   \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.
00131                           *   \param[in] back_col Color of the rectangle background. Alpha can be <255 to enable transparency.
00132                           *   \param[in] border_col Color of the rectangle frame. Alpha can be <255 to enable transparency.
00133                           *   \param[in] text_col Color of the text background. Alpha can be <255 to enable transparency.
00134                           *   \param[in] border_width Width of the border, in pixels
00135                           *   \param[in] text_font, text_style, text_spacing, text_kerning See mrpt::opengl::gl_utils::glDrawText()
00136                           *
00137                           *  Example (see directory: 'samples/display3D_custom_render'):
00138                           *
00139                           *  <img src="gl_utils_message_box.jpg" >
00140                           */
00141                         void OPENGL_IMPEXP renderMessageBox(
00142                                 const float msg_x, const float msg_y,
00143                                 const float msg_w, const float msg_h,
00144                                 const std::string &text,
00145                                 float text_scale,
00146                                 const mrpt::utils::TColor back_col = mrpt::utils::TColor(0,0,50, 150),
00147                                 const mrpt::utils::TColor border_col = mrpt::utils::TColor(0,0,0, 140),
00148                                 const mrpt::utils::TColor text_col = mrpt::utils::TColor(255,255,255, 220),
00149                                 const float border_width = 4.0f,
00150                                 const std::string & text_font = std::string("sans"),
00151                                 mrpt::opengl::TOpenGLFontStyle text_style = mrpt::opengl::FILL,
00152                                 const double text_spacing = 1.5,
00153                                 const double text_kerning = 0.1
00154                                 );
00155 
00156                         /** @} */  // -----------------------------------------------------
00157 
00158                         /** @name OpenGL bitmapped 2D fonts
00159                             @{ */
00160 
00161                         /** This method is safe for calling from within ::render() methods \sa renderTextBitmap */
00162                         void OPENGL_IMPEXP renderTextBitmap( const char *str, void *fontStyle );
00163 
00164                         /** Return the exact width in pixels for a given string, as will be rendered by renderTextBitmap().
00165                           * \sa renderTextBitmap
00166                           */
00167                         int OPENGL_IMPEXP textBitmapWidth(
00168                                 const std::string &str,
00169                                 mrpt::opengl::TOpenGLFont    font = mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_24 );
00170 
00171                         /** @} */   // --------------------------------------------------
00172 
00173 
00174                         /** @name OpenGL vector 3D fonts
00175                             @{ */
00176 
00177                         /// sets the font to use for future font rendering commands. currently sans, serif and mono are available.
00178                         /// @param fontname string containing font name
00179                         void OPENGL_IMPEXP  glSetFont( const std::string & fontname );
00180 
00181                         /// returns the name of the currently active font
00182                         const OPENGL_IMPEXP std::string & glGetFont();
00183 
00184                         /// renders a string in GL using the current settings.
00185                         /// Font coordinates are +X along the line and +Y along the up direction of glyphs.
00186                         /// The origin is at the top baseline at the left of the first character. Characters have a maximum size of 1.
00187                         /// linefeed is interpreted as a new line and the start is offset in -Y direction by @ref spacing . Individual characters
00188                         /// are separated by @ref kerning + plus their individual with.
00189                         /// @param text string to be rendered, unknown characters are replaced with '?'
00190                         /// @param textScale The size of the characters (default=1.0)
00191                         /// @param style rendering style
00192                         /// @param spacing distance between individual text lines
00193                         /// @param kerning distance between characters
00194                         /// \note This functions comes from libcvd (LGPL, http://www.edwardrosten.com/cvd/ )
00195                         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);
00196 
00197                         /// returns the size of the bounding box of a text to be rendered, similar to @ref glDrawText but without any visual output
00198                         /// \note This functions comes from libcvd (LGPL, http://www.edwardrosten.com/cvd/ )
00199                         mrpt::utils::TPixelCoordf OPENGL_IMPEXP glGetExtends(const std::string & text, const double textScale, double spacing = 1.5, double kerning = 0.1);
00200 
00201                         /** @} */   // --------------------------------------------------
00202 
00203                 }
00204         }
00205 }
00206 
00207 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011