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-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 
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 
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 = new TAuxDLData;
41  std::atexit(deleteSingleton);
42  }
43  return *m_pInstance;
44  }
45 
47 };
49 {
51  {
53  TAuxDLData::m_pInstance = nullptr;
54  }
55 }
57 
58 // Default constructor:
59 CRenderizableDisplayList::CRenderizableDisplayList()
60  : m_dl(INVALID_DISPLAY_LIST_ID), m_dl_recreate(true)
61 {
62 }
63 
64 // Destructor:
66 {
67  // If we had an associated display list:
69  {
70  // Delete the graphical memory (actually, enque the request...)
72  obj.dls_to_delete_cs.lock();
73  obj.dls_to_delete.push_back(m_dl);
74  obj.dls_to_delete_cs.unlock();
75  }
76 }
77 
78 // This is the virtual rendering method CRenderizable expects from us.
79 // We call our derived class to save the list, then just call that list:
81 {
82 #if MRPT_HAS_OPENGL_GLUT
84  {
85  // The object is in a state where caching a display list is not
86  // preferred, so render directly:
87  render_dl();
88  }
89  else
90  {
91  // We must delete pending dl's in the same thread we create them, so,
92  // let's do it here, for example:
94  if (!obj.dls_to_delete.empty())
95  {
96  obj.dls_to_delete_cs.lock();
97 #if MRPT_HAS_OPENGL_GLUT
98  for (size_t i = 0; i < obj.dls_to_delete.size(); i++)
99  glDeleteLists(obj.dls_to_delete[i], 1);
100 #endif
101  obj.dls_to_delete.clear();
102  obj.dls_to_delete_cs.unlock();
103  }
104 
106  {
107  m_dl = glGenLists(1); // Assign list ID upon first usage.
108  if (glGetError() != GL_NO_ERROR)
109  std::cerr << "glGenLists: Error" << std::endl;
110  }
111 
112  if (m_dl_recreate)
113  {
114  m_dl_recreate = false;
116  if (glGetError() != GL_NO_ERROR)
117  std::cerr << "glNewList: Error" << std::endl;
118 
119  // Call derived class:
120  render_dl();
121 
122  glEndList();
123  if (glGetError() != GL_NO_ERROR)
124  std::cerr << "glEndList: Error" << std::endl;
125  }
126 
127  // Call the list:
128  glCallList(m_dl);
129  glGetError(); // Clear any error flags that may remain: this is because
130  // it seems FBO rendering lead to "errors" but it
131  // actually renders OK...
132  }
133 #endif
134 }
CRenderizableDisplayList.h
mrpt::opengl::CRenderizable
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
mrpt::opengl::CRenderizableDisplayList::render
void render() const override
Render the object, regenerating the display list if needed, otherwise just calling it.
Definition: CRenderizableDisplayList.cpp:80
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
mrpt::opengl::CRenderizableDisplayList::~CRenderizableDisplayList
virtual ~CRenderizableDisplayList()
Definition: CRenderizableDisplayList.cpp:65
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
TAuxDLData::dls_to_delete_cs
std::recursive_mutex dls_to_delete_cs
Definition: CRenderizableDisplayList.cpp:34
mrpt::opengl::CRenderizableDisplayList::render_dl
virtual void render_dl() const =0
Derived classes must implement this method to the render the object.
TAuxDLData::getSingleton
static TAuxDLData & getSingleton()
Definition: CRenderizableDisplayList.cpp:36
TAuxDLData::m_pInstance
static TAuxDLData * m_pInstance
Definition: CRenderizableDisplayList.cpp:46
INVALID_DISPLAY_LIST_ID
#define INVALID_DISPLAY_LIST_ID
Definition: CRenderizableDisplayList.h:18
deleteSingleton
void deleteSingleton()
Definition: CRenderizableDisplayList.cpp:48
TAuxDLData
Definition: CRenderizableDisplayList.cpp:31
glNewList
GLAPI void GLAPIENTRY glNewList(GLuint list, GLenum mode)
mrpt::opengl::CRenderizableDisplayList::should_skip_display_list_cache
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 ...
Definition: CRenderizableDisplayList.h:64
mrpt::opengl::CRenderizableDisplayList::m_dl_recreate
bool m_dl_recreate
If using display lists, this is true when the list must be updated (the object changes,...
Definition: CRenderizableDisplayList.h:49
glCallList
GLAPI void GLAPIENTRY glCallList(GLuint list)
opengl-precomp.h
glDeleteLists
GLAPI void GLAPIENTRY glDeleteLists(GLuint list, GLsizei range)
glEndList
GLAPI void GLAPIENTRY glEndList(void)
opengl_internals.h
GL_COMPILE
#define GL_COMPILE
Definition: glew.h:588
IMPLEMENTS_VIRTUAL_SERIALIZABLE
IMPLEMENTS_VIRTUAL_SERIALIZABLE(CRenderizableDisplayList, CRenderizable, mrpt::opengl) void deleteSingleton()
TAuxDLData::dls_to_delete
std::vector< unsigned int > dls_to_delete
Definition: CRenderizableDisplayList.cpp:33
mrpt::opengl::CRenderizableDisplayList::m_dl
unsigned int m_dl
Display list ID, for derived classes that want to use it (it's automatically deleted and freed on des...
Definition: CRenderizableDisplayList.h:46
glGenLists
GLAPI GLuint GLAPIENTRY glGenLists(GLsizei range)
CArchive.h
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GL_NO_ERROR
#define GL_NO_ERROR
Definition: glew.h:326
glGetError
GLAPI GLenum GLAPIENTRY glGetError(void)



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