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 
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 
100  for (const auto& m_viewport : m_viewports)
101  m_viewport->render(win_dims[2], win_dims[3]);
102 
103  // Assure we restore the original viewport:
104  glViewport(win_dims[0], win_dims[1], win_dims[2], win_dims[3]);
105 
106 #else
108  "The MRPT has been compiled with MRPT_HAS_OPENGL_GLUT=0! OpenGL "
109  "functions are not implemented");
110 #endif
111  MRPT_END
112 }
113 
116 {
117  out << m_followCamera;
118 
119  uint32_t n;
120  n = (uint32_t)m_viewports.size();
121  out << n;
122  for (const auto& m_viewport : m_viewports) out << *m_viewport;
123 }
124 
127 {
128  switch (version)
129  {
130  case 0:
131  {
132  // Old style: Just one viewport:
133  clear(true);
135 
136  // Load objects:
137  uint32_t n;
138  in >> n;
139 
140  view->clear();
141  view->m_objects.resize(n);
142  for_each(
143  view->m_objects.begin(), view->m_objects.end(),
145  }
146  break;
147  case 1:
148  {
149  in >> m_followCamera;
150 
151  uint32_t i, n;
152  in >> n;
153  clear(false);
154 
155  for (i = 0; i < n; i++)
156  {
157  CSerializable::Ptr newObj;
158  in >> newObj;
159 
160  COpenGLViewport::Ptr newView =
161  std::dynamic_pointer_cast<COpenGLViewport>(newObj);
162  newView->m_parent = this;
163  m_viewports.push_back(newView);
164  }
165  }
166  break;
167  default:
169  };
170 }
171 
172 /*---------------------------------------------------------------
173  insert
174  ---------------------------------------------------------------*/
176  const CRenderizable::Ptr& newObject, const std::string& viewportName)
177 {
178  MRPT_START
179  for (auto& m_viewport : m_viewports)
180  {
181  if (m_viewport->m_name == viewportName)
182  {
183  m_viewport->insert(newObject);
184  return;
185  }
186  }
188  "Error: viewport '%s' not found.", viewportName.c_str());
189  MRPT_END
190 }
191 
192 /*---------------------------------------------------------------
193  getByName
194  ---------------------------------------------------------------*/
196  const string& str, const string& viewportName)
197 {
198  MRPT_UNUSED_PARAM(viewportName);
200  for (auto& m_viewport : m_viewports)
201  if ((obj = m_viewport->getByName(str))) break;
202  return obj;
203 }
204 
205 /*---------------------------------------------------------------
206  initializeAllTextures
207  ---------------------------------------------------------------*/
209 {
210  for (auto& m_viewport : m_viewports) m_viewport->initializeAllTextures();
211 }
212 
213 /*--------------------------------------------------------------
214  dumpListOfObjects
215  ---------------------------------------------------------------*/
216 void COpenGLScene::dumpListOfObjects(std::vector<std::string>& lst)
217 {
218  lst.clear();
219 
220  for (auto& v : m_viewports)
221  {
222  lst.emplace_back(string("VIEWPORT: ") + v->m_name);
223  lst.emplace_back("============================================");
224  v->dumpListOfObjects(lst);
225  }
226 }
227 
228 /*--------------------------------------------------------------
229  createViewport
230  ---------------------------------------------------------------*/
232 {
233  MRPT_START
234 
235  COpenGLViewport::Ptr old = getViewport(viewportName);
236  if (old) return old;
237 
238  COpenGLViewport::Ptr theNew =
239  COpenGLViewport::Ptr(new COpenGLViewport(this, viewportName));
240  m_viewports.push_back(theNew);
241  return theNew;
242 
243  MRPT_END
244 }
245 
246 /*--------------------------------------------------------------
247  getViewport
248  ---------------------------------------------------------------*/
250  const std::string& viewportName) const
251 {
252  MRPT_START
253  for (const auto& m_viewport : m_viewports)
254  if (m_viewport->m_name == viewportName) return m_viewport;
255  return COpenGLViewport::Ptr();
256  MRPT_END
257 }
258 
259 /*--------------------------------------------------------------
260  removeObject
261  ---------------------------------------------------------------*/
263  const CRenderizable::Ptr& obj, const std::string& viewportName)
264 {
265  MRPT_START
266 
267  COpenGLViewport::Ptr view = getViewport(viewportName);
268  ASSERT_(view);
269  view->removeObject(obj);
270 
271  MRPT_END
272 }
273 
274 bool COpenGLScene::traceRay(const mrpt::poses::CPose3D& o, double& dist) const
275 {
276  bool found = false;
277  double tmp;
278  for (const auto& vp : m_viewports)
279  {
280  for (auto it2 = vp->m_objects.begin(); it2 != vp->m_objects.end();
281  ++it2)
282  if ((*it2)->traceRay(o, tmp))
283  {
284  if (!found)
285  {
286  found = true;
287  dist = tmp;
288  }
289  else if (tmp < dist)
290  dist = tmp;
291  }
292  }
293  return found;
294 }
295 
296 bool COpenGLScene::saveToFile(const std::string& fil) const
297 {
298  try
299  {
302  return true;
303  }
304  catch (...)
305  {
306  return false;
307  }
308 }
309 
311 {
312  try
313  {
316  return true;
317  }
318  catch (...)
319  {
320  return false;
321  }
322 }
323 
324 /** Evaluates the bounding box of this object (including possible children) in
325  * the coordinate frame of the object parent. */
328  const std::string& vpn) const
329 {
330  COpenGLViewport::Ptr vp = this->getViewport(vpn);
331  ASSERTMSG_(vp, "No opengl viewport exists with the given name");
332 
333  return vp->getBoundingBox(bb_min, bb_max);
334 }
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) ...
unsigned char uint8_t
Definition: rptypes.h:44
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:586
#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:53
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
unsigned __int32 uint32_t
Definition: rptypes.h:50
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: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019