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-2018, 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/img/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::img::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 { return mrpt::RAD2DEG(m_pose.yaw()); }
140  /** Rotation relative to parent coordinate origin, in **DEGREES**. */
141  inline double getPosePitch() const { return mrpt::RAD2DEG(m_pose.pitch()); }
142  /** Rotation relative to parent coordinate origin, in **DEGREES**. */
143  inline double getPoseRoll() const { return mrpt::RAD2DEG(m_pose.roll()); }
144  /** Rotation relative to parent coordinate origin, in radians. */
145  inline double getPoseYawRad() const { return m_pose.yaw(); }
146  /** Rotation relative to parent coordinate origin, in radians. */
147  inline double getPosePitchRad() const { return m_pose.pitch(); }
148  /** Rotation relative to parent coordinate origin, in radians. */
149  inline double getPoseRollRad() const { return m_pose.roll(); }
150  /** Color components in the range [0,1] */
151  inline double getColorR() const { return m_color.R / 255.; }
152  /** Color components in the range [0,1] */
153  inline double getColorG() const { return m_color.G / 255.; }
154  /** Color components in the range [0,1] */
155  inline double getColorB() const { return m_color.B / 255.; }
156  /** Color components in the range [0,1] */
157  inline double getColorA() const { return m_color.A / 255.; }
158  /** Color components in the range [0,255] */
159  inline uint8_t getColorR_u8() const { return m_color.R; }
160  /** Color components in the range [0,255] */
161  inline uint8_t getColorG_u8() const { return m_color.G; }
162  /** Color components in the range [0,255] */
163  inline uint8_t getColorB_u8() const { return m_color.B; }
164  /** Color components in the range [0,255] */
165  inline uint8_t getColorA_u8() const { return m_color.A; }
166  /**Color components in the range [0,1] \return a ref to this */
167  CRenderizable& setColorR(const double r)
168  {
169  return setColorR_u8(static_cast<uint8_t>(255 * r));
170  }
171  /**Color components in the range [0,1] \return a ref to this */
172  CRenderizable& setColorG(const double g)
173  {
174  return setColorG_u8(static_cast<uint8_t>(255 * g));
175  }
176  /**Color components in the range [0,1] \return a ref to this */
177  CRenderizable& setColorB(const double b)
178  {
179  return setColorB_u8(static_cast<uint8_t>(255 * b));
180  }
181  /**Color components in the range [0,1] \return a ref to this */
182  CRenderizable& setColorA(const double a)
183  {
184  return setColorA_u8(static_cast<uint8_t>(255 * a));
185  }
186  /**Color components in the range [0,255] \return a ref to this */
188  {
189  m_color.R = r;
190  return *this;
191  }
192  /**Color components in the range [0,255] \return a ref to this */
194  {
195  m_color.G = g;
196  return *this;
197  }
198  /**Color components in the range [0,255] \return a ref to this */
200  {
201  m_color.B = b;
202  return *this;
203  }
204  /**Color components in the range [0,255] \return a ref to this */
206  {
207  m_color.A = a;
208  return *this;
209  }
210 
211  /** Scale to apply to the object, in all three axes (default=1) \return a
212  * ref to this */
213  inline CRenderizable& setScale(float s)
214  {
216  return *this;
217  }
218  /** Scale to apply to the object in each axis (default=1) \return a ref to
219  * this */
220  inline CRenderizable& setScale(float sx, float sy, float sz)
221  {
222  m_scale_x = sx;
223  m_scale_y = sy;
224  m_scale_z = sz;
225  return *this;
226  }
227  /** Get the current scaling factor in one axis */
228  inline float getScaleX() const { return m_scale_x; }
229  /** Get the current scaling factor in one axis */
230  inline float getScaleY() const { return m_scale_y; }
231  /** Get the current scaling factor in one axis */
232  inline float getScaleZ() const { return m_scale_z; }
233  /** Returns the object color property as a TColorf */
235  {
236  return mrpt::img::TColorf(m_color);
237  }
238  /** Changes the default object color \return a ref to this */
240  {
241  return setColor_u8(
243  c.R * 255.f, c.G * 255.f, c.B * 255.f, c.A * 255.f));
244  }
245 
246  /** Set the color components of this object (R,G,B,Alpha, in the range 0-1)
247  * \return a ref to this */
248  inline CRenderizable& setColor(double R, double G, double B, double A = 1)
249  {
250  return setColor_u8(R * 255, G * 255, B * 255, A * 255);
251  }
252 
253  /** Returns the object color property as a TColor */
254  inline const mrpt::img::TColor& getColor_u8() const { return m_color; }
255  /*** Changes the default object color \return a ref to this */
256  virtual CRenderizable& setColor_u8(const mrpt::img::TColor& c);
257 
258  /** Set the color components of this object (R,G,B,Alpha, in the range
259  * 0-255) \return a ref to this */
261  {
262  return setColor_u8(mrpt::img::TColor(R, G, B, A));
263  }
264 
265  /** @} */
266 
267  /** Default constructor: */
268  CRenderizable();
269  virtual ~CRenderizable();
270 
271  /** Implements the rendering of 3D objects in each class derived from
272  * CRenderizable.
273  */
274  virtual void render() const = 0;
275 
276  /** Simulation of ray-trace, given a pose. Returns true if the ray
277  * effectively collisions with the object (returning the distance to the
278  * origin of the ray in "dist"), or false in other case. "dist" variable
279  * yields undefined behaviour when false is returned
280  */
281  virtual bool traceRay(const mrpt::poses::CPose3D& o, double& dist) const;
282 
283  /** This method is safe for calling from within ::render() methods \sa
284  * renderTextBitmap, mrpt::opengl::gl_utils */
285  static void renderTextBitmap(const char* str, void* fontStyle);
286 
287  /** Return the exact width in pixels for a given string, as will be rendered
288  * by renderTextBitmap().
289  * \sa renderTextBitmap, mrpt::opengl::gl_utils
290  */
291  static int textBitmapWidth(
292  const std::string& str,
295 
296  /** Render a text message in the current rendering context, creating a
297  * glViewport in the way (do not call within ::render() methods)
298  * - Coordinates (x,y) are 2D pixels, starting at bottom-left of the
299  * viewport. Negative numbers will wrap to the opposite side of the viewport
300  * (e.g. x=-10 means 10px fromt the right).
301  * - The text color is defined by (color_r,color_g,color_b), each float
302  * numbers in the range [0,1].
303  * \sa renderTextBitmap, textBitmapWidth, mrpt::opengl::gl_utils
304  */
305  static void renderTextBitmap(
306  int screen_x, int screen_y, const std::string& str, float color_r = 1,
307  float color_g = 1, float color_b = 1,
310 
311  /** Evaluates the bounding box of this object (including possible children)
312  * in the coordinate frame of the object parent. */
313  virtual void getBoundingBox(
314  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const = 0;
315 
316  protected:
317  /** Checks glGetError and throws an exception if an error situation is found
318  */
319  static void checkOpenGLError();
320 
323 
324  /** Returns the lowest next free texture name (avoid using OpenGL's own
325  * function since we may call them from different threads and seem it's not
326  * cool). */
327  static unsigned int getNewTextureNumber();
328  static void releaseTextureName(unsigned int i);
329 };
330 /** A list of objects pointers, automatically managing memory free at
331  * destructor, and managing copies correctly. */
332 using CListOpenGLObjects = std::deque<CRenderizable::Ptr>;
333 
334 /** Applies a mrpt::poses::CPose3D transformation to the object. Note that this
335  * method doesn't <i>set</i> the pose to the given value, but <i>combines</i> it
336  * with the existing one.
337  * \sa setPose */
340 
341 } // namespace opengl
342 } // namespace mrpt
343 
344 #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:140
CRenderizable & setColorB(const double b)
Color components in the range [0,1].
double getPosePitch() const
Rotation relative to parent coordinate origin, in DEGREES.
GLdouble GLdouble z
Definition: glext.h:3872
double RAD2DEG(const double x)
Radians to degrees.
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:28
virtual CRenderizable & setColorA_u8(const uint8_t a)
Color components in the range [0,255].
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:130
CRenderizable & setLocation(const mrpt::math::TPoint3D &p)
Changes the location of the object, keeping untouched the orientation.
const double G
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:534
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:528
double getPoseX() const
Translation relative to parent coordinate origin.
virtual CRenderizable & setColorR_u8(const uint8_t r)
Color components in the range [0,255].
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.
uint8_t B
Definition: TColor.h:48
uint8_t G
Definition: TColor.h:48
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].
const mrpt::img::TColor & getColor_u8() const
Returns the object color property as a TColor.
#define DEFINE_VIRTUAL_SERIALIZABLE(class_name)
This declaration must be inserted in virtual CSerializable classes definition:
unsigned char uint8_t
Definition: rptypes.h:41
CRenderizable & setColor(const mrpt::img::TColorf &c)
Changes the default object color.
float m_scale_x
Scale components to apply to the object (default=1)
Definition: CRenderizable.h:59
virtual CRenderizable & setColorB_u8(const uint8_t b)
Color components in the range [0,255].
virtual CRenderizable & setColor_u8(const mrpt::img::TColor &c)
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:25
void writeToStreamRender(mrpt::serialization::CArchive &out) const
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
GLint GLvoid * img
Definition: glext.h:3763
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)
GLubyte g
Definition: glext.h:6279
GLubyte GLubyte b
Definition: glext.h:6279
const std::string & getName() const
Returns the name of the object.
Definition: CRenderizable.h:70
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)
GLsizei const GLchar ** string
Definition: glext.h:4101
A class used to store a 2D point.
Definition: CPoint2D.h:35
A class used to store a 3D point.
Definition: CPoint3D.h:33
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:540
uint8_t R
Definition: TColor.h:48
double getPoseYawRad() const
Rotation relative to parent coordinate origin, in radians.
mrpt::img::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:54
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.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
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
double getPoseYaw() const
Rotation relative to parent coordinate origin, in DEGREES.
mrpt::img::TColorf getColor() const
Returns the object color property as a TColorf.
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
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].
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:32
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.
void readFromStreamRender(mrpt::serialization::CArchive &in)
static void renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
A RGB color - 8bit.
Definition: TColor.h:22
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)
uint8_t A
Definition: TColor.h:48
double getColorR() const
Color components in the range [0,1].



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019