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-2018, 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 
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() : m_followCamera(false) { createViewport("main"); }
45 /*--------------------------------------------------------------
46  Copy constructor
47  ---------------------------------------------------------------*/
48 COpenGLScene::COpenGLScene(const COpenGLScene& obj) : CSerializable()
49 {
50  (*this) = obj;
51 }
52 
53 /*---------------------------------------------------------------
54  Destructor:
55  ---------------------------------------------------------------*/
57 /*---------------------------------------------------------------
58  Clear the scene.
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  {
74  m_followCamera = obj.m_followCamera;
75 
76  clear();
77  m_viewports = obj.m_viewports;
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 
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 
117 {
118  out << m_followCamera;
119 
120  uint32_t n;
121  n = (uint32_t)m_viewports.size();
122  out << n;
124  it != m_viewports.end(); ++it)
125  out << **it;
126 }
127 
130 {
131  switch (version)
132  {
133  case 0:
134  {
135  // Old style: Just one viewport:
136  clear(true);
138 
139  // Load objects:
140  uint32_t n;
141  in >> n;
142 
143  view->clear();
144  view->m_objects.resize(n);
145  for_each(
146  view->m_objects.begin(), view->m_objects.end(),
148  }
149  break;
150  case 1:
151  {
152  in >> m_followCamera;
153 
154  uint32_t i, n;
155  in >> n;
156  clear(false);
157 
158  for (i = 0; i < n; i++)
159  {
160  CSerializable::Ptr newObj;
161  in >> newObj;
162 
163  COpenGLViewport::Ptr newView =
164  std::dynamic_pointer_cast<COpenGLViewport>(newObj);
165  newView->m_parent = this;
166  m_viewports.push_back(newView);
167  }
168  }
169  break;
170  default:
172  };
173 }
174 
175 /*---------------------------------------------------------------
176  insert
177  ---------------------------------------------------------------*/
179  const CRenderizable::Ptr& newObject, const std::string& viewportName)
180 {
181  MRPT_START
182  for (TListViewports::iterator it = m_viewports.begin();
183  it != m_viewports.end(); ++it)
184  {
185  if ((*it)->m_name == viewportName)
186  {
187  (*it)->insert(newObject);
188  return;
189  }
190  }
192  "Error: viewport '%s' not found.", viewportName.c_str());
193  MRPT_END
194 }
195 
196 /*---------------------------------------------------------------
197  getByName
198  ---------------------------------------------------------------*/
200  const string& str, const string& viewportName)
201 {
202  MRPT_UNUSED_PARAM(viewportName);
204  for (TListViewports::iterator it = m_viewports.begin();
205  it != m_viewports.end(); ++it)
206  if ((obj = (*it)->getByName(str))) break;
207  return obj;
208 }
209 
210 /*---------------------------------------------------------------
211  initializeAllTextures
212  ---------------------------------------------------------------*/
214 {
215  for (TListViewports::iterator it = m_viewports.begin();
216  it != m_viewports.end(); ++it)
217  (*it)->initializeAllTextures();
218 }
219 
220 /*--------------------------------------------------------------
221  dumpListOfObjects
222  ---------------------------------------------------------------*/
223 void COpenGLScene::dumpListOfObjects(std::vector<std::string>& lst)
224 {
225  lst.clear();
226 
227  for (auto& v : m_viewports)
228  {
229  lst.emplace_back(string("VIEWPORT: ") + v->m_name);
230  lst.emplace_back("============================================");
231  v->dumpListOfObjects(lst);
232  }
233 }
234 
235 /*--------------------------------------------------------------
236  createViewport
237  ---------------------------------------------------------------*/
239 {
240  MRPT_START
241 
242  COpenGLViewport::Ptr old = getViewport(viewportName);
243  if (old) return old;
244 
245  COpenGLViewport::Ptr theNew =
246  COpenGLViewport::Ptr(new COpenGLViewport(this, viewportName));
247  m_viewports.push_back(theNew);
248  return theNew;
249 
250  MRPT_END
251 }
252 
253 /*--------------------------------------------------------------
254  getViewport
255  ---------------------------------------------------------------*/
257  const std::string& viewportName) const
258 {
259  MRPT_START
261  it != m_viewports.end(); ++it)
262  if ((*it)->m_name == viewportName) return *it;
263  return COpenGLViewport::Ptr();
264  MRPT_END
265 }
266 
267 /*--------------------------------------------------------------
268  removeObject
269  ---------------------------------------------------------------*/
271  const CRenderizable::Ptr& obj, const std::string& viewportName)
272 {
273  MRPT_START
274 
275  COpenGLViewport::Ptr view = getViewport(viewportName);
276  ASSERT_(view);
277  view->removeObject(obj);
278 
279  MRPT_END
280 }
281 
282 bool COpenGLScene::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
283 {
284  bool found = false;
285  double tmp;
287  it != m_viewports.end(); ++it)
288  {
289  const COpenGLViewport::Ptr& vp = *it;
290  for (CListOpenGLObjects::const_iterator it2 = vp->m_objects.begin();
291  it2 != vp->m_objects.end(); ++it2)
292  if ((*it2)->traceRay(o, tmp))
293  {
294  if (!found)
295  {
296  found = true;
297  dist = tmp;
298  }
299  else if (tmp < dist)
300  dist = tmp;
301  }
302  }
303  return found;
304 }
305 
306 bool COpenGLScene::saveToFile(const std::string& fil) const
307 {
308  try
309  {
312  return true;
313  }
314  catch (...)
315  {
316  return false;
317  }
318 }
319 
321 {
322  try
323  {
326  return true;
327  }
328  catch (...)
329  {
330  return false;
331  }
332 }
333 
334 /** Evaluates the bounding box of this object (including possible children) in
335  * the coordinate frame of the object parent. */
338  const std::string& vpn) const
339 {
340  COpenGLViewport::Ptr vp = this->getViewport(vpn);
341  ASSERTMSG_(vp, "No opengl viewport exists with the given name");
342 
343  return vp->getBoundingBox(bb_min, bb_max);
344 }
virtual ~COpenGLScene()
Destructor:
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.
Scalar * iterator
Definition: eigen_plugins.h:26
#define MRPT_START
Definition: exceptions.h:262
std::shared_ptr< COpenGLViewport > Ptr
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLenum GLsizei n
Definition: glext.h:5074
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
STL namespace.
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) ...
unsigned char uint8_t
Definition: rptypes.h:41
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:90
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:555
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
This base provides a set of functions for maths stuff.
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:101
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:254
const GLdouble * v
Definition: glext.h:3678
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...
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:52
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:86
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:266
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:59
Transparently opens a compressed "gz" file and reads uncompressed data from it.
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
int GLint
Definition: glew.h:209
Saves data to a file and transparently compress the data using the given compression level...
Lightweight 3D point.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
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)...
const Scalar * const_iterator
Definition: eigen_plugins.h:27
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: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020