Main MRPT website > C++ reference for MRPT 1.5.7
COpenGLScene.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 
16 #include <mrpt/utils/CStringList.h>
18 #include <mrpt/utils/CStream.h>
19 
22 
23 #include "opengl_internals.h"
24 
25 using namespace mrpt;
26 using namespace mrpt::opengl;
27 using namespace mrpt::utils;
28 using namespace mrpt::math;
29 using namespace std;
30 
31 // Include libraries in linking:
32 #if MRPT_HAS_OPENGL_GLUT
33  #ifdef MRPT_OS_WINDOWS
34  // WINDOWS:
35  #if defined(_MSC_VER) || defined(__BORLANDC__)
36  #pragma comment (lib,"opengl32.lib")
37  #pragma comment (lib,"GlU32.lib")
38  #endif
39  #endif // MRPT_OS_WINDOWS
40 #endif // MRPT_HAS_OPENGL_GLUT
41 
42 
44 
45 
46 /*---------------------------------------------------------------
47  Constructor
48 ---------------------------------------------------------------*/
50  m_followCamera(false)
51 {
52  createViewport("main");
53 }
54 
55 /*--------------------------------------------------------------
56  Copy constructor
57  ---------------------------------------------------------------*/
60 {
61  (*this) = obj;
62 }
63 
64 /*---------------------------------------------------------------
65  Destructor:
66  ---------------------------------------------------------------*/
68 {
69  clear(false);
70 }
71 
72 /*---------------------------------------------------------------
73  Clear the scene.
74  ---------------------------------------------------------------*/
75 void COpenGLScene::clear( bool createMainViewport )
76 {
77  m_viewports.clear();
78 
79  if (createMainViewport)
80  createViewport("main");
81 }
82 
83 /*---------------------------------------------------------------
84  =
85  ---------------------------------------------------------------*/
87 {
88  if (this != &obj)
89  {
90  m_followCamera = obj.m_followCamera;
91 
92  clear();
93  m_viewports = obj.m_viewports;
94  for_each(m_viewports.begin(),m_viewports.end(), metaprogramming::ObjectMakeUnique() );
95  }
96  return *this;
97 }
98 
99 /*---------------------------------------------------------------
100  render
101  ---------------------------------------------------------------*/
103 {
104  MRPT_START
105 
106 #if MRPT_HAS_OPENGL_GLUT
107  // We need the size of the viewport at the beginning: should be the whole window:
108  GLint win_dims[4];
109  glGetIntegerv( GL_VIEWPORT, win_dims );
110 
111  for (TListViewports::const_iterator it=m_viewports.begin();it!=m_viewports.end();++it)
112  (*it)->render( win_dims[2],win_dims[3] );
113 
114  // Assure we restore the original viewport:
115  glViewport( win_dims[0],win_dims[1],win_dims[2],win_dims[3] );
116 
117 #else
118  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENGL_GLUT=0! OpenGL functions are not implemented");
119 #endif
120  MRPT_END
121 }
122 
123 /*---------------------------------------------------------------
124  Implements the writing to a CStream capability of
125  CSerializable objects
126  ---------------------------------------------------------------*/
128 {
129  if (version)
130  *version = 1;
131  else
132  {
133  out << m_followCamera;
134 
135  uint32_t n;
136  n = (uint32_t)m_viewports.size();
137  out << n;
138  for (TListViewports::const_iterator it=m_viewports.begin();it!=m_viewports.end();++it)
139  out << **it;
140  }
141 }
142 
143 /*---------------------------------------------------------------
144  Implements the reading from a CStream capability of
145  CSerializable objects
146  ---------------------------------------------------------------*/
148 {
149  switch(version)
150  {
151  case 0:
152  {
153  // Old style: Just one viewport:
154  clear(true);
155  COpenGLViewportPtr view = m_viewports[0];
156 
157  // Load objects:
158  uint32_t n;
159  in >> n;
160 
161  view->clear();
162  view->m_objects.resize(n);
163  for_each(view->m_objects.begin(), view->m_objects.end(), metaprogramming::ObjectReadFromStream(&in) );
164  }
165  break;
166  case 1:
167  {
168  in >> m_followCamera;
169 
170  uint32_t i,n;
171  in >> n;
172  clear(false);
173 
174 
175  for (i=0;i<n;i++)
176  {
177  CSerializablePtr newObj;
178  in >> newObj;
179 
180  COpenGLViewportPtr newView = COpenGLViewportPtr(newObj);
181  newView->m_parent = this;
182  m_viewports.push_back( newView );
183  }
184 
185  } break;
186  default:
188 
189  };
190 }
191 
192 /*---------------------------------------------------------------
193  insert
194  ---------------------------------------------------------------*/
195 void COpenGLScene::insert( const CRenderizablePtr &newObject, const std::string &viewportName )
196 {
197  MRPT_START
198  for (TListViewports::iterator it = m_viewports.begin();it!= m_viewports.end();++it)
199  {
200  if ((*it)->m_name == viewportName )
201  {
202  (*it)->insert(newObject);
203  return;
204  }
205  }
206  THROW_EXCEPTION_FMT("Error: viewport '%s' not found.",viewportName.c_str());
207  MRPT_END
208 }
209 
210 /*---------------------------------------------------------------
211  getByName
212  ---------------------------------------------------------------*/
213 CRenderizablePtr COpenGLScene::getByName( const string &str, const string &viewportName )
214 {
215  MRPT_UNUSED_PARAM(viewportName);
216  CRenderizablePtr obj;
217  for (TListViewports::iterator it=m_viewports.begin();it!=m_viewports.end();++it)
218  if ( (obj = (*it)->getByName(str) ).present() )
219  break;
220  return obj;
221 }
222 
223 /*---------------------------------------------------------------
224  initializeAllTextures
225  ---------------------------------------------------------------*/
227 {
228  for (TListViewports::iterator it=m_viewports.begin();it!=m_viewports.end();++it)
229  (*it)->initializeAllTextures();
230 }
231 
232 /*--------------------------------------------------------------
233  dumpListOfObjects
234  ---------------------------------------------------------------*/
236 {
237  lst.clear();
238 
239  for (TListViewports::iterator it=m_viewports.begin();it!=m_viewports.end();++it)
240  {
241  lst.add( string("VIEWPORT: ")+ (*it)->m_name );
242  lst.add("============================================");
243  (*it)->dumpListOfObjects(lst);
244  }
245 }
246 
247 
248 
249 /*--------------------------------------------------------------
250  createViewport
251  ---------------------------------------------------------------*/
252 COpenGLViewportPtr COpenGLScene::createViewport( const string &viewportName )
253 {
254  MRPT_START
255 
256  COpenGLViewportPtr old = getViewport(viewportName);
257  if (old)
258  return old;
259 
260  COpenGLViewportPtr theNew = COpenGLViewportPtr( new COpenGLViewport(this, viewportName) );
261  m_viewports.push_back(theNew);
262  return theNew;
263 
264  MRPT_END
265 }
266 
267 /*--------------------------------------------------------------
268  getViewport
269  ---------------------------------------------------------------*/
270 COpenGLViewportPtr COpenGLScene::getViewport( const std::string &viewportName ) const
271 {
272  MRPT_START
273  for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
274  if ( (*it)->m_name == viewportName)
275  return *it;
276  return COpenGLViewportPtr();
277  MRPT_END
278 }
279 
280 
281 /*--------------------------------------------------------------
282  removeObject
283  ---------------------------------------------------------------*/
284 void COpenGLScene::removeObject( const CRenderizablePtr &obj, const std::string &viewportName )
285 {
286  MRPT_START
287 
288  COpenGLViewportPtr view = getViewport(viewportName);
289  ASSERT_(view.present());
290  view->removeObject(obj);
291 
292  MRPT_END
293 }
294 
295 bool COpenGLScene::traceRay(const mrpt::poses::CPose3D &o,double &dist) const {
296  bool found=false;
297  double tmp;
298  for (TListViewports::const_iterator it=m_viewports.begin();it!=m_viewports.end();++it) {
299  const COpenGLViewportPtr &vp=*it;
300  for (CListOpenGLObjects::const_iterator it2=vp->m_objects.begin();it2!=vp->m_objects.end();++it2) if ((*it2)->traceRay(o,tmp)) {
301  if (!found) {
302  found=true;
303  dist=tmp;
304  } else if (tmp<dist) dist=tmp;
305  }
306  }
307  return found;
308 }
309 
310 
311 bool COpenGLScene::saveToFile(const std::string &fil) const
312 {
313  try {
314  CFileGZOutputStream(fil) << *this;
315  return true;
316  }
317  catch (...) { return false; }
318 }
319 
321 {
322  try {
323  CFileGZInputStream(fil) >> *this;
324  return true;
325  }
326  catch (...) { return false; }
327 }
328 
329 
330 /** Evaluates the bounding box of this object (including possible children) in the coordinate frame of the object parent. */
332 {
333  COpenGLViewportPtr vp = this->getViewport(vpn);
334  ASSERTMSG_(vp.present(), "No opengl viewport exists with the given name")
335 
336  return vp->getBoundingBox(bb_min, bb_max);
337 }
virtual ~COpenGLScene()
Destructor:
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
An object for making smart pointers unique (ie, making copies if necessary), intended for being used ...
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLenum GLsizei n
Definition: glext.h:4618
Scalar * iterator
Definition: eigen_plugins.h:23
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
A viewport within a COpenGLScene, containing a set of OpenGL objects to render.
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max, const std::string &vpn=std::string("main")) const
Evaluates the bounding box of the scene in the given viewport (default: "main").
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) ...
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
A class for storing a list of text lines.
Definition: CStringList.h:32
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
void insert(const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main"))
Insert a new object into the scene, in the given viewport (by default, into the "main" viewport)...
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void removeObject(const CRenderizablePtr &obj, const std::string &viewportName=std::string("main"))
Removes the given object from the scene (it also deletes the object to free its memory).
Transparently opens a compressed "gz" file and reads uncompressed data from it.
void clear()
Clear the whole list.
Definition: CStringList.cpp:84
An object for reading objects from a stream, intended for being used in STL algorithms.
void clear(bool createMainViewport=true)
Clear the list of objects and viewports in the scene, deleting objects&#39; memory, and leaving just the ...
int version
Definition: mrpt_jpeglib.h:898
COpenGLViewportPtr getViewport(const std::string &viewportName=std::string("main")) const
Returns the viewport with the given name, or NULL if it does not exist; note that the default viewpor...
GLsizei const GLchar ** string
Definition: glext.h:3919
bool saveToFile(const std::string &fil) const
Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D.
TListViewports m_viewports
The list of viewports, indexed by name.
Definition: COpenGLScene.h:203
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
COpenGLViewportPtr createViewport(const std::string &viewportName)
Creates a new viewport, adding it to the scene and returning a pointer to the new object...
#define GL_VIEWPORT
Definition: glew.h:413
#define CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level...
bool loadFromFile(const std::string &fil)
Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
CRenderizablePtr getByName(const std::string &str, const std::string &viewportName=std::string("main"))
Returns the first object with a given name, or NULL (an empty smart pointer) if not found...
COpenGLScene & operator=(const COpenGLScene &obj)
Copy operator:
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:72
GLAPI void GLAPIENTRY glGetIntegerv(GLenum pname, GLint *params)
void render() const
Render this scene.
GLuint in
Definition: glext.h:6301
The namespace for 3D scene representation and rendering.
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:49
#define ASSERT_(f)
int GLint
Definition: glew.h:205
Lightweight 3D point.
unsigned __int32 uint32_t
Definition: rptypes.h:49
#define ASSERTMSG_(f, __ERROR_MSG)
void dumpListOfObjects(mrpt::utils::CStringList &lst)
Retrieves a list of all objects in text form.
void add(const std::string &str)
Appends a new string at the end of the string list.
Definition: CStringList.cpp:49



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