Main MRPT website > C++ reference for MRPT 1.9.9
CRenderizableDisplayList.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-2017, 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 
13 #include <mrpt/utils/CStream.h>
14 #include <mutex>
15 #include <cstdlib> // atexit()
16 
17 #include "opengl_internals.h"
18 
19 using namespace std;
20 using namespace mrpt;
21 using namespace mrpt::opengl;
22 using namespace mrpt::utils;
23 
26 
27 // TAuxDLData: needed since it seems we must delete display lists from the same
28 // thread we create them....
29 
30 // Phoenix Singleton pattern
31 void deleteSingleton();
32 struct TAuxDLData
33 {
34  std::vector<unsigned int> dls_to_delete;
35  std::recursive_mutex dls_to_delete_cs;
36 
38  {
39  if (!m_pInstance)
40  {
41  m_pInstance = new TAuxDLData;
42  std::atexit(deleteSingleton);
43  }
44  return *m_pInstance;
45  }
46 
48 };
50 {
52  {
54  TAuxDLData::m_pInstance = nullptr;
55  }
56 }
58 
59 // Default constructor:
60 CRenderizableDisplayList::CRenderizableDisplayList()
61  : m_dl(INVALID_DISPLAY_LIST_ID), m_dl_recreate(true)
62 {
63 }
64 
65 // Destructor:
67 {
68  // If we had an associated display list:
70  {
71  // Delete the graphical memory (actually, enque the request...)
73  obj.dls_to_delete_cs.lock();
74  obj.dls_to_delete.push_back(m_dl);
75  obj.dls_to_delete_cs.unlock();
76  }
77 }
78 
79 // This is the virtual rendering method CRenderizable expects from us.
80 // We call our derived class to save the list, then just call that list:
82 {
83 #if MRPT_HAS_OPENGL_GLUT
85  {
86  // The object is in a state where caching a display list is not
87  // preferred, so render directly:
88  render_dl();
89  }
90  else
91  {
92  // We must delete pending dl's in the same thread we create them, so,
93  // let's do it here, for example:
95  if (!obj.dls_to_delete.empty())
96  {
97  obj.dls_to_delete_cs.lock();
98 #if MRPT_HAS_OPENGL_GLUT
99  for (size_t i = 0; i < obj.dls_to_delete.size(); i++)
100  glDeleteLists(obj.dls_to_delete[i], 1);
101 #endif
102  obj.dls_to_delete.clear();
103  obj.dls_to_delete_cs.unlock();
104  }
105 
107  {
108  m_dl = glGenLists(1); // Assign list ID upon first usage.
109  if (glGetError() != GL_NO_ERROR)
110  std::cerr << "glGenLists: Error" << std::endl;
111  }
112 
113  if (m_dl_recreate)
114  {
115  m_dl_recreate = false;
117  if (glGetError() != GL_NO_ERROR)
118  std::cerr << "glNewList: Error" << std::endl;
119 
120  // Call derived class:
121  render_dl();
122 
123  glEndList();
124  if (glGetError() != GL_NO_ERROR)
125  std::cerr << "glEndList: Error" << std::endl;
126  }
127 
128  // Call the list:
129  glCallList(m_dl);
130  glGetError(); // Clear any error flags that may remain: this is because
131  // it seems FBO rendering lead to "errors" but it
132  // actually renders OK...
133  }
134 #endif
135 }
bool m_dl_recreate
If using display lists, this is true when the list must be updated (the object changes, it&#39;s the first rendering, etc...).
void deleteSingleton()
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
GLAPI GLuint GLAPIENTRY glGenLists(GLsizei range)
std::vector< unsigned int > dls_to_delete
static TAuxDLData * m_pInstance
void render() const override
Render the object, regenerating the display list if needed, otherwise just calling it...
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
IMPLEMENTS_VIRTUAL_SERIALIZABLE(CRenderizableDisplayList, CRenderizable, mrpt::opengl) void deleteSingleton()
GLAPI void GLAPIENTRY glDeleteLists(GLuint list, GLsizei range)
virtual bool should_skip_display_list_cache() const
Optional: If the object has some state in which creating a display list is NOT preferred over direct ...
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
unsigned int m_dl
Display list ID, for derived classes that want to use it (it&#39;s automatically deleted and freed on des...
virtual void render_dl() const =0
Derived classes must implement this method to the render the object.
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
GLAPI void GLAPIENTRY glNewList(GLuint list, GLenum mode)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define GL_NO_ERROR
Definition: glew.h:326
#define INVALID_DISPLAY_LIST_ID
std::recursive_mutex dls_to_delete_cs
static TAuxDLData & getSingleton()
GLAPI GLenum GLAPIENTRY glGetError(void)
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLAPI void GLAPIENTRY glEndList(void)
#define GL_COMPILE
Definition: glew.h:588
GLAPI void GLAPIENTRY glCallList(GLuint list)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019