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  // MRPT 1.9.9 (Aug 2019): Was
165  // union {
166  // uint8_t magic_signature[2 + 2];
167  // (the extra 4 bytes will be used only for the old format)
168  // uint32_t magic_signature_uint32;
169  // So we can interpret the 4bytes above as a 32bit number cleanly.
170  // };
171  // Get rid of the "old" serialization format to avoid using "union".
172 
173  uint8_t magic_signature[2];
174 
175  in >> magic_signature[0] >> magic_signature[1];
176 
177  const bool is_new_format =
178  (magic_signature[0] == 0xFF) && ((magic_signature[1] & 0x80) != 0);
179 
180  if (is_new_format)
181  {
182  // NEW FORMAT:
183  uint8_t serialization_version = (magic_signature[1] & 0x1F);
184  const bool all_scales_unity = ((magic_signature[1] & 0x40) != 0);
185  const bool all_scales_equal_but_not_unity =
186  ((magic_signature[1] & 0x20) != 0);
187 
188  switch (serialization_version)
189  {
190  case 0:
191  {
192  // "m_name"
193  uint16_t nameLen;
194  in >> nameLen;
195  m_name.resize(nameLen);
196  if (nameLen) in.ReadBuffer((void*)(&m_name[0]), m_name.size());
197 
198  // Color, as u8:
199  in >> m_color.R >> m_color.G >> m_color.B >> m_color.A;
200 
201  // the rest of fields:
202  float x, y, z, yaw, pitch, roll;
203  in >> x >> y >> z >> yaw >> pitch >> roll;
204  m_pose.x(x);
205  m_pose.y(y);
206  m_pose.z(z);
208 
209  if (all_scales_unity)
210  m_scale_x = m_scale_y = m_scale_z = 1;
211  else
212  {
213  if (all_scales_equal_but_not_unity)
214  {
215  in >> m_scale_x;
217  }
218  else
219  in >> m_scale_x >> m_scale_y >> m_scale_z;
220  }
221 
222  in >> m_show_name >> m_visible;
223  }
224  break;
225  default:
227  "Can't parse CRenderizable standard data field: corrupt "
228  "data stream or format in a newer MRPT format? "
229  "(serialization version=%u)",
230  static_cast<unsigned int>(serialization_version));
231  };
232  }
233  else
234  {
235  // OLD FORMAT:
236  THROW_EXCEPTION("Serialized object is too old! Unsupported format.");
237  }
238 }
239 
241 {
243 }
244 
245 /*--------------------------------------------------------------
246  setPose
247  ---------------------------------------------------------------*/
249 {
250  m_pose = o;
251  return *this;
252 }
254 {
256  return *this;
257 }
259 {
261  return *this;
262 }
264 {
266  return *this;
267 }
268 
269 /** Set the 3D pose from a mrpt::poses::CPose3D object */
271 {
272  m_pose.setFromValues(o.x(), o.y(), o.z(), 0, 0, 0);
273  return *this;
274 }
275 /** Set the 3D pose from a mrpt::poses::CPose3D object */
277 {
278  m_pose.setFromValues(o.x(), o.y(), 0, 0, 0, 0);
279  return *this;
280 }
281 
284 {
285  return false;
286 }
287 
290 {
291  r->setPose(p + mrpt::poses::CPose3D(r->getPose()));
292  return r;
293 }
294 
296 {
297  m_color.R = c.R;
298  m_color.G = c.G;
299  m_color.B = c.B;
300  m_color.A = c.A;
301  return *this;
302 }
303 
304 /** This method is safe for calling from within ::render() methods \sa
305  * renderTextBitmap */
306 void CRenderizable::renderTextBitmap(const char* str, void* fontStyle)
307 {
308  gl_utils::renderTextBitmap(str, fontStyle);
309 }
310 
311 /** Return the exact width in pixels for a given string, as will be rendered by
312  * renderTextBitmap().
313  * \sa renderTextBitmap
314  */
316  const std::string& str, mrpt::opengl::TOpenGLFont font)
317 {
318  return gl_utils::textBitmapWidth(str, font);
319 }
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:775
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
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:122
#define MAX_GL_TEXTURE_IDS
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:486
#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:552
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:546
STL namespace.
uint8_t B
Definition: TColor.h:46
uint8_t G
Definition: TColor.h:46
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:54
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:558
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:54
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:85
mrpt::vision::TStereoCalibResults out
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
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:265
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:24
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
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: 45d659fbb Tue Dec 10 18:21:14 2019 +0100 at mar dic 10 18:30:09 CET 2019