Main MRPT website > C++ reference for 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 }
n
GLenum GLsizei n
Definition: glext.h:5074
glViewport
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
mrpt::opengl::COpenGLScene::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: COpenGLScene.cpp:115
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::serialization::metaprogramming::ObjectReadFromStream
An object for reading objects from a stream, intended for being used in STL algorithms.
Definition: metaprogramming_serialization.h:24
mrpt::opengl::COpenGLScene::initializeAllTextures
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
Definition: COpenGLScene.cpp:213
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:178
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:128
CRenderizableDisplayList.h
mrpt::opengl::COpenGLScene
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
Definition: COpenGLScene.h:59
mrpt::opengl::CRenderizableDisplayList
A renderizable object suitable for rendering with OpenGL's display lists.
Definition: CRenderizableDisplayList.h:39
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
CFileGZOutputStream.h
mrpt::opengl::COpenGLScene::~COpenGLScene
virtual ~COpenGLScene()
Destructor:
Definition: COpenGLScene.cpp:56
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
mrpt::opengl::CRenderizable::Ptr
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
uint8_t
unsigned char uint8_t
Definition: rptypes.h:41
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::opengl::COpenGLScene::dumpListOfObjects
void dumpListOfObjects(std::vector< std::string > &lst)
Retrieves a list of all objects in text form.
Definition: COpenGLScene.cpp:223
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
v
const GLdouble * v
Definition: glext.h:3678
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:336
mrpt::opengl::COpenGLScene::m_viewports
TListViewports m_viewports
The list of viewports, indexed by name.
Definition: COpenGLScene.h:254
mrpt::io::CFileGZInputStream
Transparently opens a compressed "gz" file and reads uncompressed data from it.
Definition: io/CFileGZInputStream.h:26
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
COpenGLScene.h
mrpt::io::CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level.
Definition: io/CFileGZOutputStream.h:26
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
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:320
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:238
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Definition: CSerializable.h:114
glGetIntegerv
GLAPI void GLAPIENTRY glGetIntegerv(GLenum pname, GLint *params)
metaprogramming_serialization.h
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
mrpt::math::TPoint3D
Lightweight 3D point.
Definition: lightweight_geom_data.h:378
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:306
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:561
opengl_internals.h
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:249
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::opengl::COpenGLViewport
A viewport within a COpenGLScene, containing a set of OpenGL objects to render.
Definition: COpenGLViewport.h:60
mrpt::opengl::COpenGLScene::operator=
COpenGLScene & operator=(const COpenGLScene &obj)
Copy operator:
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:13
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:270
mrpt::serialization::metaprogramming
Definition: metaprogramming_serialization.h:17
in
GLuint in
Definition: glext.h:7274
string
GLsizei const GLchar ** string
Definition: glext.h:4101
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
GLint
int GLint
Definition: glew.h:209
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:116
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:256
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
GL_VIEWPORT
#define GL_VIEWPORT
Definition: glew.h:417
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:199
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< COpenGLViewport > Ptr
Definition: COpenGLViewport.h:63
uint32_t
unsigned __int32 uint32_t
Definition: rptypes.h:47
mrpt::opengl::COpenGLScene::render
void render() const
Render this scene.
Definition: COpenGLScene.cpp:90
mrpt::opengl::COpenGLScene::traceRay
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
Definition: COpenGLScene.cpp:282
mrpt::opengl::COpenGLScene::COpenGLScene
COpenGLScene()
Constructor.
Definition: COpenGLScene.cpp:44



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST