MRPT  2.0.4
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, [[maybe_unused]] const string& viewportName)
199 {
200  CRenderizable::Ptr obj;
201  for (auto& m_viewport : m_viewports)
202  if ((obj = m_viewport->getByName(str))) break;
203  return obj;
204 }
205 
207 {
208  for (auto& m_viewport : m_viewports) m_viewport->initializeTextures();
209 }
210 
211 /*--------------------------------------------------------------
212  dumpListOfObjects
213  ---------------------------------------------------------------*/
214 void COpenGLScene::dumpListOfObjects(std::vector<std::string>& lst)
215 {
216  lst.clear();
217 
218  for (auto& v : m_viewports)
219  {
220  lst.emplace_back(string("VIEWPORT: ") + v->m_name);
221  lst.emplace_back("============================================");
222  v->dumpListOfObjects(lst);
223  }
224 }
225 
226 /*--------------------------------------------------------------
227  createViewport
228  ---------------------------------------------------------------*/
230 {
231  MRPT_START
232 
233  COpenGLViewport::Ptr old = getViewport(viewportName);
234  if (old) return old;
235 
236  auto theNew = std::make_shared<COpenGLViewport>(this, viewportName);
237  m_viewports.push_back(theNew);
238  return theNew;
239 
240  MRPT_END
241 }
242 
243 /*--------------------------------------------------------------
244  getViewport
245  ---------------------------------------------------------------*/
247  const std::string& viewportName) const
248 {
249  MRPT_START
250  for (const auto& m_viewport : m_viewports)
251  if (m_viewport->m_name == viewportName) return m_viewport;
252  return COpenGLViewport::Ptr();
253  MRPT_END
254 }
255 
256 /*--------------------------------------------------------------
257  removeObject
258  ---------------------------------------------------------------*/
260  const CRenderizable::Ptr& obj, const std::string& viewportName)
261 {
262  MRPT_START
263 
264  COpenGLViewport::Ptr view = getViewport(viewportName);
265  ASSERT_(view);
266  view->removeObject(obj);
267 
268  MRPT_END
269 }
270 
271 bool COpenGLScene::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
272 {
273  bool found = false;
274  double tmp;
275  for (const auto& vp : m_viewports)
276  {
277  for (auto it2 = vp->m_objects.begin(); it2 != vp->m_objects.end();
278  ++it2)
279  if ((*it2)->traceRay(o, tmp))
280  {
281  if (!found)
282  {
283  found = true;
284  dist = tmp;
285  }
286  else if (tmp < dist)
287  dist = tmp;
288  }
289  }
290  return found;
291 }
292 
293 bool COpenGLScene::saveToFile(const std::string& fil) const
294 {
295  try
296  {
299  return true;
300  }
301  catch (...)
302  {
303  return false;
304  }
305 }
306 
307 bool COpenGLScene::loadFromFile(const std::string& fil)
308 {
309  try
310  {
313  return true;
314  }
315  catch (...)
316  {
317  return false;
318  }
319 }
320 
321 /** Evaluates the bounding box of this object (including possible children) in
322  * the coordinate frame of the object parent. */
325  const std::string& vpn) const
326 {
327  COpenGLViewport::Ptr vp = this->getViewport(vpn);
328  ASSERTMSG_(vp, "No opengl viewport exists with the given name");
329 
330  return vp->getBoundingBox(bb_min, bb_max);
331 }
332 
334 {
335  auto do_free = [](const mrpt::opengl::CRenderizable::Ptr& o) {
336  o->freeOpenGLResources();
337  };
338 
339  visitAllObjects(do_free);
340 }
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.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020