Main MRPT website > C++ reference for MRPT 1.9.9
CRenderizable.cpp
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 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/opengl/CRenderizable.h> // Include these before windows.h!!
13 #include <mrpt/opengl/gl_utils.h>
14 #include <mrpt/utils/CStringList.h>
15 #include <mrpt/math/utils.h>
16 #include <mrpt/poses/CPoint3D.h>
17 #include <mrpt/poses/CPoint2D.h>
18 #include <mrpt/poses/CPose3D.h>
19 #include <mrpt/utils/CStream.h>
20 
21 #include <mutex>
22 
23 #include "opengl_internals.h"
24 
25 using namespace std;
26 using namespace mrpt;
27 using namespace mrpt::opengl;
28 using namespace mrpt::utils;
29 
31 
32 #define MAX_GL_TEXTURE_IDS 0x10000
33 #define MAX_GL_TEXTURE_IDS_MASK 0x0FFFF
34 
36 {
37  private:
39  : freeTextureNames(MAX_GL_TEXTURE_IDS, false),
40  next_free_texture(1) // 0 is a reserved number!!
41  {
42  }
43 
44  public:
45  std::vector<bool> freeTextureNames;
46  unsigned int next_free_texture;
47  std::recursive_mutex cs;
48 
50  {
51  static TOpenGLNameBooker dat;
52  return dat;
53  }
54 };
55 
56 // Default constructor:
57 CRenderizable::CRenderizable()
58  : m_name(),
59  m_show_name(false),
60  m_color(255, 255, 255, 255),
61  m_pose(),
62  m_scale_x(1),
63  m_scale_y(1),
64  m_scale_z(1),
65  m_visible(true)
66 {
67 }
68 
69 // Destructor:
71 /** Returns the lowest, free texture name.
72  */
74 {
76 
78 
79  std::lock_guard<std::recursive_mutex> lock(booker.cs);
80 
81  unsigned int ret = booker.next_free_texture;
82  unsigned int tries = 0;
83  while (ret != 0 && booker.freeTextureNames[ret])
84  {
85  ret++;
86  ret = ret % MAX_GL_TEXTURE_IDS_MASK;
87 
88  if (++tries >= MAX_GL_TEXTURE_IDS)
90  "Maximum number of textures (%u) excedeed! (are you deleting "
91  "them?)",
92  (unsigned int)MAX_GL_TEXTURE_IDS);
93  }
94 
95  booker.freeTextureNames[ret] = true; // mark as used.
96  booker.next_free_texture = ret + 1;
97  return ret;
98  MRPT_END
99 }
100 
102 {
104  std::lock_guard<std::recursive_mutex> lock(booker.cs);
105  booker.freeTextureNames[i] = false;
106  if (i < booker.next_free_texture)
107  booker.next_free_texture = i; // try to reuse texture numbers.
108  // "glDeleteTextures" seems not to be neeeded, since we do the reservation
109  // of texture names by our own.
110 }
111 
113 {
114  // MRPT 0.9.5 svn 2774 (Dec 14th 2011):
115  // Added support of versioning at this level of serialization too.
116  // Should have been done from the beginning, terrible mistake on my part.
117  // Now, the only solution is something as ugly as this:
118  //
119  // For reference: In the past this started as:
120  // out << m_name << (float)(m_color.R) << (float)(m_color.G) <<
121  // (float)(m_color.B) << (float)(m_color.A);
122  // ...
123 
124  const uint8_t serialization_version =
125  0; // can't be >31 (but it would be mad geting to that situation!)
126 
127  const bool all_scales_equal =
129  const bool all_scales_unity = (all_scales_equal && m_scale_x == 1.0f);
130 
131  const uint8_t magic_signature[2] = {
132  0xFF,
133  // bit7: fixed to 1 to mark this new header format
134  // bit6: whether the 3 scale{x,y,z} are equal to 1.0
135  // bit5: whether the 3 scale{x,y,z} are equal to each other
136  static_cast<uint8_t>(
137  serialization_version |
138  (all_scales_unity ? 0xC0 : (all_scales_equal ? 0xA0 : 0x80)))};
139 
140  out << magic_signature[0] << magic_signature[1];
141 
142  // "m_name"
143  const uint16_t nameLen = static_cast<uint16_t>(m_name.size());
144  out << nameLen;
145  if (nameLen) out.WriteBuffer(m_name.c_str(), m_name.size());
146 
147  // Color, as u8:
148  out << m_color.R << m_color.G << m_color.B << m_color.A;
149 
150  // the rest of fields:
151  out << (float)m_pose.x() << (float)m_pose.y() << (float)m_pose.z()
152  << (float)m_pose.yaw() << (float)m_pose.pitch() << (float)m_pose.roll();
153 
154  if (!all_scales_unity)
155  {
156  if (all_scales_equal)
157  out << m_scale_x;
158  else
159  out << m_scale_x << m_scale_y << m_scale_z;
160  }
161 
162  out << m_show_name << m_visible;
163 }
164 
166 {
167  // MRPT 0.9.5 svn 2774 (Dec 14th 2011):
168  // See comments in CRenderizable::writeToStreamRender() for the employed
169  // serialization mechanism.
170  //
171 
172  // Read signature:
173  union {
174  uint8_t magic_signature[2 + 2]; // (the extra 4 bytes will be used only
175  // for the old format)
176  uint32_t magic_signature_uint32; // So we can interpret the 4bytes
177  // above as a 32bit number cleanly.
178  };
179 
180  in >> magic_signature[0] >> magic_signature[1];
181 
182  const bool is_new_format =
183  (magic_signature[0] == 0xFF) && ((magic_signature[1] & 0x80) != 0);
184 
185  if (is_new_format)
186  {
187  // NEW FORMAT:
188  uint8_t serialization_version = (magic_signature[1] & 0x1F);
189  const bool all_scales_unity = ((magic_signature[1] & 0x40) != 0);
190  const bool all_scales_equal_but_not_unity =
191  ((magic_signature[1] & 0x20) != 0);
192 
193  switch (serialization_version)
194  {
195  case 0:
196  {
197  // "m_name"
198  uint16_t nameLen;
199  in >> nameLen;
200  m_name.resize(nameLen);
201  if (nameLen) in.ReadBuffer((void*)(&m_name[0]), m_name.size());
202 
203  // Color, as u8:
204  in >> m_color.R >> m_color.G >> m_color.B >> m_color.A;
205 
206  // the rest of fields:
207  float x, y, z, yaw, pitch, roll;
208  in >> x >> y >> z >> yaw >> pitch >> roll;
209  m_pose.x(x);
210  m_pose.y(y);
211  m_pose.z(z);
213 
214  if (all_scales_unity)
215  m_scale_x = m_scale_y = m_scale_z = 1;
216  else
217  {
218  if (all_scales_equal_but_not_unity)
219  {
220  in >> m_scale_x;
222  }
223  else
224  in >> m_scale_x >> m_scale_y >> m_scale_z;
225  }
226 
227  in >> m_show_name >> m_visible;
228  }
229  break;
230  default:
232  "Can't parse CRenderizable standard data field: corrupt "
233  "data stream or format in a newer MRPT format? "
234  "(serialization version=%u)",
235  static_cast<unsigned int>(serialization_version))
236  };
237  }
238  else
239  {
240  // OLD FORMAT:
241  // Was: in >> m_name;
242  // We already read 2 bytes from the string uint32_t length:
243  in >> magic_signature[2] >> magic_signature[3];
244  {
245  const uint32_t nameLen =
246  magic_signature_uint32; // *reinterpret_cast<const
247  // uint32_t*>(&magic_signature[0]);
248  m_name.resize(nameLen);
249  if (nameLen) in.ReadBuffer((void*)(&m_name[0]), m_name.size());
250  }
251 
252  float f;
253  float yaw_deg, pitch_deg, roll_deg;
254 
256  in >> col.R >> col.G >> col.B >> col.A;
258  col.R / 255, col.G / 255, col.B / 255,
259  col.A / 255); // For some stupid reason, colors were saved
260  // multiplied by 255... (facepalm)
261 
262  in >> f;
263  m_pose.x(f);
264  in >> f;
265  m_pose.y(f);
266  in >> f;
267  m_pose.z(f);
268  in >> yaw_deg;
269  in >> pitch_deg;
270  in >> f;
271  roll_deg = f;
272  // Version 2: Add scale vars:
273  // JL: Yes, this is a crappy hack since I forgot to enable versions
274  // here...what? :-P
275  if (f != 16.0f && f != 17.0f)
276  {
277  // Old version:
278  // "roll_deg" is the actual roll.
279  in >> m_show_name;
280  m_scale_x = m_scale_y = m_scale_z = 1; // Default values
281  }
282  else
283  {
284  // New version >=v2:
285  in >> roll_deg;
286  in >> m_show_name;
287 
288  // Scale data:
289  in >> m_scale_x >> m_scale_y >> m_scale_z;
290 
291  if (f == 17.0f) // version>=v3
292  in >> m_visible;
293  else
294  m_visible = true; // Default
295  }
296 
298  DEG2RAD(yaw_deg), DEG2RAD(pitch_deg), DEG2RAD(roll_deg));
299  }
300 }
301 
303 {
305 }
306 
307 /*--------------------------------------------------------------
308  setPose
309  ---------------------------------------------------------------*/
311 {
312  m_pose = o;
313  return *this;
314 }
316 {
318  return *this;
319 }
321 {
323  return *this;
324 }
326 {
328  return *this;
329 }
330 
331 /** Set the 3D pose from a mrpt::poses::CPose3D object */
333 {
334  m_pose.setFromValues(o.x(), o.y(), o.z(), 0, 0, 0);
335  return *this;
336 }
337 /** Set the 3D pose from a mrpt::poses::CPose3D object */
339 {
340  m_pose.setFromValues(o.x(), o.y(), 0, 0, 0, 0);
341  return *this;
342 }
343 
344 /*--------------------------------------------------------------
345  getPose
346  ---------------------------------------------------------------*/
348 {
349  return mrpt::math::TPose3D(m_pose);
350 }
351 
352 /*--------------------------------------------------------------
353  traceRay
354  ---------------------------------------------------------------*/
355 bool CRenderizable::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
356 {
358  MRPT_UNUSED_PARAM(dist);
359  return false;
360 }
361 
364 {
365  r->setPose(p + mrpt::poses::CPose3D(r->getPose()));
366  return r;
367 }
368 
370 {
371  m_color.R = c.R;
372  m_color.G = c.G;
373  m_color.B = c.B;
374  m_color.A = c.A;
375  return *this;
376 }
377 
378 /** This method is safe for calling from within ::render() methods \sa
379  * renderTextBitmap */
380 void CRenderizable::renderTextBitmap(const char* str, void* fontStyle)
381 {
382  gl_utils::renderTextBitmap(str, fontStyle);
383 }
384 
385 /** Return the exact width in pixels for a given string, as will be rendered by
386  * renderTextBitmap().
387  * \sa renderTextBitmap
388  */
390  const std::string& str, mrpt::opengl::TOpenGLFont font)
391 {
392  return gl_utils::textBitmapWidth(str, font);
393 }
#define MAX_GL_TEXTURE_IDS
#define MAX_GL_TEXTURE_IDS_MASK
#define IMPLEMENTS_VIRTUAL_SERIALIZABLE( class_name, base_class_name, NameSpace)
This must be inserted as implementation of some required members for virtual CSerializable classes:
#define DEG2RAD
Definition: bits.h:112
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:44
static void releaseTextureName(unsigned int i)
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:57
static unsigned int getNewTextureNumber()
Returns the lowest next free texture name (avoid using OpenGL's own function since we may call them f...
virtual CRenderizable & setColor_u8(const mrpt::utils::TColor &c)
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().
bool m_visible
Is the object visible? (default=true)
Definition: CRenderizable.h:61
void writeToStreamRender(utils::CStream &out) const
static void renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Simulation of ray-trace, given a pose.
static void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
float m_scale_x
Scale components to apply to the object (default=1)
Definition: CRenderizable.h:59
void readFromStreamRender(utils::CStream &in)
CRenderizable & setPose(const mrpt::poses::CPose3D &o)
Set the 3D pose from a mrpt::poses::CPose3D object (return a ref to this)
mrpt::math::TPose3D getPose() const
Returns the 3D pose of the object as TPose3D.
mrpt::utils::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:54
A class used to store a 2D point.
Definition: CPoint2D.h:37
A class used to store a 3D point.
Definition: CPoint3D.h:33
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle.
Definition: CPose2D.h:41
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:89
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:539
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:545
void setFromValues(const double x0, const double y0, const double z0, const double yaw=0, const double pitch=0, const double roll=0)
Set the pose from a 3D position (meters) and yaw/pitch/roll angles (radians) - This method recomputes...
Definition: CPose3D.cpp:248
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:533
void setYawPitchRoll(const double yaw_, const double pitch_, const double roll_)
Set the 3 angles of the 3D pose (in radians) - This method recomputes the internal rotation coordinat...
Definition: CPose3D.h:473
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
The virtual base class which provides a unified interface for all persistent objects in MRPT.
Definition: CSerializable.h:45
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:42
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:64
const GLubyte * c
Definition: glext.h:6313
GLenum GLint GLint y
Definition: glext.h:3538
GLuint in
Definition: glext.h:7274
GLenum GLint x
Definition: glext.h:3538
GLfloat GLfloat p
Definition: glext.h:6305
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
GLdouble GLdouble z
Definition: glext.h:3872
GLsizei const GLchar ** string
Definition: glext.h:4101
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:26
#define MRPT_START
Definition: mrpt_macros.h:425
#define MRPT_END
Definition: mrpt_macros.h:429
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
Definition: mrpt_macros.h:365
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: mrpt_macros.h:121
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().
Definition: gl_utils.cpp:298
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:140
void renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
Definition: gl_utils.cpp:255
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:16
mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:132
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned __int16 uint16_t
Definition: rptypes.h:44
unsigned __int32 uint32_t
Definition: rptypes.h:47
unsigned char uint8_t
Definition: rptypes.h:41
std::recursive_mutex cs
std::vector< bool > freeTextureNames
static TOpenGLNameBooker & instance()
unsigned int next_free_texture
Lightweight 2D pose.
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
A RGB color - 8bit.
Definition: TColor.h:26
A RGB color - floats in the range [0,1].
Definition: TColor.h:79



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST