Main MRPT website > C++ reference for MRPT 1.9.9
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 
15 #include <mrpt/utils/CStringList.h>
17 #include <mrpt/utils/CStream.h>
18 
21 
22 #include "opengl_internals.h"
23 
24 using namespace mrpt;
25 using namespace mrpt::opengl;
26 using namespace mrpt::utils;
27 using namespace mrpt::math;
28 using namespace std;
29 
30 // Include libraries in linking:
31 #if MRPT_HAS_OPENGL_GLUT
32 #ifdef MRPT_OS_WINDOWS
33 // WINDOWS:
34 #if defined(_MSC_VER) || defined(__BORLANDC__)
35 #pragma comment(lib, "opengl32.lib")
36 #pragma comment(lib, "GlU32.lib")
37 #endif
38 #endif // MRPT_OS_WINDOWS
39 #endif // MRPT_HAS_OPENGL_GLUT
40 
42 
43 /*---------------------------------------------------------------
44  Constructor
45 ---------------------------------------------------------------*/
46 COpenGLScene::COpenGLScene() : m_followCamera(false) { createViewport("main"); }
47 /*--------------------------------------------------------------
48  Copy constructor
49  ---------------------------------------------------------------*/
51 {
52  (*this) = obj;
53 }
54 
55 /*---------------------------------------------------------------
56  Destructor:
57  ---------------------------------------------------------------*/
59 /*---------------------------------------------------------------
60  Clear the scene.
61  ---------------------------------------------------------------*/
62 void COpenGLScene::clear(bool createMainViewport)
63 {
64  m_viewports.clear();
65 
66  if (createMainViewport) createViewport("main");
67 }
68 
69 /*---------------------------------------------------------------
70  =
71  ---------------------------------------------------------------*/
73 {
74  if (this != &obj)
75  {
76  m_followCamera = obj.m_followCamera;
77 
78  clear();
79  m_viewports = obj.m_viewports;
80  for_each(
81  m_viewports.begin(), m_viewports.end(),
83  }
84  return *this;
85 }
86 
87 /*---------------------------------------------------------------
88  render
89  ---------------------------------------------------------------*/
91 {
93 
94 #if MRPT_HAS_OPENGL_GLUT
95  // We need the size of the viewport at the beginning: should be the whole
96  // window:
97  GLint win_dims[4];
98  glGetIntegerv(GL_VIEWPORT, win_dims);
99 
101  it != m_viewports.end(); ++it)
102  (*it)->render(win_dims[2], win_dims[3]);
103 
104  // Assure we restore the original viewport:
105  glViewport(win_dims[0], win_dims[1], win_dims[2], win_dims[3]);
106 
107 #else
109  "The MRPT has been compiled with MRPT_HAS_OPENGL_GLUT=0! OpenGL "
110  "functions are not implemented");
111 #endif
112  MRPT_END
113 }
114 
115 /*---------------------------------------------------------------
116  Implements the writing to a CStream capability of
117  CSerializable objects
118  ---------------------------------------------------------------*/
119 void COpenGLScene::writeToStream(mrpt::utils::CStream& out, int* version) const
120 {
121  if (version)
122  *version = 1;
123  else
124  {
125  out << m_followCamera;
126 
127  uint32_t n;
128  n = (uint32_t)m_viewports.size();
129  out << n;
131  it != m_viewports.end(); ++it)
132  out << **it;
133  }
134 }
135 
136 /*---------------------------------------------------------------
137  Implements the reading from a CStream capability of
138  CSerializable objects
139  ---------------------------------------------------------------*/
141 {
142  switch (version)
143  {
144  case 0:
145  {
146  // Old style: Just one viewport:
147  clear(true);
149 
150  // Load objects:
151  uint32_t n;
152  in >> n;
153 
154  view->clear();
155  view->m_objects.resize(n);
156  for_each(
157  view->m_objects.begin(), view->m_objects.end(),
159  }
160  break;
161  case 1:
162  {
163  in >> m_followCamera;
164 
165  uint32_t i, n;
166  in >> n;
167  clear(false);
168 
169  for (i = 0; i < n; i++)
170  {
171  CSerializable::Ptr newObj;
172  in >> newObj;
173 
174  COpenGLViewport::Ptr newView =
175  std::dynamic_pointer_cast<COpenGLViewport>(newObj);
176  newView->m_parent = this;
177  m_viewports.push_back(newView);
178  }
179  }
180  break;
181  default:
183  };
184 }
185 
186 /*---------------------------------------------------------------
187  insert
188  ---------------------------------------------------------------*/
190  const CRenderizable::Ptr& newObject, const std::string& viewportName)
191 {
192  MRPT_START
193  for (TListViewports::iterator it = m_viewports.begin();
194  it != m_viewports.end(); ++it)
195  {
196  if ((*it)->m_name == viewportName)
197  {
198  (*it)->insert(newObject);
199  return;
200  }
201  }
203  "Error: viewport '%s' not found.", viewportName.c_str());
204  MRPT_END
205 }
206 
207 /*---------------------------------------------------------------
208  getByName
209  ---------------------------------------------------------------*/
211  const string& str, const string& viewportName)
212 {
213  MRPT_UNUSED_PARAM(viewportName);
215  for (TListViewports::iterator it = m_viewports.begin();
216  it != m_viewports.end(); ++it)
217  if ((obj = (*it)->getByName(str))) break;
218  return obj;
219 }
220 
221 /*---------------------------------------------------------------
222  initializeAllTextures
223  ---------------------------------------------------------------*/
225 {
226  for (TListViewports::iterator it = m_viewports.begin();
227  it != m_viewports.end(); ++it)
228  (*it)->initializeAllTextures();
229 }
230 
231 /*--------------------------------------------------------------
232  dumpListOfObjects
233  ---------------------------------------------------------------*/
235 {
236  lst.clear();
237 
238  for (TListViewports::iterator it = m_viewports.begin();
239  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  createViewport
249  ---------------------------------------------------------------*/
251 {
252  MRPT_START
253 
254  COpenGLViewport::Ptr old = getViewport(viewportName);
255  if (old) return old;
256 
257  COpenGLViewport::Ptr theNew =
258  COpenGLViewport::Ptr(new COpenGLViewport(this, viewportName));
259  m_viewports.push_back(theNew);
260  return theNew;
261 
262  MRPT_END
263 }
264 
265 /*--------------------------------------------------------------
266  getViewport
267  ---------------------------------------------------------------*/
269  const std::string& viewportName) const
270 {
271  MRPT_START
273  it != m_viewports.end(); ++it)
274  if ((*it)->m_name == viewportName) return *it;
275  return COpenGLViewport::Ptr();
276  MRPT_END
277 }
278 
279 /*--------------------------------------------------------------
280  removeObject
281  ---------------------------------------------------------------*/
283  const CRenderizable::Ptr& obj, const std::string& viewportName)
284 {
285  MRPT_START
286 
287  COpenGLViewport::Ptr view = getViewport(viewportName);
288  ASSERT_(view);
289  view->removeObject(obj);
290 
291  MRPT_END
292 }
293 
294 bool COpenGLScene::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
295 {
296  bool found = false;
297  double tmp;
299  it != m_viewports.end(); ++it)
300  {
301  const COpenGLViewport::Ptr& vp = *it;
302  for (CListOpenGLObjects::const_iterator it2 = vp->m_objects.begin();
303  it2 != vp->m_objects.end(); ++it2)
304  if ((*it2)->traceRay(o, tmp))
305  {
306  if (!found)
307  {
308  found = true;
309  dist = tmp;
310  }
311  else if (tmp < dist)
312  dist = tmp;
313  }
314  }
315  return found;
316 }
317 
318 bool COpenGLScene::saveToFile(const std::string& fil) const
319 {
320  try
321  {
322  CFileGZOutputStream(fil) << *this;
323  return true;
324  }
325  catch (...)
326  {
327  return false;
328  }
329 }
330 
332 {
333  try
334  {
335  CFileGZInputStream(fil) >> *this;
336  return true;
337  }
338  catch (...)
339  {
340  return false;
341  }
342 }
343 
344 /** Evaluates the bounding box of this object (including possible children) in
345  * the coordinate frame of the object parent. */
348  const std::string& vpn) const
349 {
350  COpenGLViewport::Ptr vp = this->getViewport(vpn);
351  ASSERTMSG_(vp, "No opengl viewport exists with the given name")
352 
353  return vp->getBoundingBox(bb_min, bb_max);
354 }
virtual ~COpenGLScene()
Destructor:
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::shared_ptr< COpenGLViewport > Ptr
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:44
#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:5074
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
Scalar * iterator
Definition: eigen_plugins.h:26
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
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:41
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
#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.
Transparently opens a compressed "gz" file and reads uncompressed data from it.
void clear()
Clear the whole list.
Definition: CStringList.cpp:73
COpenGLViewport::Ptr createViewport(const std::string &viewportName)
Creates a new viewport, adding it to the scene and returning a pointer to the new object...
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 ...
GLsizei const GLchar ** string
Definition: glext.h:4101
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:255
std::shared_ptr< CSerializable > Ptr
Definition: CSerializable.h:47
#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)
#define GL_VIEWPORT
Definition: glew.h:417
COpenGLViewport::Ptr getViewport(const std::string &viewportName=std::string("main")) const
Returns the viewport with the given name, or nullptr if it does not exist; note that the default view...
#define CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level...
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
bool loadFromFile(const std::string &fil)
Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
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:88
CRenderizable::Ptr getByName(const std::string &str, const std::string &viewportName=std::string("main"))
Returns the first object with a given name, or nullptr (an empty smart pointer) if not found...
GLAPI void GLAPIENTRY glGetIntegerv(GLenum pname, GLint *params)
void render() const
Render this scene.
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:60
#define ASSERT_(f)
int GLint
Definition: glew.h:209
Lightweight 3D point.
unsigned __int32 uint32_t
Definition: rptypes.h:47
void insert(const CRenderizable::Ptr &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 ASSERTMSG_(f, __ERROR_MSG)
void removeObject(const CRenderizable::Ptr &obj, const std::string &viewportName=std::string("main"))
Removes the given object from the scene (it also deletes the object to free its memory).
utils::safe_ptr< COpenGLScene > m_parent
The scene that contains this viewport.
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:43



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019