MRPT  1.9.9
COpenGLScene.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-2020, 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 
18 
19 #include <mrpt/opengl/opengl_api.h>
20 
21 using namespace mrpt;
22 using namespace mrpt::opengl;
24 using namespace mrpt::math;
25 using namespace std;
26 
27 // Include libraries in linking:
28 #if MRPT_HAS_OPENGL_GLUT
29 #ifdef _WIN32
30 // WINDOWS:
31 #if defined(_MSC_VER)
32 #pragma comment(lib, "opengl32.lib")
33 #pragma comment(lib, "GlU32.lib")
34 #endif
35 #endif // _WIN32
36 #endif // MRPT_HAS_OPENGL_GLUT
37 
39 
40 /*---------------------------------------------------------------
41  Constructor
42 ---------------------------------------------------------------*/
43 COpenGLScene::COpenGLScene() { createViewport("main"); }
44 /*--------------------------------------------------------------
45  Copy constructor
46  ---------------------------------------------------------------*/
47 COpenGLScene::COpenGLScene(const COpenGLScene& obj) : CSerializable()
48 {
49  (*this) = obj;
50 }
51 
53 
55 {
56  for (auto& v : m_viewports)
57  if (v) v->unloadShaders();
58 }
59 
60 void COpenGLScene::clear(bool createMainViewport)
61 {
62  m_viewports.clear();
63 
64  if (createMainViewport) createViewport("main");
65 }
66 
67 /*---------------------------------------------------------------
68  =
69  ---------------------------------------------------------------*/
71 {
72  if (this != &obj)
73  {
75 
76  clear();
78  for_each(m_viewports.begin(), m_viewports.end(), [](auto& ptr) {
79  // make a unique copy of each object (copied as a shared ptr)
80  ptr.reset(
81  dynamic_cast<mrpt::opengl::COpenGLViewport*>(ptr->clone()));
82  });
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  CHECK_OPENGL_ERROR();
100 
101  for (const auto& m_viewport : m_viewports)
102  m_viewport->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  CHECK_OPENGL_ERROR();
107 
108 #else
110  "The MRPT has been compiled with MRPT_HAS_OPENGL_GLUT=0! OpenGL "
111  "functions are not implemented");
112 #endif
113  MRPT_END
114 }
115 
116 uint8_t COpenGLScene::serializeGetVersion() const { return 1; }
118 {
119  out << m_followCamera;
120 
121  uint32_t n;
122  n = (uint32_t)m_viewports.size();
123  out << n;
124  for (const auto& m_viewport : m_viewports) out << *m_viewport;
125 }
126 
128  mrpt::serialization::CArchive& in, uint8_t version)
129 {
130  switch (version)
131  {
132  case 0:
133  {
134  // Old style: Just one viewport:
135  clear(true);
137 
138  // Load objects:
139  uint32_t n;
140  in >> n;
141 
142  view->clear();
143  view->m_objects.resize(n);
144  for_each(
145  view->m_objects.begin(), view->m_objects.end(),
146  ObjectReadFromStream(&in));
147  }
148  break;
149  case 1:
150  {
151  in >> m_followCamera;
152 
153  uint32_t i, n;
154  in >> n;
155  clear(false);
156 
157  for (i = 0; i < n; i++)
158  {
159  CSerializable::Ptr newObj;
160  in >> newObj;
161 
162  COpenGLViewport::Ptr newView =
163  std::dynamic_pointer_cast<COpenGLViewport>(newObj);
164  newView->m_parent = this;
165  m_viewports.push_back(newView);
166  }
167  }
168  break;
169  default:
171  };
172 }
173 
174 /*---------------------------------------------------------------
175  insert
176  ---------------------------------------------------------------*/
178  const CRenderizable::Ptr& newObject, const std::string& viewportName)
179 {
180  MRPT_START
181  for (auto& m_viewport : m_viewports)
182  {
183  if (m_viewport->m_name == viewportName)
184  {
185  m_viewport->insert(newObject);
186  return;
187  }
188  }
190  "Error: viewport '%s' not found.", viewportName.c_str());
191  MRPT_END
192 }
193 
194 /*---------------------------------------------------------------
195  getByName
196  ---------------------------------------------------------------*/
198  const string& str, const string& viewportName)
199 {
200  MRPT_UNUSED_PARAM(viewportName);
201  CRenderizable::Ptr obj;
202  for (auto& m_viewport : m_viewports)
203  if ((obj = m_viewport->getByName(str))) break;
204  return obj;
205 }
206 
208 {
209  for (auto& m_viewport : m_viewports) m_viewport->initializeTextures();
210 }
211 
212 /*--------------------------------------------------------------
213  dumpListOfObjects
214  ---------------------------------------------------------------*/
215 void COpenGLScene::dumpListOfObjects(std::vector<std::string>& lst)
216 {
217  lst.clear();
218 
219  for (auto& v : m_viewports)
220  {
221  lst.emplace_back(string("VIEWPORT: ") + v->m_name);
222  lst.emplace_back("============================================");
223  v->dumpListOfObjects(lst);
224  }
225 }
226 
227 /*--------------------------------------------------------------
228  createViewport
229  ---------------------------------------------------------------*/
231 {
232  MRPT_START
233 
234  COpenGLViewport::Ptr old = getViewport(viewportName);
235  if (old) return old;
236 
237  auto theNew = std::make_shared<COpenGLViewport>(this, viewportName);
238  m_viewports.push_back(theNew);
239  return theNew;
240 
241  MRPT_END
242 }
243 
244 /*--------------------------------------------------------------
245  getViewport
246  ---------------------------------------------------------------*/
248  const std::string& viewportName) const
249 {
250  MRPT_START
251  for (const auto& m_viewport : m_viewports)
252  if (m_viewport->m_name == viewportName) return m_viewport;
253  return COpenGLViewport::Ptr();
254  MRPT_END
255 }
256 
257 /*--------------------------------------------------------------
258  removeObject
259  ---------------------------------------------------------------*/
261  const CRenderizable::Ptr& obj, const std::string& viewportName)
262 {
263  MRPT_START
264 
265  COpenGLViewport::Ptr view = getViewport(viewportName);
266  ASSERT_(view);
267  view->removeObject(obj);
268 
269  MRPT_END
270 }
271 
272 bool COpenGLScene::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
273 {
274  bool found = false;
275  double tmp;
276  for (const auto& vp : m_viewports)
277  {
278  for (auto it2 = vp->m_objects.begin(); it2 != vp->m_objects.end();
279  ++it2)
280  if ((*it2)->traceRay(o, tmp))
281  {
282  if (!found)
283  {
284  found = true;
285  dist = tmp;
286  }
287  else if (tmp < dist)
288  dist = tmp;
289  }
290  }
291  return found;
292 }
293 
294 bool COpenGLScene::saveToFile(const std::string& fil) const
295 {
296  try
297  {
300  return true;
301  }
302  catch (...)
303  {
304  return false;
305  }
306 }
307 
308 bool COpenGLScene::loadFromFile(const std::string& fil)
309 {
310  try
311  {
314  return true;
315  }
316  catch (...)
317  {
318  return false;
319  }
320 }
321 
322 /** Evaluates the bounding box of this object (including possible children) in
323  * the coordinate frame of the object parent. */
326  const std::string& vpn) const
327 {
328  COpenGLViewport::Ptr vp = this->getViewport(vpn);
329  ASSERTMSG_(vp, "No opengl viewport exists with the given name");
330 
331  return vp->getBoundingBox(bb_min, bb_max);
332 }
333 
335 {
336  auto do_free = [](const mrpt::opengl::CRenderizable::Ptr& o) {
337  o->freeOpenGLResources();
338  };
339 
340  visitAllObjects(do_free);
341 }
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
An object for reading objects from a stream, intended for being used in STL algorithms.
#define MRPT_START
Definition: exceptions.h:241
void freeOpenGLResources()
Ensure all OpenGL buffers are destroyed.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
void visitAllObjects(FUNCTOR functor) const
Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied func...
Definition: COpenGLScene.h:213
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
STL namespace.
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").
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream.
Definition: CArchive.h:592
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
This base provides a set of functions for maths stuff.
void initializeTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::initializeTextures) ...
void dumpListOfObjects(std::vector< std::string > &lst)
Retrieves a list of all objects in text form.
COpenGLViewport::Ptr createViewport(const std::string &viewportName)
Creates a new viewport, adding it to the scene and returning a pointer to the new object...
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
void clear(bool createMainViewport=true)
Clear the list of objects and viewports in the scene, deleting objects&#39; memory, and leaving just the ...
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:233
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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...
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
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)
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
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...
void render() const
Render this scene.
#define MRPT_END
Definition: exceptions.h:245
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:56
const auto bb_max
std::shared_ptr< mrpt::opengl ::COpenGLViewport > Ptr
Transparently opens a compressed "gz" file and reads uncompressed data from it.
const auto bb_min
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Saves data to a file and transparently compress the data using the given compression level...
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
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)...
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).
void unloadShaders()
Ensure all shaders are unloaded in all viewports.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020