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-2019, 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 
19 
20 #include "opengl_internals.h"
21 
22 using namespace mrpt;
23 using namespace mrpt::opengl;
25 using namespace mrpt::math;
26 using namespace std;
27 
28 // Include libraries in linking:
29 #if MRPT_HAS_OPENGL_GLUT
30 #ifdef _WIN32
31 // WINDOWS:
32 #if defined(_MSC_VER)
33 #pragma comment(lib, "opengl32.lib")
34 #pragma comment(lib, "GlU32.lib")
35 #endif
36 #endif // _WIN32
37 #endif // MRPT_HAS_OPENGL_GLUT
38 
40 
41 /*---------------------------------------------------------------
42  Constructor
43 ---------------------------------------------------------------*/
44 COpenGLScene::COpenGLScene() { createViewport("main"); }
45 /*--------------------------------------------------------------
46  Copy constructor
47  ---------------------------------------------------------------*/
48 COpenGLScene::COpenGLScene(const COpenGLScene& obj) : CSerializable()
49 {
50  (*this) = obj;
51 }
52 
54 
55 void COpenGLScene::clear(bool createMainViewport)
56 {
57  m_viewports.clear();
58 
59  if (createMainViewport) createViewport("main");
60 }
61 
62 /*---------------------------------------------------------------
63  =
64  ---------------------------------------------------------------*/
66 {
67  if (this != &obj)
68  {
69  m_followCamera = obj.m_followCamera;
70 
71  clear();
72  m_viewports = obj.m_viewports;
73  for_each(m_viewports.begin(), m_viewports.end(), [](auto& ptr) {
74  // make a unique copy of each object (copied as a shared ptr)
75  ptr.reset(
76  dynamic_cast<mrpt::opengl::COpenGLViewport*>(ptr->clone()));
77  });
78  }
79  return *this;
80 }
81 
82 /*---------------------------------------------------------------
83  render
84  ---------------------------------------------------------------*/
86 {
88 
89 #if MRPT_HAS_OPENGL_GLUT
90  // We need the size of the viewport at the beginning: should be the whole
91  // window:
92  GLint win_dims[4];
93  glGetIntegerv(GL_VIEWPORT, win_dims);
94 
95  for (const auto& m_viewport : m_viewports)
96  m_viewport->render(win_dims[2], win_dims[3]);
97 
98  // Assure we restore the original viewport:
99  glViewport(win_dims[0], win_dims[1], win_dims[2], win_dims[3]);
100 
101 #else
103  "The MRPT has been compiled with MRPT_HAS_OPENGL_GLUT=0! OpenGL "
104  "functions are not implemented");
105 #endif
106  MRPT_END
107 }
108 
109 uint8_t COpenGLScene::serializeGetVersion() const { return 1; }
111 {
112  out << m_followCamera;
113 
114  uint32_t n;
115  n = (uint32_t)m_viewports.size();
116  out << n;
117  for (const auto& m_viewport : m_viewports) out << *m_viewport;
118 }
119 
121  mrpt::serialization::CArchive& in, uint8_t version)
122 {
123  switch (version)
124  {
125  case 0:
126  {
127  // Old style: Just one viewport:
128  clear(true);
130 
131  // Load objects:
132  uint32_t n;
133  in >> n;
134 
135  view->clear();
136  view->m_objects.resize(n);
137  for_each(
138  view->m_objects.begin(), view->m_objects.end(),
140  }
141  break;
142  case 1:
143  {
144  in >> m_followCamera;
145 
146  uint32_t i, n;
147  in >> n;
148  clear(false);
149 
150  for (i = 0; i < n; i++)
151  {
152  CSerializable::Ptr newObj;
153  in >> newObj;
154 
155  COpenGLViewport::Ptr newView =
156  std::dynamic_pointer_cast<COpenGLViewport>(newObj);
157  newView->m_parent = this;
158  m_viewports.push_back(newView);
159  }
160  }
161  break;
162  default:
164  };
165 }
166 
167 /*---------------------------------------------------------------
168  insert
169  ---------------------------------------------------------------*/
171  const CRenderizable::Ptr& newObject, const std::string& viewportName)
172 {
173  MRPT_START
174  for (auto& m_viewport : m_viewports)
175  {
176  if (m_viewport->m_name == viewportName)
177  {
178  m_viewport->insert(newObject);
179  return;
180  }
181  }
183  "Error: viewport '%s' not found.", viewportName.c_str());
184  MRPT_END
185 }
186 
187 /*---------------------------------------------------------------
188  getByName
189  ---------------------------------------------------------------*/
191  const string& str, const string& viewportName)
192 {
193  MRPT_UNUSED_PARAM(viewportName);
195  for (auto& m_viewport : m_viewports)
196  if ((obj = m_viewport->getByName(str))) break;
197  return obj;
198 }
199 
200 /*---------------------------------------------------------------
201  initializeAllTextures
202  ---------------------------------------------------------------*/
204 {
205  for (auto& m_viewport : m_viewports) m_viewport->initializeAllTextures();
206 }
207 
208 /*--------------------------------------------------------------
209  dumpListOfObjects
210  ---------------------------------------------------------------*/
211 void COpenGLScene::dumpListOfObjects(std::vector<std::string>& lst)
212 {
213  lst.clear();
214 
215  for (auto& v : m_viewports)
216  {
217  lst.emplace_back(string("VIEWPORT: ") + v->m_name);
218  lst.emplace_back("============================================");
219  v->dumpListOfObjects(lst);
220  }
221 }
222 
223 /*--------------------------------------------------------------
224  createViewport
225  ---------------------------------------------------------------*/
227 {
228  MRPT_START
229 
230  COpenGLViewport::Ptr old = getViewport(viewportName);
231  if (old) return old;
232 
233  COpenGLViewport::Ptr theNew =
234  COpenGLViewport::Ptr(new COpenGLViewport(this, viewportName));
235  m_viewports.push_back(theNew);
236  return theNew;
237 
238  MRPT_END
239 }
240 
241 /*--------------------------------------------------------------
242  getViewport
243  ---------------------------------------------------------------*/
245  const std::string& viewportName) const
246 {
247  MRPT_START
248  for (const auto& m_viewport : m_viewports)
249  if (m_viewport->m_name == viewportName) return m_viewport;
250  return COpenGLViewport::Ptr();
251  MRPT_END
252 }
253 
254 /*--------------------------------------------------------------
255  removeObject
256  ---------------------------------------------------------------*/
258  const CRenderizable::Ptr& obj, const std::string& viewportName)
259 {
260  MRPT_START
261 
262  COpenGLViewport::Ptr view = getViewport(viewportName);
263  ASSERT_(view);
264  view->removeObject(obj);
265 
266  MRPT_END
267 }
268 
269 bool COpenGLScene::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
270 {
271  bool found = false;
272  double tmp;
273  for (const auto& vp : m_viewports)
274  {
275  for (auto it2 = vp->m_objects.begin(); it2 != vp->m_objects.end();
276  ++it2)
277  if ((*it2)->traceRay(o, tmp))
278  {
279  if (!found)
280  {
281  found = true;
282  dist = tmp;
283  }
284  else if (tmp < dist)
285  dist = tmp;
286  }
287  }
288  return found;
289 }
290 
291 bool COpenGLScene::saveToFile(const std::string& fil) const
292 {
293  try
294  {
297  return true;
298  }
299  catch (...)
300  {
301  return false;
302  }
303 }
304 
306 {
307  try
308  {
311  return true;
312  }
313  catch (...)
314  {
315  return false;
316  }
317 }
318 
319 /** Evaluates the bounding box of this object (including possible children) in
320  * the coordinate frame of the object parent. */
323  const std::string& vpn) const
324 {
325  COpenGLViewport::Ptr vp = this->getViewport(vpn);
326  ASSERTMSG_(vp, "No opengl viewport exists with the given name");
327 
328  return vp->getBoundingBox(bb_min, bb_max);
329 }
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
std::shared_ptr< COpenGLViewport > Ptr
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
GLenum GLsizei n
Definition: glext.h:5136
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
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.
#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:591
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
This base provides a set of functions for maths stuff.
~COpenGLScene() override
Destructor:
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 ...
GLsizei const GLchar ** string
Definition: glext.h:4116
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:238
const GLdouble * v
Definition: glext.h:3684
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:418
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)
Copy operator:
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:84
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.
#define MRPT_END
Definition: exceptions.h:245
GLuint in
Definition: glext.h:7391
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:58
const auto bb_max
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.
int GLint
Definition: glew.h:210
Saves data to a file and transparently compress the data using the given compression level...
Lightweight 3D point.
Definition: TPoint3D.h:90
#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).
#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: abb8b1a1e Fri Oct 18 14:19:12 2019 +0200 at vie oct 18 14:20:13 CEST 2019