MRPT  1.9.9
CRenderizable.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
12 #include <mrpt/math/TPose3D.h>
13 #include <mrpt/math/utils.h>
14 #include <mrpt/opengl/CRenderizable.h> // Include these before windows.h!!
15 #include <mrpt/opengl/gl_utils.h>
16 #include <mrpt/poses/CPoint2D.h>
17 #include <mrpt/poses/CPoint3D.h>
18 #include <mrpt/poses/CPose3D.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 
30 
31 #define MAX_GL_TEXTURE_IDS 0x10000
32 #define MAX_GL_TEXTURE_IDS_MASK 0x0FFFF
33 
35 {
36  private:
37  TOpenGLNameBooker() : freeTextureNames(MAX_GL_TEXTURE_IDS, false) {}
38 
39  public:
40  std::vector<bool> freeTextureNames;
41  unsigned int next_free_texture{1};
42  std::recursive_mutex cs;
43 
45  {
46  static TOpenGLNameBooker dat;
47  return dat;
48  }
49 };
50 
51 // Default constructor:
52 CRenderizable::CRenderizable()
53  : m_name(),
54 
55  m_color(255, 255, 255, 255),
56  m_pose()
57 
58 {
59 }
60 
61 // Destructor:
63 /** Returns the lowest, free texture name.
64  */
66 {
68 
70 
71  std::lock_guard<std::recursive_mutex> lock(booker.cs);
72 
73  unsigned int ret = booker.next_free_texture;
74  unsigned int tries = 0;
75  while (ret != 0 && booker.freeTextureNames[ret])
76  {
77  ret++;
78  ret = ret % MAX_GL_TEXTURE_IDS_MASK;
79 
80  if (++tries >= MAX_GL_TEXTURE_IDS)
82  "Maximum number of textures (%u) excedeed! (are you deleting "
83  "them?)",
84  (unsigned int)MAX_GL_TEXTURE_IDS);
85  }
86 
87  booker.freeTextureNames[ret] = true; // mark as used.
88  booker.next_free_texture = ret + 1;
89  return ret;
90  MRPT_END
91 }
92 
94 {
96  std::lock_guard<std::recursive_mutex> lock(booker.cs);
97  booker.freeTextureNames[i] = false;
98  if (i < booker.next_free_texture)
99  booker.next_free_texture = i; // try to reuse texture numbers.
100  // "glDeleteTextures" seems not to be neeeded, since we do the reservation
101  // of texture names by our own.
102 }
103 
106 {
107  // MRPT 0.9.5 svn 2774 (Dec 14th 2011):
108  // Added support of versioning at this level of serialization too.
109  // Should have been done from the beginning, terrible mistake on my part.
110  // Now, the only solution is something as ugly as this:
111  //
112  // For reference: In the past this started as:
113  // out << m_name << (float)(m_color.R) << (float)(m_color.G) <<
114  // (float)(m_color.B) << (float)(m_color.A);
115  // ...
116 
117  const uint8_t serialization_version =
118  0; // can't be >31 (but it would be mad geting to that situation!)
119 
120  const bool all_scales_equal =
122  const bool all_scales_unity = (all_scales_equal && m_scale_x == 1.0f);
123 
124  const uint8_t magic_signature[2] = {
125  0xFF,
126  // bit7: fixed to 1 to mark this new header format
127  // bit6: whether the 3 scale{x,y,z} are equal to 1.0
128  // bit5: whether the 3 scale{x,y,z} are equal to each other
129  static_cast<uint8_t>(
130  serialization_version |
131  (all_scales_unity ? 0xC0 : (all_scales_equal ? 0xA0 : 0x80)))};
132 
133  out << magic_signature[0] << magic_signature[1];
134 
135  // "m_name"
136  const auto nameLen = static_cast<uint16_t>(m_name.size());
137  out << nameLen;
138  if (nameLen) out.WriteBuffer(m_name.c_str(), m_name.size());
139 
140  // Color, as u8:
141  out << m_color.R << m_color.G << m_color.B << m_color.A;
142 
143  // the rest of fields:
144  out << (float)m_pose.x() << (float)m_pose.y() << (float)m_pose.z()
145  << (float)m_pose.yaw() << (float)m_pose.pitch() << (float)m_pose.roll();
146 
147  if (!all_scales_unity)
148  {
149  if (all_scales_equal)
150  out << m_scale_x;
151  else
152  out << m_scale_x << m_scale_y << m_scale_z;
153  }
154 
155  out << m_show_name << m_visible;
156 }
157 
159 {
160  // MRPT 0.9.5 svn 2774 (Dec 14th 2011):
161  // See comments in CRenderizable::writeToStreamRender() for the employed
162  // serialization mechanism.
163  //
164 
165  // Read signature:
166  union {
167  uint8_t magic_signature[2 + 2]; // (the extra 4 bytes will be used only
168  // for the old format)
169  uint32_t magic_signature_uint32; // So we can interpret the 4bytes
170  // above as a 32bit number cleanly.
171  };
172 
173  in >> magic_signature[0] >> magic_signature[1];
174 
175  const bool is_new_format =
176  (magic_signature[0] == 0xFF) && ((magic_signature[1] & 0x80) != 0);
177 
178  if (is_new_format)
179  {
180  // NEW FORMAT:
181  uint8_t serialization_version = (magic_signature[1] & 0x1F);
182  const bool all_scales_unity = ((magic_signature[1] & 0x40) != 0);
183  const bool all_scales_equal_but_not_unity =
184  ((magic_signature[1] & 0x20) != 0);
185 
186  switch (serialization_version)
187  {
188  case 0:
189  {
190  // "m_name"
191  uint16_t nameLen;
192  in >> nameLen;
193  m_name.resize(nameLen);
194  if (nameLen) in.ReadBuffer((void*)(&m_name[0]), m_name.size());
195 
196  // Color, as u8:
197  in >> m_color.R >> m_color.G >> m_color.B >> m_color.A;
198 
199  // the rest of fields:
200  float x, y, z, yaw, pitch, roll;
201  in >> x >> y >> z >> yaw >> pitch >> roll;
202  m_pose.x(x);
203  m_pose.y(y);
204  m_pose.z(z);
206 
207  if (all_scales_unity)
208  m_scale_x = m_scale_y = m_scale_z = 1;
209  else
210  {
211  if (all_scales_equal_but_not_unity)
212  {
213  in >> m_scale_x;
215  }
216  else
217  in >> m_scale_x >> m_scale_y >> m_scale_z;
218  }
219 
220  in >> m_show_name >> m_visible;
221  }
222  break;
223  default:
225  "Can't parse CRenderizable standard data field: corrupt "
226  "data stream or format in a newer MRPT format? "
227  "(serialization version=%u)",
228  static_cast<unsigned int>(serialization_version));
229  };
230  }
231  else
232  {
233  // OLD FORMAT:
234  // Was: in >> m_name;
235  // We already read 2 bytes from the string uint32_t length:
236  in >> magic_signature[2] >> magic_signature[3];
237  {
238  const uint32_t nameLen =
239  magic_signature_uint32; // *reinterpret_cast<const
240  // uint32_t*>(&magic_signature[0]);
241  m_name.resize(nameLen);
242  if (nameLen) in.ReadBuffer((void*)(&m_name[0]), m_name.size());
243  }
244 
245  float f;
246  float yaw_deg, pitch_deg, roll_deg;
247 
248  mrpt::img::TColorf col;
249  in >> col.R >> col.G >> col.B >> col.A;
251  col.R / 255, col.G / 255, col.B / 255,
252  col.A / 255); // For some stupid reason, colors were saved
253  // multiplied by 255... (facepalm)
254 
255  in >> f;
256  m_pose.x(f);
257  in >> f;
258  m_pose.y(f);
259  in >> f;
260  m_pose.z(f);
261  in >> yaw_deg;
262  in >> pitch_deg;
263  in >> f;
264  roll_deg = f;
265  // Version 2: Add scale vars:
266  // JL: Yes, this is a crappy hack since I forgot to enable versions
267  // here...what? :-P
268  if (f != 16.0f && f != 17.0f)
269  {
270  // Old version:
271  // "roll_deg" is the actual roll.
272  in >> m_show_name;
273  m_scale_x = m_scale_y = m_scale_z = 1; // Default values
274  }
275  else
276  {
277  // New version >=v2:
278  in >> roll_deg;
279  in >> m_show_name;
280 
281  // Scale data:
282  in >> m_scale_x >> m_scale_y >> m_scale_z;
283 
284  if (f == 17.0f) // version>=v3
285  in >> m_visible;
286  else
287  m_visible = true; // Default
288  }
289 
291  DEG2RAD(yaw_deg), DEG2RAD(pitch_deg), DEG2RAD(roll_deg));
292  }
293 }
294 
296 {
298 }
299 
300 /*--------------------------------------------------------------
301  setPose
302  ---------------------------------------------------------------*/
304 {
305  m_pose = o;
306  return *this;
307 }
309 {
311  return *this;
312 }
314 {
316  return *this;
317 }
319 {
321  return *this;
322 }
323 
324 /** Set the 3D pose from a mrpt::poses::CPose3D object */
326 {
327  m_pose.setFromValues(o.x(), o.y(), o.z(), 0, 0, 0);
328  return *this;
329 }
330 /** Set the 3D pose from a mrpt::poses::CPose3D object */
332 {
333  m_pose.setFromValues(o.x(), o.y(), 0, 0, 0, 0);
334  return *this;
335 }
336 
339 {
340  return false;
341 }
342 
345 {
346  r->setPose(p + mrpt::poses::CPose3D(r->getPose()));
347  return r;
348 }
349 
351 {
352  m_color.R = c.R;
353  m_color.G = c.G;
354  m_color.B = c.B;
355  m_color.A = c.A;
356  return *this;
357 }
358 
359 /** This method is safe for calling from within ::render() methods \sa
360  * renderTextBitmap */
361 void CRenderizable::renderTextBitmap(const char* str, void* fontStyle)
362 {
363  gl_utils::renderTextBitmap(str, fontStyle);
364 }
365 
366 /** Return the exact width in pixels for a given string, as will be rendered by
367  * renderTextBitmap().
368  * \sa renderTextBitmap
369  */
371  const std::string& str, mrpt::opengl::TOpenGLFont font)
372 {
373  return gl_utils::textBitmapWidth(str, font);
374 }
void renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
Definition: gl_utils.cpp:272
#define IMPLEMENTS_VIRTUAL_SERIALIZABLE(class_name, base_class, NS)
This must be inserted as implementation of some required members for virtual CSerializable classes: ...
mrpt::math::TPose3D asTPose() const
Definition: CPose3D.cpp:765
static TOpenGLNameBooker & instance()
std::vector< bool > freeTextureNames
#define MRPT_START
Definition: exceptions.h:241
unsigned int next_free_texture
GLdouble GLdouble z
Definition: glext.h:3879
unsigned __int16 uint16_t
Definition: rptypes.h:47
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:123
#define MAX_GL_TEXTURE_IDS
double DEG2RAD(const double x)
Degrees to radians.
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Simulation of ray-trace, given a pose.
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:482
#define MAX_GL_TEXTURE_IDS_MASK
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:40
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:548
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:542
STL namespace.
uint8_t B
Definition: TColor.h:46
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CArchive.cpp:49
uint8_t G
Definition: TColor.h:46
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:54
unsigned char uint8_t
Definition: rptypes.h:44
float m_scale_x
Scale components to apply to the object (default=1)
Definition: CRenderizable.h:56
virtual CRenderizable & setColor_u8(const mrpt::img::TColor &c)
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:22
void writeToStreamRender(mrpt::serialization::CArchive &out) const
const GLubyte * c
Definition: glext.h:6406
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
GLsizei const GLchar ** string
Definition: glext.h:4116
A class used to store a 2D point.
Definition: CPoint2D.h:32
A class used to store a 3D point.
Definition: CPoint3D.h:31
std::recursive_mutex cs
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:554
uint8_t R
Definition: TColor.h:46
mrpt::img::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:51
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:53
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
static unsigned int getNewTextureNumber()
Returns the lowest next free texture name (avoid using OpenGL&#39;s own function since we may call them f...
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:39
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
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:315
void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:155
#define MRPT_END
Definition: exceptions.h:245
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
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:256
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:23
GLuint in
Definition: glext.h:7391
Lightweight 2D pose.
Definition: TPose2D.h:22
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
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:3542
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:3542
bool m_visible
Is the object visible? (default=true)
Definition: CRenderizable.h:58
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
unsigned __int32 uint32_t
Definition: rptypes.h:50
mrpt::math::TPose3D getPose() const
Returns the 3D pose of the object as TPose3D.
GLfloat GLfloat p
Definition: glext.h:6398
static void releaseTextureName(unsigned int i)
uint8_t A
Definition: TColor.h:46



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019