Main MRPT website > C++ reference for MRPT 1.9.9
CRenderizable.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_CRenderizable_H
10 #define opengl_CRenderizable_H
11 
13 #include <mrpt/utils/TColor.h>
14 #include <mrpt/math/math_frwds.h>
15 #include <mrpt/poses/CPose3D.h>
18 #include <deque>
19 
20 namespace mrpt
21 {
22 namespace opengl
23 {
24 class COpenGLViewport;
25 class CSetOfObjects;
26 
27 /** The base class of 3D objects that can be directly rendered through OpenGL.
28  * In this class there are a set of common properties to all 3D objects,
29  *mainly:
30  * - A name (m_name): A name that can be optionally asigned to objects for
31  *easing its reference.
32  * - 6D coordinates (x,y,z,yaw,pitch,roll), relative to the "current"
33  *reference framework. By default, any object is referenced to global scene
34  *coordinates.
35  * - A RGB color: This field will be used in simple elements (points,
36  *lines,
37  *text,...) but is ignored in more complex objects that carry their own color
38  *information (triangle sets,...)
39  * See the main class opengl::COpenGLScene
40  * \sa opengl::COpenGLScene, mrpt::opengl
41  * \ingroup mrpt_opengl_grp
42  */
44 {
46 
47  friend class mrpt::opengl::COpenGLViewport;
48  friend class mrpt::opengl::CSetOfObjects;
49 
50  protected:
51  std::string m_name;
53  /** Color components in the range [0,255] */
54  mrpt::utils::TColor m_color;
55  /** 6D pose wrt the parent coordinate reference. This class automatically
56  * holds the cached 3x3 rotation matrix for quick load into opengl stack. */
57  mrpt::poses::CPose3D m_pose;
58  /** Scale components to apply to the object (default=1) */
60  /** Is the object visible? (default=true) */
61  bool m_visible;
62 
63  public:
64  /** @name Changes the appearance of the object to render
65  @{ */
66 
67  /** Changes the name of the object */
68  void setName(const std::string& n) { m_name = n; }
69  /** Returns the name of the object */
70  const std::string& getName() const { return m_name; }
71  inline bool isVisible()
72  const /** Is the object visible? \sa setVisibility */
73  {
74  return m_visible;
75  }
76  inline void setVisibility(
77  bool visible =
78  true) /** Set object visibility (default=true) \sa isVisible */
79  {
80  m_visible = visible;
81  }
82 
83  /** Enables or disables showing the name of the object as a label when
84  * rendering */
85  inline void enableShowName(bool showName = true) { m_show_name = showName; }
86  /** \sa enableShowName */
87  inline bool isShowNameEnabled() const { return m_show_name; }
88  /** Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to
89  * this) */
91  /** Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to
92  * this) */
94  /** Set the 3D pose from a mrpt::math::TPose3D object (return a ref to
95  * this) */
97  /** Set the 3D pose from a mrpt::math::TPose3D object (return a ref to
98  * this) */
100  /** Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to
101  * this) */
103  /** Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to
104  * this) */
106 
107  /** Returns the 3D pose of the object as TPose3D */
109  /** Returns a const ref to the 3D pose of the object as mrpt::poses::CPose3D
110  * (which explicitly contains the 3x3 rotation matrix) */
111  inline const mrpt::poses::CPose3D& getPoseRef() const { return m_pose; }
112  /** Changes the location of the object, keeping untouched the orientation
113  * \return a ref to this */
114  inline CRenderizable& setLocation(double x, double y, double z)
115  {
116  m_pose.x(x);
117  m_pose.y(y);
118  m_pose.z(z);
119  return *this;
120  }
121 
122  /** Changes the location of the object, keeping untouched the orientation
123  * \return a ref to this */
125  {
126  m_pose.x(p.x);
127  m_pose.y(p.y);
128  m_pose.z(p.z);
129  return *this;
130  }
131 
132  /** Translation relative to parent coordinate origin. */
133  inline double getPoseX() const { return m_pose.x(); }
134  /** Translation relative to parent coordinate origin. */
135  inline double getPoseY() const { return m_pose.y(); }
136  /** Translation relative to parent coordinate origin. */
137  inline double getPoseZ() const { return m_pose.z(); }
138  /** Rotation relative to parent coordinate origin, in **DEGREES**. */
139  inline double getPoseYaw() const
140  {
141  return mrpt::utils::RAD2DEG(m_pose.yaw());
142  }
143  /** Rotation relative to parent coordinate origin, in **DEGREES**. */
144  inline double getPosePitch() const
145  {
147  }
148  /** Rotation relative to parent coordinate origin, in **DEGREES**. */
149  inline double getPoseRoll() const
150  {
151  return mrpt::utils::RAD2DEG(m_pose.roll());
152  }
153  /** Rotation relative to parent coordinate origin, in radians. */
154  inline double getPoseYawRad() const { return m_pose.yaw(); }
155  /** Rotation relative to parent coordinate origin, in radians. */
156  inline double getPosePitchRad() const { return m_pose.pitch(); }
157  /** Rotation relative to parent coordinate origin, in radians. */
158  inline double getPoseRollRad() const { return m_pose.roll(); }
159  /** Color components in the range [0,1] */
160  inline double getColorR() const { return m_color.R / 255.; }
161  /** Color components in the range [0,1] */
162  inline double getColorG() const { return m_color.G / 255.; }
163  /** Color components in the range [0,1] */
164  inline double getColorB() const { return m_color.B / 255.; }
165  /** Color components in the range [0,1] */
166  inline double getColorA() const { return m_color.A / 255.; }
167  /** Color components in the range [0,255] */
168  inline uint8_t getColorR_u8() const { return m_color.R; }
169  /** Color components in the range [0,255] */
170  inline uint8_t getColorG_u8() const { return m_color.G; }
171  /** Color components in the range [0,255] */
172  inline uint8_t getColorB_u8() const { return m_color.B; }
173  /** Color components in the range [0,255] */
174  inline uint8_t getColorA_u8() const { return m_color.A; }
175  /**Color components in the range [0,1] \return a ref to this */
176  CRenderizable& setColorR(const double r)
177  {
178  return setColorR_u8(static_cast<uint8_t>(255 * r));
179  }
180  /**Color components in the range [0,1] \return a ref to this */
181  CRenderizable& setColorG(const double g)
182  {
183  return setColorG_u8(static_cast<uint8_t>(255 * g));
184  }
185  /**Color components in the range [0,1] \return a ref to this */
186  CRenderizable& setColorB(const double b)
187  {
188  return setColorB_u8(static_cast<uint8_t>(255 * b));
189  }
190  /**Color components in the range [0,1] \return a ref to this */
191  CRenderizable& setColorA(const double a)
192  {
193  return setColorA_u8(static_cast<uint8_t>(255 * a));
194  }
195  /**Color components in the range [0,255] \return a ref to this */
197  {
198  m_color.R = r;
199  return *this;
200  }
201  /**Color components in the range [0,255] \return a ref to this */
203  {
204  m_color.G = g;
205  return *this;
206  }
207  /**Color components in the range [0,255] \return a ref to this */
209  {
210  m_color.B = b;
211  return *this;
212  }
213  /**Color components in the range [0,255] \return a ref to this */
215  {
216  m_color.A = a;
217  return *this;
218  }
219 
220  /** Scale to apply to the object, in all three axes (default=1) \return a
221  * ref to this */
222  inline CRenderizable& setScale(float s)
223  {
225  return *this;
226  }
227  /** Scale to apply to the object in each axis (default=1) \return a ref to
228  * this */
229  inline CRenderizable& setScale(float sx, float sy, float sz)
230  {
231  m_scale_x = sx;
232  m_scale_y = sy;
233  m_scale_z = sz;
234  return *this;
235  }
236  /** Get the current scaling factor in one axis */
237  inline float getScaleX() const { return m_scale_x; }
238  /** Get the current scaling factor in one axis */
239  inline float getScaleY() const { return m_scale_y; }
240  /** Get the current scaling factor in one axis */
241  inline float getScaleZ() const { return m_scale_z; }
242  /** Returns the object color property as a TColorf */
244  {
246  }
247  /** Changes the default object color \return a ref to this */
249  {
250  return setColor_u8(
252  c.R * 255.f, c.G * 255.f, c.B * 255.f, c.A * 255.f));
253  }
254 
255  /** Set the color components of this object (R,G,B,Alpha, in the range 0-1)
256  * \return a ref to this */
257  inline CRenderizable& setColor(double R, double G, double B, double A = 1)
258  {
259  return setColor_u8(R * 255, G * 255, B * 255, A * 255);
260  }
261 
262  /** Returns the object color property as a TColor */
263  inline const mrpt::utils::TColor& getColor_u8() const { return m_color; }
264  /*** Changes the default object color \return a ref to this */
266 
267  /** Set the color components of this object (R,G,B,Alpha, in the range
268  * 0-255) \return a ref to this */
270  {
271  return setColor_u8(mrpt::utils::TColor(R, G, B, A));
272  }
273 
274  /** @} */
275 
276  /** Default constructor: */
277  CRenderizable();
278  virtual ~CRenderizable();
279 
280  /** Implements the rendering of 3D objects in each class derived from
281  * CRenderizable.
282  */
283  virtual void render() const = 0;
284 
285  /** Simulation of ray-trace, given a pose. Returns true if the ray
286  * effectively collisions with the object (returning the distance to the
287  * origin of the ray in "dist"), or false in other case. "dist" variable
288  * yields undefined behaviour when false is returned
289  */
290  virtual bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const;
291 
292  /** This method is safe for calling from within ::render() methods \sa
293  * renderTextBitmap, mrpt::opengl::gl_utils */
294  static void renderTextBitmap(const char* str, void* fontStyle);
295 
296  /** Return the exact width in pixels for a given string, as will be rendered
297  * by renderTextBitmap().
298  * \sa renderTextBitmap, mrpt::opengl::gl_utils
299  */
300  static int textBitmapWidth(
301  const std::string& str,
304 
305  /** Render a text message in the current rendering context, creating a
306  * glViewport in the way (do not call within ::render() methods)
307  * - Coordinates (x,y) are 2D pixels, starting at bottom-left of the
308  * viewport. Negative numbers will wrap to the opposite side of the viewport
309  * (e.g. x=-10 means 10px fromt the right).
310  * - The text color is defined by (color_r,color_g,color_b), each float
311  * numbers in the range [0,1].
312  * \sa renderTextBitmap, textBitmapWidth, mrpt::opengl::gl_utils
313  */
314  static void renderTextBitmap(
315  int screen_x, int screen_y, const std::string& str, float color_r = 1,
316  float color_g = 1, float color_b = 1,
319 
320  /** Evaluates the bounding box of this object (including possible children)
321  * in the coordinate frame of the object parent. */
322  virtual void getBoundingBox(
323  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const = 0;
324 
325  protected:
326  /** Checks glGetError and throws an exception if an error situation is found
327  */
328  static void checkOpenGLError();
329 
330  void writeToStreamRender(utils::CStream& out) const;
332 
333  /** Returns the lowest next free texture name (avoid using OpenGL's own
334  * function since we may call them from different threads and seem it's not
335  * cool). */
336  static unsigned int getNewTextureNumber();
337  static void releaseTextureName(unsigned int i);
338 };
339 /** A list of objects pointers, automatically managing memory free at
340  * destructor, and managing copies correctly. */
341 using CListOpenGLObjects = std::deque<CRenderizable::Ptr>;
342 
343 /** Applies a mrpt::poses::CPose3D transformation to the object. Note that this
344  * method doesn't <i>set</i> the pose to the given value, but <i>combines</i> it
345  * with the existing one.
346  * \sa setPose */
349 
350 } // end namespace
351 } // End of namespace
352 
353 #endif
double getColorA() const
Color components in the range [0,1].
void setName(const std::string &n)
Changes the name of the object.
Definition: CRenderizable.h:68
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
CRenderizable & setColorB(const double b)
Color components in the range [0,1].
double getPosePitch() const
Rotation relative to parent coordinate origin, in DEGREES.
void writeToStreamRender(utils::CStream &out) const
GLdouble GLdouble z
Definition: glext.h:3872
mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:132
float getScaleX() const
Get the current scaling factor in one axis.
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:30
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
virtual CRenderizable & setColorA_u8(const uint8_t a)
Color components in the range [0,255].
CRenderizable & setLocation(const mrpt::math::TPoint3D &p)
Changes the location of the object, keeping untouched the orientation.
std::deque< CRenderizable::Ptr > CListOpenGLObjects
A list of objects pointers, automatically managing memory free at destructor, and managing copies cor...
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Simulation of ray-trace, given a pose.
double getPosePitchRad() const
Rotation relative to parent coordinate origin, in radians.
GLenum GLsizei n
Definition: glext.h:5074
uint8_t getColorR_u8() const
Color components in the range [0,255].
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
virtual void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const =0
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:539
CRenderizable & setLocation(double x, double y, double z)
Changes the location of the object, keeping untouched the orientation.
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:533
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
double getPoseX() const
Translation relative to parent coordinate origin.
virtual CRenderizable & setColorR_u8(const uint8_t r)
Color components in the range [0,255].
CRenderizable & setColor(const mrpt::utils::TColorf &c)
Changes the default object color.
void enableShowName(bool showName=true)
Enables or disables showing the name of the object as a label when rendering.
Definition: CRenderizable.h:85
STL namespace.
double getPoseZ() const
Translation relative to parent coordinate origin.
CRenderizable & setColorA(const double a)
Color components in the range [0,1].
uint8_t getColorA_u8() const
Color components in the range [0,255].
double getPoseRoll() const
Rotation relative to parent coordinate origin, in DEGREES.
GLdouble s
Definition: glext.h:3676
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:57
A viewport within a COpenGLScene, containing a set of OpenGL objects to render.
CRenderizable & setColorR(const double r)
Color components in the range [0,1].
#define DEFINE_VIRTUAL_SERIALIZABLE(class_name)
This declaration must be inserted in virtual CSerializable classes definition:
unsigned char uint8_t
Definition: rptypes.h:41
float m_scale_x
Scale components to apply to the object (default=1)
Definition: CRenderizable.h:59
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
virtual CRenderizable & setColorB_u8(const uint8_t b)
Color components in the range [0,255].
double RAD2DEG(const double x)
Radians to degrees.
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:25
uint8_t getColorG_u8() const
Color components in the range [0,255].
CRenderizable & setColor_u8(uint8_t R, uint8_t G, uint8_t B, uint8_t A=255)
Set the color components of this object (R,G,B,Alpha, in the range 0-255)
virtual CRenderizable & setColorG_u8(const uint8_t g)
Color components in the range [0,255].
const GLubyte * c
Definition: glext.h:6313
bool isVisible() const
Is the object visible?
Definition: CRenderizable.h:71
CRenderizable & setScale(float sx, float sy, float sz)
Scale to apply to the object in each axis (default=1)
const mrpt::utils::TColor & getColor_u8() const
Returns the object color property as a TColor.
GLubyte g
Definition: glext.h:6279
A RGB color - 8bit.
Definition: TColor.h:25
GLubyte GLubyte b
Definition: glext.h:6279
const std::string & getName() const
Returns the name of the object.
Definition: CRenderizable.h:70
mrpt::utils::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:54
CRenderizable & setScale(float s)
Scale to apply to the object, in all three axes (default=1)
CRenderizable & setColor(double R, double G, double B, double A=1)
Set the color components of this object (R,G,B,Alpha, in the range 0-1)
void readFromStreamRender(utils::CStream &in)
GLsizei const GLchar ** string
Definition: glext.h:4101
A class used to store a 2D point.
Definition: CPoint2D.h:36
A class used to store a 3D point.
Definition: CPoint3D.h:32
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:545
double getPoseYawRad() const
Rotation relative to parent coordinate origin, in radians.
double getPoseY() const
Translation relative to parent coordinate origin.
static void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
static unsigned int getNewTextureNumber()
Returns the lowest next free texture name (avoid using OpenGL&#39;s own function since we may call them f...
void setVisibility(bool visible=true)
Set object visibility (default=true)
Definition: CRenderizable.h:76
static 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().
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
const float R
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
virtual CRenderizable & setColor_u8(const mrpt::utils::TColor &c)
double getPoseYaw() const
Rotation relative to parent coordinate origin, in DEGREES.
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
GLuint in
Definition: glext.h:7274
Lightweight 2D pose.
CRenderizable & setColorG(const double g)
Color components in the range [0,1].
A RGB color - floats in the range [0,1].
Definition: TColor.h:78
CRenderizable & setPose(const mrpt::poses::CPose3D &o)
Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
GLenum GLint GLint y
Definition: glext.h:3538
double getPoseRollRad() const
Rotation relative to parent coordinate origin, in radians.
const mrpt::poses::CPose3D & getPoseRef() const
Returns a const ref to the 3D pose of the object as mrpt::poses::CPose3D (which explicitly contains t...
float getScaleY() const
Get the current scaling factor in one axis.
static void renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
GLenum GLint x
Definition: glext.h:3538
double getColorB() const
Color components in the range [0,1].
Lightweight 3D point.
uint8_t getColorB_u8() const
Color components in the range [0,255].
bool m_visible
Is the object visible? (default=true)
Definition: CRenderizable.h:61
CRenderizable()
Default constructor:
virtual void render() const =0
Implements the rendering of 3D objects in each class derived from CRenderizable.
double getColorG() const
Color components in the range [0,1].
mrpt::math::TPose3D getPose() const
Returns the 3D pose of the object as TPose3D.
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GLfloat GLfloat p
Definition: glext.h:6305
float getScaleZ() const
Get the current scaling factor in one axis.
static void releaseTextureName(unsigned int i)
double getColorR() const
Color components in the range [0,1].
mrpt::utils::TColorf getColor() const
Returns the object color property as a TColorf.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019