Main MRPT website > C++ reference
MRPT logo

CRenderizable.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
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_CRenderizable_H
00029 #define opengl_CRenderizable_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/CSerializable.h>
00033 
00034 #include <mrpt/synch/CCriticalSection.h>
00035 #include <mrpt/math/lightweight_geom_data.h>
00036 
00037 #include <mrpt/opengl/link_pragmas.h>
00038 
00039 namespace mrpt
00040 {
00041         namespace poses { class CPose3D; class CPoint3D; class CPoint2D; }
00042         namespace utils { class CStringList; }
00043 
00044         namespace opengl
00045         {
00046                 class COpenGLViewport;
00047                 class CSetOfObjects;
00048 
00049                 // This must be added to any CSerializable derived class:
00050                 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CRenderizable, mrpt::utils::CSerializable, OPENGL_IMPEXP )
00051 
00052                 /** The base class of 3D objects that can be directly rendered through OpenGL.
00053                   *  In this class there are a set of common properties to all 3D objects, mainly:
00054                   *             - A name (m_name): A name that can be optionally asigned to objects for easing its reference.
00055                   *             - 6D coordinates (x,y,z,yaw,pitch,roll), relative to the "current" reference framework. By default, any object is referenced to global scene coordinates.
00056                   *             - A RGB color: This field will be used in simple elements (points, lines, text,...) but is ignored in more complex objects that carry their own color information (triangle sets,...)
00057                   *  See the main class opengl::COpenGLScene
00058                   *  \sa opengl::COpenGLScene, mrpt::opengl
00059                   */
00060                 class OPENGL_IMPEXP CRenderizable : public mrpt::utils::CSerializable
00061                 {
00062                         DEFINE_VIRTUAL_SERIALIZABLE( CRenderizable )
00063 
00064                         friend class mrpt::opengl::COpenGLViewport;
00065                         friend class mrpt::opengl::CSetOfObjects;
00066 
00067                 protected:
00068                         std::string                             m_name;
00069                         bool                                    m_show_name;
00070                         double                                  m_color_R,m_color_G,m_color_B,m_color_A;    //!< Color components in the range [0,1]
00071                         double                                  m_x,m_y,m_z;                                                            //!< Translation relative to parent coordinate origin.
00072                         double                                  m_yaw,m_pitch,m_roll;                                           //!< Rotation relative to parent coordinate origin, in **DEGREES**.
00073                         float                                   m_scale_x, m_scale_y, m_scale_z;                        //!< Scale components to apply to the object (default=1)
00074                         bool                                    m_visible; //!< Is the object visible? (default=true)
00075 
00076                 public:
00077                         /** @name Changes the appearance of the object to render
00078                             @{ */
00079 
00080                         void setName(const std::string &n) { m_name=n; }        //!< Changes the name of the object
00081                         std::string getName() const { return m_name; }          //!< Returns the name of the object
00082 
00083                         inline bool isVisible() const /** Is the object visible? \sa setVisibility */  { return m_visible; }
00084                         inline void setVisibility(bool visible=true) /** Set object visibility (default=true) \sa isVisible */  { m_visible=visible; }
00085 
00086                         void enableShowName(bool showName=true) { m_show_name=showName; }       //!< Enables or disables showing the name of the object as a label when rendering
00087 
00088                         CRenderizable& setPose( const mrpt::poses::CPose3D &o );        //!< Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
00089                         CRenderizable& setPose( const mrpt::math::TPose3D &o ); //!< Set the 3D pose from a  mrpt::math::TPose3D object (return a ref to this)
00090                         CRenderizable& setPose( const mrpt::poses::CPoint3D &o );       //!< Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
00091                         CRenderizable& setPose( const mrpt::poses::CPoint2D &o );       //!< Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
00092 
00093                         mrpt::math::TPose3D getPose() const;    //!< Returns the 3D pose of the object
00094 
00095                         /** Changes the location of the object, keeping untouched the orientation  \return a ref to this */
00096                         inline CRenderizable& setLocation(double x,double y,double z) { m_x=x; m_y=y; m_z=z; return *this; }
00097 
00098                         /** Changes the location of the object, keeping untouched the orientation  \return a ref to this  */
00099                         inline CRenderizable& setLocation(const mrpt::math::TPoint3D &p ) { m_x=p.x; m_y=p.y; m_z=p.z; return *this;  }
00100 
00101                         double getPoseX() const { return m_x; } //!< Translation relative to parent coordinate origin.
00102                         double getPoseY() const { return m_y; } //!< Translation relative to parent coordinate origin.
00103                         double getPoseZ() const { return m_z; } //!< Translation relative to parent coordinate origin.
00104                         double getPoseYaw() const { return m_yaw; } //!< Rotation relative to parent coordinate origin, in **DEGREES**.
00105                         double getPosePitch() const { return m_pitch; } //!< Rotation relative to parent coordinate origin, in **DEGREES**.
00106                         double getPoseRoll() const { return m_roll; } //!< Rotation relative to parent coordinate origin, in **DEGREES**.
00107 
00108                         double getColorR() const { return m_color_R; } //!< Color components in the range [0,1]
00109                         double getColorG() const { return m_color_G; } //!< Color components in the range [0,1]
00110                         double getColorB() const { return m_color_B; } //!< Color components in the range [0,1]
00111                         double getColorA() const { return m_color_A; } //!< Color components in the range [0,1]
00112 
00113                         virtual CRenderizable&  setColorR(const double r)       {m_color_R=r; return *this;}    //!<Color components in the range [0,1] \return a ref to this
00114                         virtual CRenderizable&  setColorG(const double g)       {m_color_G=g; return *this;}    //!<Color components in the range [0,1] \return a ref to this
00115                         virtual CRenderizable&  setColorB(const double b)       {m_color_B=b; return *this;}    //!<Color components in the range [0,1] \return a ref to this
00116                         virtual CRenderizable&  setColorA(const double a)       {m_color_A=a; return *this;}    //!<Color components in the range [0,1] \return a ref to this
00117 
00118                         inline CRenderizable& setScale(float s)  { m_scale_x=m_scale_y=m_scale_z = s; return *this; } //!< Scale to apply to the object, in all three axes (default=1)  \return a ref to this
00119                         inline CRenderizable& setScale(float sx,float sy,float sz)  { m_scale_x=sx; m_scale_y=sy; m_scale_z = sz; return *this; } //!< Scale to apply to the object in each axis (default=1)  \return a ref to this
00120                         inline float getScaleX() const { return m_scale_x; }  //!< Get the current scaling factor in one axis
00121                         inline float getScaleY() const { return m_scale_y; }  //!< Get the current scaling factor in one axis
00122                         inline float getScaleZ() const { return m_scale_z; }  //!< Get the current scaling factor in one axis
00123 
00124 
00125                         inline mrpt::utils::TColorf getColor() const { return mrpt::utils::TColorf(m_color_R,m_color_G,m_color_B,m_color_A); }  //!< Returns the object color property as a TColorf
00126                         virtual CRenderizable& setColor( const mrpt::utils::TColorf &c);  //!< Changes the default object color \return a ref to this
00127 
00128                         /** Set the color components of this object (R,G,B,Alpha, in the range 0-1)  \return a ref to this */
00129                         virtual CRenderizable& setColor( double R, double G, double B, double A=1);
00130 
00131                         /** @} */
00132 
00133 
00134                         /** Default constructor:  */
00135                         CRenderizable();
00136                         virtual ~CRenderizable();
00137 
00138                         /** Interface for the stlplus smart pointer class. */
00139                         inline CRenderizable * clone() const
00140                         {
00141                                 return static_cast<CRenderizable*>( this->duplicate() );
00142                         }
00143 
00144                         /** Implements the rendering of 3D objects in each class derived from CRenderizable.
00145                           */
00146                         virtual void  render() const = 0;
00147 
00148 
00149                         /** Simulation of ray-trace, given a pose. Returns true if the ray effectively collisions with the object (returning the distance to the origin of the ray in "dist"), or false in other case. "dist" variable yields undefined behaviour when false is returned
00150                           */
00151                         virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
00152 
00153                         static void     renderTextBitmap( const char *str, void *fontStyle );
00154                         
00155                         /** Information about the rendering process being issued. \sa See getCurrentRenderingInfo for more details */
00156                         struct OPENGL_IMPEXP TRenderInfo
00157                         {
00158                                 int vp_x, vp_y, vp_width, vp_height;    //!< Rendering viewport geometry (in pixels)
00159                                 Eigen::Matrix<float,4,4,Eigen::ColMajor>  proj_matrix;  //!< The 4x4 projection matrix
00160                                 Eigen::Matrix<float,4,4,Eigen::ColMajor>  model_matrix;  //!< The 4x4 model transformation matrix
00161                                 Eigen::Matrix<float,4,4,Eigen::ColMajor>  full_matrix;  //!< PROJ * MODEL
00162                                 mrpt::math::TPoint3Df   camera_position;  //!< The 3D location of the camera
00163 
00164                                 /** Computes the normalized coordinates (range=[0,1]) on the current rendering viewport of a
00165                                   * point with local coordinates (wrt to the current model matrix) of (x,y,z).
00166                                   *  The output proj_z_depth is the real distance from the eye to the point.
00167                                   */
00168                                 void projectPoint(float x,float y,float z, float &proj_x, float &proj_y, float &proj_z_depth) const
00169                                 {
00170                                         const Eigen::Matrix<float,4,1,Eigen::ColMajor> proj = full_matrix * Eigen::Matrix<float,4,1,Eigen::ColMajor>(x,y,z,1);
00171                                         proj_x = proj[2] ? proj[0]/proj[2] : 0;
00172                                         proj_y = proj[2] ? proj[1]/proj[2] : 0;
00173                                         proj_z_depth = proj[3];
00174                                 }
00175 
00176                                 /** Exactly like projectPoint but the (x,y) projected coordinates are given in pixels instead of normalized coordinates. */
00177                                 void projectPointPixels(float x,float y,float z, float &proj_x_px, float &proj_y_px, float &proj_z_depth) const
00178                                 {
00179                                         projectPoint(x,y,z,proj_x_px,proj_y_px,proj_z_depth);
00180                                         proj_x_px = (proj_x_px+1.0f)*(vp_width/2);
00181                                         proj_y_px = (proj_y_px+1.0f)*(vp_height/2);
00182                                 }
00183                         };
00184 
00185                 protected:
00186                         /** Checks glGetError and throws an exception if an error situation is found */
00187                         static void checkOpenGLError();
00188                         /** Can be used by derived classes to draw a triangle with a normal vector computed automatically - to be called within a glBegin()-glEnd() block. */
00189                         static void renderTriangleWithNormal( const mrpt::math::TPoint3D &p1,const mrpt::math::TPoint3D &p2,const mrpt::math::TPoint3D &p3 );
00190 
00191                         void  writeToStreamRender(utils::CStream &out) const;
00192                         void  readFromStreamRender(utils::CStream &in);
00193 
00194                         /** Returns the lowest next free texture name (avoid using OpenGL's own function since we may call them from different threads and seem it's not cool).  */
00195                         static unsigned int getNewTextureNumber();
00196                         static void releaseTextureName(unsigned int i);
00197 
00198 
00199                         /** Gather useful information on the render parameters.
00200                           *  It can be called from within the render() method of derived classes, and
00201                           *   the returned matrices can be used to determine whether a given point (lx,ly,lz)
00202                           *   in local coordinates wrt the object being rendered falls within the screen or not:
00203                           * \code
00204                           *  TRenderInfo ri;
00205                           *  getCurrentRenderingInfo(ri);
00206                           *  Eigen::Matrix<float,4,4> M= ri.proj_matrix * ri.model_matrix * HomogeneousMatrix(lx,ly,lz);
00207                           *  const float rend_x = M(0,3)/M(3,3);
00208                           *  const float rend_y = M(1,3)/M(3,3);
00209                           * \endcode
00210                           *  where (rend_x,rend_y) are both in the range [-1,1].
00211                           */
00212                         void getCurrentRenderingInfo(TRenderInfo &ri) const;
00213 
00214                 };
00215                 /**
00216                   * Applies a CPose3D transformation to the object. Note that this method doesn't <i>set</i> the pose to the given value, but <i>combines</i> it with the existing one.
00217                   * \sa setPose
00218                   */
00219                 OPENGL_IMPEXP CRenderizablePtr & operator<<(CRenderizablePtr &r,const mrpt::poses::CPose3D &p);
00220 
00221         } // end namespace
00222 
00223 } // End of namespace
00224 
00225 
00226 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011