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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020