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 }
opengl_api.h
mrpt::opengl::COpenGLScene::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: COpenGLScene.cpp:116
mrpt::serialization::metaprogramming::ObjectReadFromStream
An object for reading objects from a stream, intended for being used in STL algorithms.
Definition: metaprogramming_serialization.h:21
mrpt::math::TPoint3D_< double >
mrpt::opengl::COpenGLScene::insert
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).
Definition: COpenGLScene.cpp:177
mrpt::opengl::COpenGLScene::serializeFrom
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: COpenGLScene.cpp:127
mrpt::opengl::CRenderizable
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:49
mrpt::opengl::COpenGLScene
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
Definition: COpenGLScene.h:57
mrpt::opengl::COpenGLScene::unloadShaders
void unloadShaders()
Ensure all shaders are unloaded in all viewports.
Definition: COpenGLScene.cpp:54
CFileGZOutputStream.h
mrpt::opengl::COpenGLScene::initializeTextures
void initializeTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::initializeTextures)
Definition: COpenGLScene.cpp:206
out
mrpt::vision::TStereoCalibResults out
Definition: chessboard_stereo_camera_calib_unittest.cpp:25
mrpt::opengl::CRenderizable::Ptr
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:50
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:16
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::opengl::COpenGLScene::dumpListOfObjects
void dumpListOfObjects(std::vector< std::string > &lst)
Retrieves a list of all objects in text form.
Definition: COpenGLScene.cpp:214
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:55
bb_max
const auto bb_max
Definition: CPose3DPDFGrid_unittest.cpp:25
mrpt::opengl::COpenGLScene::getBoundingBox
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").
Definition: COpenGLScene.cpp:323
bb_min
const auto bb_min
Definition: CPose3DPDFGrid_unittest.cpp:23
mrpt::opengl::COpenGLScene::m_viewports
TListViewports m_viewports
The list of viewports, indexed by name.
Definition: COpenGLScene.h:233
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< mrpt::opengl ::COpenGLViewport > Ptr
Definition: COpenGLViewport.h:65
mrpt::io::CFileGZInputStream
Transparently opens a compressed "gz" file and reads uncompressed data from it.
Definition: io/CFileGZInputStream.h:27
MRPT_START
#define MRPT_START
Definition: exceptions.h:241
COpenGLScene.h
mrpt::io::CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level.
Definition: io/CFileGZOutputStream.h:27
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:87
mrpt::opengl::COpenGLScene::loadFromFile
bool loadFromFile(const std::string &fil)
Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
Definition: COpenGLScene.cpp:307
CRenderizable.h
mrpt::opengl::COpenGLScene::createViewport
COpenGLViewport::Ptr createViewport(const std::string &viewportName)
Creates a new viewport, adding it to the scene and returning a pointer to the new object.
Definition: COpenGLScene.cpp:229
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
Definition: CSerializable.h:166
mrpt::opengl::COpenGLScene::~COpenGLScene
~COpenGLScene() override
Definition: COpenGLScene.cpp:52
metaprogramming_serialization.h
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
opengl-precomp.h
mrpt::opengl::COpenGLScene::saveToFile
bool saveToFile(const std::string &fil) const
Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D.
Definition: COpenGLScene.cpp:293
mrpt::serialization::archiveFrom
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
Definition: CArchive.h:592
mrpt::opengl::COpenGLScene::clear
void clear(bool createMainViewport=true)
Clear the list of objects and viewports in the scene, deleting objects' memory, and leaving just the ...
Definition: COpenGLScene.cpp:60
mrpt::opengl::COpenGLScene::m_followCamera
bool m_followCamera
Definition: COpenGLScene.h:230
MRPT_END
#define MRPT_END
Definition: exceptions.h:245
mrpt::opengl::COpenGLScene::operator=
COpenGLScene & operator=(const COpenGLScene &obj)
Definition: COpenGLScene.cpp:70
CFileGZInputStream.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:12
mrpt::opengl::COpenGLScene::removeObject
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).
Definition: COpenGLScene.cpp:259
mrpt::opengl::COpenGLScene::freeOpenGLResources
void freeOpenGLResources()
Ensure all OpenGL buffers are destroyed.
Definition: COpenGLScene.cpp:333
mrpt::serialization::metaprogramming
Definition: metaprogramming_serialization.h:14
mrpt::opengl::COpenGLScene::serializeTo
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: COpenGLScene.cpp:117
CArchive.h
mrpt::opengl::COpenGLScene::getViewport
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...
Definition: COpenGLScene.cpp:246
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
mrpt::opengl::COpenGLScene::getByName
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.
Definition: COpenGLScene.cpp:197
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:14
mrpt::opengl::COpenGLScene::render
void render() const
Render this scene.
Definition: COpenGLScene.cpp:90
mrpt::opengl::COpenGLScene::visitAllObjects
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
mrpt::opengl::COpenGLScene::traceRay
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
Definition: COpenGLScene.cpp:271
mrpt::opengl::COpenGLScene::COpenGLScene
COpenGLScene()
Definition: COpenGLScene.cpp:43



Page generated by Doxygen 1.8.18 for MRPT 2.0.4 at Thu Sep 24 07:14:18 UTC 2020