MRPT  1.9.9
CRenderizableDisplayList.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 
14 #include <cstdlib> // atexit()
15 #include <mutex>
16 
17 #include "opengl_internals.h"
18 
19 using namespace std;
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 
25 
26 // TAuxDLData: needed since it seems we must delete display lists from the same
27 // thread we create them....
28 
29 // Phoenix Singleton pattern
30 void deleteSingleton();
31 struct TAuxDLData
32 {
33  std::vector<unsigned int> dls_to_delete;
34  std::recursive_mutex dls_to_delete_cs;
35 
37  {
38  if (!m_pInstance)
39  {
40  m_pInstance = std::make_unique<TAuxDLData>();
41  }
42  return *m_pInstance;
43  }
44 
45  static std::unique_ptr<TAuxDLData> m_pInstance;
46 };
47 
48 std::unique_ptr<TAuxDLData> TAuxDLData::m_pInstance;
49 
50 // Destructor:
51 CRenderizableDisplayList::~CRenderizableDisplayList()
52 {
53  // If we had an associated display list:
54  if (m_dl != INVALID_DISPLAY_LIST_ID)
55  {
56  // Delete the graphical memory (actually, enque the request...)
58  obj.dls_to_delete_cs.lock();
59  obj.dls_to_delete.push_back(m_dl);
60  obj.dls_to_delete_cs.unlock();
61  }
62 }
63 
64 // This is the virtual rendering method CRenderizable expects from us.
65 // We call our derived class to save the list, then just call that list:
66 void CRenderizableDisplayList::render() const
67 {
68 #if MRPT_HAS_OPENGL_GLUT
69  if (should_skip_display_list_cache())
70  {
71  // The object is in a state where caching a display list is not
72  // preferred, so render directly:
73  render_dl();
74  }
75  else
76  {
77  // We must delete pending dl's in the same thread we create them, so,
78  // let's do it here, for example:
80  if (!obj.dls_to_delete.empty())
81  {
82  obj.dls_to_delete_cs.lock();
83 #if MRPT_HAS_OPENGL_GLUT
84  for (unsigned int i : obj.dls_to_delete) glDeleteLists(i, 1);
85 #endif
86  obj.dls_to_delete.clear();
87  obj.dls_to_delete_cs.unlock();
88  }
89 
90  if (m_dl == INVALID_DISPLAY_LIST_ID)
91  {
92  m_dl = glGenLists(1); // Assign list ID upon first usage.
93  if (glGetError() != GL_NO_ERROR)
94  std::cerr << "glGenLists: Error" << std::endl;
95  }
96 
97  if (m_dl_recreate)
98  {
99  m_dl_recreate = false;
100  glNewList(m_dl, GL_COMPILE);
101  if (glGetError() != GL_NO_ERROR)
102  std::cerr << "glNewList: Error" << std::endl;
103 
104  // Call derived class:
105  render_dl();
106 
107  glEndList();
108  if (glGetError() != GL_NO_ERROR)
109  std::cerr << "glEndList: Error" << std::endl;
110  }
111 
112  // Call the list:
113  glCallList(m_dl);
114  glGetError(); // Clear any error flags that may remain: this is because
115  // it seems FBO rendering lead to "errors" but it
116  // actually renders OK...
117  }
118 #endif
119 }
std::vector< unsigned int > dls_to_delete
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:40
IMPLEMENTS_VIRTUAL_SERIALIZABLE(CRenderizableDisplayList, CRenderizable, mrpt::opengl) void deleteSingleton()
STL namespace.
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define INVALID_DISPLAY_LIST_ID
std::recursive_mutex dls_to_delete_cs
static std::unique_ptr< TAuxDLData > m_pInstance
static TAuxDLData & getSingleton()
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7e629e01a Sat Dec 14 00:05:55 2019 +0100 at sáb dic 14 00:15:10 CET 2019