Main MRPT website > C++ reference for MRPT 1.5.7
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>
15 #include <mrpt/utils/CStringList.h>
16 #include <mrpt/math/utils.h>
17 #include <mrpt/poses/CPoint3D.h>
18 #include <mrpt/poses/CPoint2D.h>
19 #include <mrpt/poses/CPose3D.h>
20 #include <mrpt/utils/CStream.h>
21 
22 #include "opengl_internals.h"
23 
24 using namespace std;
25 using namespace mrpt;
26 using namespace mrpt::opengl;
27 using namespace mrpt::utils;
28 using namespace mrpt::synch;
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  cs()
42  {
43  }
44 
45 public:
46  std::vector<bool> freeTextureNames;
47  unsigned int next_free_texture;
49 
51  {
52  static TOpenGLNameBooker dat;
53  return dat;
54  }
55 };
56 
57 // Default constructor:
58 CRenderizable::CRenderizable() :
59  m_name(),
60  m_show_name(false),
61  m_color(255,255,255,255),
62  m_pose(),
63  m_scale_x(1), m_scale_y(1), m_scale_z(1),
64  m_visible(true)
65 {
66 }
67 
68 // Destructor:
70 {
71 }
72 
73 
74 
75 /** Returns the lowest, free texture name.
76  */
78 {
80 
82 
83  CCriticalSectionLocker lock ( &booker.cs );
84 
85  unsigned int ret = booker.next_free_texture;
86  unsigned int tries = 0;
87  while (ret!=0 && booker.freeTextureNames[ret])
88  {
89  ret++;
90  ret = ret % MAX_GL_TEXTURE_IDS_MASK;
91 
92  if (++tries>=MAX_GL_TEXTURE_IDS)
93  THROW_EXCEPTION_FMT("Maximum number of textures (%u) excedeed! (are you deleting them?)", (unsigned int)MAX_GL_TEXTURE_IDS);
94  }
95 
96  booker.freeTextureNames[ret] = true; // mark as used.
97  booker.next_free_texture = ret+1;
98  return ret;
99  MRPT_END
100 }
101 
103 {
105  CCriticalSectionLocker lock ( &booker.cs );
106  booker.freeTextureNames[i] = false;
107  if (i<booker.next_free_texture) booker.next_free_texture = i; // try to reuse texture numbers.
108  // "glDeleteTextures" seems not to be neeeded, since we do the reservation of texture names by our own.
109 }
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) << (float)(m_color.B) << (float)(m_color.A);
121  // ...
122 
123  const uint8_t serialization_version = 0; // can't be >31 (but it would be mad geting to that situation!)
124 
125  const bool all_scales_equal = (m_scale_x==m_scale_y && m_scale_z==m_scale_x);
126  const bool all_scales_unity = (all_scales_equal && m_scale_x==1.0f);
127 
128  const uint8_t magic_signature[2] = {
129  0xFF,
130  // bit7: fixed to 1 to mark this new header format
131  // bit6: whether the 3 scale{x,y,z} are equal to 1.0
132  // bit5: whether the 3 scale{x,y,z} are equal to each other
133  static_cast<uint8_t>( serialization_version | (all_scales_unity ? 0xC0 : (all_scales_equal ? 0xA0 : 0x80) ) )
134  };
135 
136  out << magic_signature[0] << magic_signature[1];
137 
138  // "m_name"
139  const uint16_t nameLen = static_cast<uint16_t>(m_name.size());
140  out << nameLen;
141  if (nameLen) out.WriteBuffer(m_name.c_str(),m_name.size());
142 
143  // Color, as u8:
144  out << m_color.R << m_color.G << m_color.B << m_color.A;
145 
146  // the rest of fields:
147  out << (float)m_pose.x() << (float)m_pose.y() << (float)m_pose.z()
148  << (float)m_pose.yaw() << (float)m_pose.pitch() << (float)m_pose.roll();
149 
150  if (!all_scales_unity)
151  {
152  if (all_scales_equal)
153  out << m_scale_x;
154  else out << m_scale_x << m_scale_y << m_scale_z;
155  }
156 
157  out << m_show_name
158  << m_visible;
159 }
160 
162 {
163  // MRPT 0.9.5 svn 2774 (Dec 14th 2011):
164  // See comments in CRenderizable::writeToStreamRender() for the employed serialization mechanism.
165  //
166 
167  // Read signature:
168  union {
169  uint8_t magic_signature[2+2]; // (the extra 4 bytes will be used only for the old format)
170  uint32_t magic_signature_uint32; // So we can interpret the 4bytes above as a 32bit number cleanly.
171  };
172 
173  in >> magic_signature[0] >> magic_signature[1];
174 
175  const bool is_new_format = (magic_signature[0]==0xFF) && ((magic_signature[1]&0x80)!=0);
176 
177  if (is_new_format)
178  {
179  // NEW FORMAT:
180  uint8_t serialization_version = (magic_signature[1] & 0x1F);
181  const bool all_scales_unity = ((magic_signature[1]&0x40)!=0);
182  const bool all_scales_equal_but_not_unity = ((magic_signature[1]&0x20)!=0);
183 
184  switch(serialization_version)
185  {
186  case 0:
187  {
188  // "m_name"
189  uint16_t nameLen;
190  in >> nameLen;
191  m_name.resize(nameLen);
192  if (nameLen) in.ReadBuffer((void*)(&m_name[0]),m_name.size());
193 
194  // Color, as u8:
195  in >> m_color.R >> m_color.G >> m_color.B >> m_color.A;
196 
197  // the rest of fields:
198  float x,y,z,yaw,pitch,roll;
199  in >> x >> y >> z >> yaw >> pitch >> roll;
200  m_pose.x(x); m_pose.y(y); m_pose.z(z);
202 
203  if (all_scales_unity)
205  else {
206  if (all_scales_equal_but_not_unity)
207  {
208  in >> m_scale_x;
210  }
211  else in >> m_scale_x >> m_scale_y >> m_scale_z;
212  }
213 
214  in >> m_show_name
215  >> m_visible;
216  }
217  break;
218  default:
219  THROW_EXCEPTION_FMT("Can't parse CRenderizable standard data field: corrupt data stream or format in a newer MRPT format? (serialization version=%u)",static_cast<unsigned int>(serialization_version))
220  };
221  }
222  else
223  {
224  // OLD FORMAT:
225  //Was: in >> m_name;
226  // We already read 2 bytes from the string uint32_t length:
227  in >> magic_signature[2] >> magic_signature[3];
228  {
229  const uint32_t nameLen = magic_signature_uint32; // *reinterpret_cast<const uint32_t*>(&magic_signature[0]);
230  m_name.resize(nameLen);
231  if (nameLen)
232  in.ReadBuffer((void*)(&m_name[0]),m_name.size());
233  }
234 
235  float f;
236  float yaw_deg,pitch_deg,roll_deg;
237 
239  in >> col.R >> col.G >> col.B >> col.A;
240  m_color = mrpt::utils::TColor(col.R/255,col.G/255,col.B/255,col.A/255); // For some stupid reason, colors were saved multiplied by 255... (facepalm)
241 
242  in >> f; m_pose.x(f);
243  in >> f; m_pose.y(f);
244  in >> f; m_pose.z(f);
245  in >> yaw_deg;
246  in >> pitch_deg;
247  in >> f; roll_deg = f;
248  // Version 2: Add scale vars:
249  // JL: Yes, this is a crappy hack since I forgot to enable versions here...what? :-P
250  if (f!=16.0f && f!=17.0f)
251  {
252  // Old version:
253  // "roll_deg" is the actual roll.
254  in >> m_show_name;
255  m_scale_x=m_scale_y=m_scale_z=1; // Default values
256  }
257  else
258  {
259  // New version >=v2:
260  in >> roll_deg;
261  in >> m_show_name;
262 
263  // Scale data:
264  in >> m_scale_x >> m_scale_y >> m_scale_z;
265 
266  if (f==17.0f) // version>=v3
267  in >>m_visible;
268  else
269  m_visible = true; // Default
270  }
271 
272  m_pose.setYawPitchRoll( DEG2RAD(yaw_deg),DEG2RAD(pitch_deg),DEG2RAD(roll_deg) );
273  }
274 }
275 
276 
278 {
280 }
281 
282 /*--------------------------------------------------------------
283  setPose
284  ---------------------------------------------------------------*/
286 {
287  m_pose = o;
288  return *this;
289 }
291 {
293  return *this;
294 }
296 {
298  return *this;
299 }
301 {
303  return *this;
304 }
305 
306 CRenderizable& CRenderizable::setPose( const mrpt::poses::CPoint3D &o ) //!< Set the 3D pose from a mrpt::poses::CPose3D object
307 {
308  m_pose.setFromValues(o.x(), o.y(), o.z(), 0,0,0 );
309  return *this;
310 }
311 CRenderizable& CRenderizable::setPose( const mrpt::poses::CPoint2D &o ) //!< Set the 3D pose from a mrpt::poses::CPose3D object
312 {
313  m_pose.setFromValues(o.x(), o.y(),0, 0,0,0 );
314  return *this;
315 }
316 
317 
318 /*--------------------------------------------------------------
319  getPose
320  ---------------------------------------------------------------*/
322 {
323  return mrpt::math::TPose3D(m_pose);
324 }
325 
326 /*--------------------------------------------------------------
327  traceRay
328  ---------------------------------------------------------------*/
329 bool CRenderizable::traceRay(const mrpt::poses::CPose3D &o,double &dist) const {
331  return false;
332 }
333 
334 CRenderizablePtr &mrpt::opengl::operator<<(CRenderizablePtr &r,const mrpt::poses::CPose3D &p) {
335  r->setPose(p+mrpt::poses::CPose3D(r->getPose()));
336  return r;
337 }
338 
340 {
341  m_color.R = c.R;
342  m_color.G = c.G;
343  m_color.B = c.B;
344  m_color.A = c.A;
345  return *this;
346 }
347 
348 
349 
350 /** This method is safe for calling from within ::render() methods \sa renderTextBitmap */
351 void CRenderizable::renderTextBitmap( const char *str, void *fontStyle )
352 {
353  gl_utils::renderTextBitmap(str,fontStyle);
354 }
355 
356 /** Return the exact width in pixels for a given string, as will be rendered by renderTextBitmap().
357  * \sa renderTextBitmap
358  */
360  const std::string &str,
362 {
363  return gl_utils::textBitmapWidth(str,font);
364 }
Recursive mutex: allow recursive locks by the owner thread.
void OPENGL_IMPEXP renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
Definition: gl_utils.cpp:238
static TOpenGLNameBooker & instance()
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:113
A class acquiring a CCriticalSection at its constructor, and releasing it at destructor.
float m_scale_z
Scale components to apply to the object (default=1)
Definition: CRenderizable.h:56
std::vector< bool > freeTextureNames
void writeToStreamRender(utils::CStream &out) const
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
unsigned int next_free_texture
GLdouble GLdouble z
Definition: glext.h:3734
OPENGL_IMPEXP mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
Definition: CLight.cpp:132
unsigned __int16 uint16_t
Definition: rptypes.h:46
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
#define MAX_GL_TEXTURE_IDS
virtual bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Simulation of ray-trace, given a pose.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
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:347
#define MAX_GL_TEXTURE_IDS_MASK
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:67
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:44
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:392
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:391
STL namespace.
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference. This class automatically holds the cached 3x3 rotation m...
Definition: CRenderizable.h:55
unsigned char uint8_t
Definition: rptypes.h:43
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
#define IMPLEMENTS_VIRTUAL_SERIALIZABLE(class_name, base_class_name, NameSpace)
This must be inserted as implementation of some required members for virtual CSerializable classes: ...
TOpenGLFont
Existing fonts for 2D texts in mrpt::opengl methods.
Definition: opengl_fonts.h:26
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
const GLubyte * c
Definition: glext.h:5590
A RGB color - 8bit.
Definition: TColor.h:26
mrpt::utils::TColor m_color
Color components in the range [0,255].
Definition: CRenderizable.h:54
This namespace provides multitask, synchronization utilities.
#define DEG2RAD
void readFromStreamRender(utils::CStream &in)
GLsizei const GLchar ** string
Definition: glext.h:3919
A class used to store a 2D point.
Definition: CPoint2D.h:36
A class used to store a 3D point.
Definition: CPoint3D.h:32
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:393
#define MRPT_START
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.
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
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:36
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
virtual CRenderizable & setColor_u8(const mrpt::utils::TColor &c)
int OPENGL_IMPEXP 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:270
void OPENGL_IMPEXP checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
Definition: gl_utils.cpp:134
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:254
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
GLuint in
Definition: glext.h:6301
Lightweight 2D pose.
The namespace for 3D scene representation and rendering.
A RGB color - floats in the range [0,1].
Definition: TColor.h:80
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:3516
static void renderTextBitmap(const char *str, void *fontStyle)
This method is safe for calling from within ::render() methods.
GLenum GLint x
Definition: glext.h:3516
bool m_visible
Is the object visible? (default=true)
Definition: CRenderizable.h:57
unsigned __int32 uint32_t
Definition: rptypes.h:49
mrpt::math::TPose3D getPose() const
Returns the 3D pose of the object as TPose3D.
synch::CCriticalSectionRecursive cs
GLfloat GLfloat p
Definition: glext.h:5587
static void releaseTextureName(unsigned int i)



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019