Main MRPT website > C++ reference
MRPT logo
CRenderizable.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_CRenderizable_H
29 #define opengl_CRenderizable_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 #include <mrpt/utils/TColor.h>
34 
35 #include <mrpt/poses/CPose3D.h>
36 
39 
41 
43 
44 namespace mrpt
45 {
46  namespace poses { class CPoint3D; class CPoint2D; }
47  namespace utils { class CStringList; }
48 
49  namespace opengl
50  {
51  class COpenGLViewport;
52  class CSetOfObjects;
53 
54 
55  // This must be added to any CSerializable derived class:
57 
58  /** A list of objects pointers, automatically managing memory free at destructor, and managing copies correctly. */
59  typedef std::deque<CRenderizablePtr> CListOpenGLObjects;
60 
61  /** The base class of 3D objects that can be directly rendered through OpenGL.
62  * In this class there are a set of common properties to all 3D objects, mainly:
63  * - A name (m_name): A name that can be optionally asigned to objects for easing its reference.
64  * - 6D coordinates (x,y,z,yaw,pitch,roll), relative to the "current" reference framework. By default, any object is referenced to global scene coordinates.
65  * - 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,...)
66  * See the main class opengl::COpenGLScene
67  * \sa opengl::COpenGLScene, mrpt::opengl
68  * \ingroup mrpt_opengl_grp
69  */
70  class OPENGL_IMPEXP CRenderizable : public mrpt::utils::CSerializable
71  {
73 
74  friend class mrpt::opengl::COpenGLViewport;
75  friend class mrpt::opengl::CSetOfObjects;
76 
77  protected:
78  std::string m_name;
79  bool m_show_name;
80  mrpt::utils::TColor m_color; //!< Color components in the range [0,255]
81  mrpt::poses::CPose3D m_pose; //!< 6D pose wrt the parent coordinate reference. This class automatically holds the cached 3x3 rotation matrix for quick load into opengl stack.
82  float m_scale_x, m_scale_y, m_scale_z; //!< Scale components to apply to the object (default=1)
83  bool m_visible; //!< Is the object visible? (default=true)
84 
85  public:
86  /** @name Changes the appearance of the object to render
87  @{ */
88 
89  void setName(const std::string &n) { m_name=n; } //!< Changes the name of the object
90  const std::string &getName() const { return m_name; } //!< Returns the name of the object
91 
92  inline bool isVisible() const /** Is the object visible? \sa setVisibility */ { return m_visible; }
93  inline void setVisibility(bool visible=true) /** Set object visibility (default=true) \sa isVisible */ { m_visible=visible; }
94 
95  inline void enableShowName(bool showName=true) { m_show_name=showName; } //!< Enables or disables showing the name of the object as a label when rendering
96  inline bool isShowNameEnabled() const { return m_show_name; } //!< \sa enableShowName
97 
98  CRenderizable& setPose( const mrpt::poses::CPose3D &o ); //!< Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
99  CRenderizable& setPose( const mrpt::math::TPose3D &o ); //!< Set the 3D pose from a mrpt::math::TPose3D object (return a ref to this)
100  CRenderizable& setPose( const mrpt::poses::CPoint3D &o ); //!< Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
101  CRenderizable& setPose( const mrpt::poses::CPoint2D &o ); //!< Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
102 
103  mrpt::math::TPose3D getPose() const; //!< Returns the 3D pose of the object as TPose3D
104  /** Returns a const ref to the 3D pose of the object as CPose3D (which explicitly contains the 3x3 rotation matrix) */
105  inline const mrpt::poses::CPose3D & getPoseRef() const { return m_pose; }
106 
107  /** Changes the location of the object, keeping untouched the orientation \return a ref to this */
108  inline CRenderizable& setLocation(double x,double y,double z) { m_pose.x(x); m_pose.y(y); m_pose.z(z); return *this; }
109 
110  /** Changes the location of the object, keeping untouched the orientation \return a ref to this */
111  inline CRenderizable& setLocation(const mrpt::math::TPoint3D &p ) { m_pose.x(p.x); m_pose.y(p.y); m_pose.z(p.z); return *this; }
112 
113  inline double getPoseX() const { return m_pose.x(); } //!< Translation relative to parent coordinate origin.
114  inline double getPoseY() const { return m_pose.y(); } //!< Translation relative to parent coordinate origin.
115  inline double getPoseZ() const { return m_pose.z(); } //!< Translation relative to parent coordinate origin.
116  inline double getPoseYaw() const { return mrpt::utils::RAD2DEG(m_pose.yaw()); } //!< Rotation relative to parent coordinate origin, in **DEGREES**.
117  inline double getPosePitch() const { return mrpt::utils::RAD2DEG(m_pose.pitch()); } //!< Rotation relative to parent coordinate origin, in **DEGREES**.
118  inline double getPoseRoll() const { return mrpt::utils::RAD2DEG(m_pose.roll()); } //!< Rotation relative to parent coordinate origin, in **DEGREES**.
119  inline double getPoseYawRad() const { return m_pose.yaw(); } //!< Rotation relative to parent coordinate origin, in radians.
120  inline double getPosePitchRad() const { return m_pose.pitch(); } //!< Rotation relative to parent coordinate origin, in radians.
121  inline double getPoseRollRad() const { return m_pose.roll(); } //!< Rotation relative to parent coordinate origin, in radians.
122 
123  inline double getColorR() const { return m_color.R/255.; } //!< Color components in the range [0,1]
124  inline double getColorG() const { return m_color.G/255.; } //!< Color components in the range [0,1]
125  inline double getColorB() const { return m_color.B/255.; } //!< Color components in the range [0,1]
126  inline double getColorA() const { return m_color.A/255.; } //!< Color components in the range [0,1]
127 
128  inline uint8_t getColorR_u8() const { return m_color.R; } //!< Color components in the range [0,255]
129  inline uint8_t getColorG_u8() const { return m_color.G; } //!< Color components in the range [0,255]
130  inline uint8_t getColorB_u8() const { return m_color.B; } //!< Color components in the range [0,255]
131  inline uint8_t getColorA_u8() const { return m_color.A; } //!< Color components in the range [0,255]
132 
133  CRenderizable& setColorR(const double r) {return setColorR_u8(static_cast<uint8_t>(255*r));} //!<Color components in the range [0,1] \return a ref to this
134  CRenderizable& setColorG(const double g) {return setColorG_u8(static_cast<uint8_t>(255*g));} //!<Color components in the range [0,1] \return a ref to this
135  CRenderizable& setColorB(const double b) {return setColorB_u8(static_cast<uint8_t>(255*b));} //!<Color components in the range [0,1] \return a ref to this
136  CRenderizable& setColorA(const double a) {return setColorA_u8(static_cast<uint8_t>(255*a));} //!<Color components in the range [0,1] \return a ref to this
137 
138  virtual CRenderizable& setColorR_u8(const uint8_t r) {m_color.R=r; return *this;} //!<Color components in the range [0,255] \return a ref to this
139  virtual CRenderizable& setColorG_u8(const uint8_t g) {m_color.G=g; return *this;} //!<Color components in the range [0,255] \return a ref to this
140  virtual CRenderizable& setColorB_u8(const uint8_t b) {m_color.B=b; return *this;} //!<Color components in the range [0,255] \return a ref to this
141  virtual CRenderizable& setColorA_u8(const uint8_t a) {m_color.A=a; return *this;} //!<Color components in the range [0,255] \return a ref to this
142 
143  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
144  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
145  inline float getScaleX() const { return m_scale_x; } //!< Get the current scaling factor in one axis
146  inline float getScaleY() const { return m_scale_y; } //!< Get the current scaling factor in one axis
147  inline float getScaleZ() const { return m_scale_z; } //!< Get the current scaling factor in one axis
148 
149 
150  inline mrpt::utils::TColorf getColor() const { return mrpt::utils::TColorf(m_color); } //!< Returns the object color property as a TColorf
151  CRenderizable& setColor( const mrpt::utils::TColorf &c) //!< Changes the default object color \return a ref to this
152  {
153  return setColor_u8(mrpt::utils::TColor(c.R*255.f,c.G*255.f,c.B*255.f,c.A*255.f));
154  }
155 
156  /** Set the color components of this object (R,G,B,Alpha, in the range 0-1) \return a ref to this */
157  inline CRenderizable& setColor( double R, double G, double B, double A=1) { return setColor_u8(R*255,G*255,B*255,A*255); }
158 
159  inline const mrpt::utils::TColor &getColor_u8() const { return m_color; } //!< Returns the object color property as a TColor
160  /*** Changes the default object color \return a ref to this */
161  virtual CRenderizable& setColor_u8( const mrpt::utils::TColor &c);
162 
163  /** Set the color components of this object (R,G,B,Alpha, in the range 0-1) \return a ref to this */
164  inline CRenderizable& setColor_u8( uint8_t R, uint8_t G, uint8_t B, uint8_t A=255) { return setColor_u8(mrpt::utils::TColor(R,G,B,A)); }
165 
166  /** @} */
167 
168 
169  /** Default constructor: */
170  CRenderizable();
171  virtual ~CRenderizable();
172 
173  /** Interface for the stlplus smart pointer class. */
174  inline CRenderizable * clone() const
175  {
176  return static_cast<CRenderizable*>( this->duplicate() );
177  }
178 
179  /** Implements the rendering of 3D objects in each class derived from CRenderizable.
180  */
181  virtual void render() const = 0;
182 
183  /** 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
184  */
185  virtual bool traceRay(const mrpt::poses::CPose3D &o,double &dist) const;
186 
187 
188  /** This method is safe for calling from within ::render() methods \sa renderTextBitmap, mrpt::opengl::gl_utils */
189  static void renderTextBitmap( const char *str, void *fontStyle );
190 
191  /** Return the exact width in pixels for a given string, as will be rendered by renderTextBitmap().
192  * \sa renderTextBitmap, mrpt::opengl::gl_utils
193  */
194  static int textBitmapWidth(
195  const std::string &str,
197 
198  /** Render a text message in the current rendering context, creating a glViewport in the way (do not call within ::render() methods)
199  * - Coordinates (x,y) are 2D pixels, starting at bottom-left of the viewport. Negative numbers will wrap to the opposite side of the viewport (e.g. x=-10 means 10px fromt the right).
200  * - The text color is defined by (color_r,color_g,color_b), each float numbers in the range [0,1].
201  * \sa renderTextBitmap, textBitmapWidth, mrpt::opengl::gl_utils
202  */
203  static void renderTextBitmap(
204  int screen_x,
205  int screen_y,
206  const std::string &str,
207  float color_r=1,
208  float color_g=1,
209  float color_b=1,
211  );
212 
213  protected:
214  /** Checks glGetError and throws an exception if an error situation is found */
215  static void checkOpenGLError();
216 
217  void writeToStreamRender(utils::CStream &out) const;
218  void readFromStreamRender(utils::CStream &in);
219 
220  /** 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). */
221  static unsigned int getNewTextureNumber();
222  static void releaseTextureName(unsigned int i);
223 
224  };
225  /**
226  * 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.
227  * \sa setPose
228  */
229  OPENGL_IMPEXP CRenderizablePtr & operator<<(CRenderizablePtr &r,const mrpt::poses::CPose3D &p);
230 
231  } // end namespace
232 
233 } // End of namespace
234 
235 // This header goes here so there we can use "CRenderizablePtr"
236 #include <mrpt/opengl/gl_utils.h>
237 
238 
239 #endif



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