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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST