Main MRPT website > C++ reference for MRPT 1.5.6
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>
15 #include <cstdlib> // atexit()
16 
17 #include "opengl_internals.h"
18 
19 
20 using namespace std;
21 using namespace mrpt;
22 using namespace mrpt::opengl;
23 using namespace mrpt::utils;
24 
26 
27 // TAuxDLData: needed since it seems we must delete display lists from the same thread we create them....
28 
29 // Phoenix Singleton pattern
31 struct TAuxDLData
32 {
33  std::vector<unsigned int> dls_to_delete;
35 
36  static TAuxDLData& getSingleton()
37  {
38  if (!m_pInstance)
39  {
40  m_pInstance = new TAuxDLData;
41  std::atexit( deleteSingleton );
42  }
43  return *m_pInstance;
44  }
45 
46  static TAuxDLData *m_pInstance;
47 };
49 {
51  {
54  }
55 }
57 
58 // Default constructor:
59 CRenderizableDisplayList::CRenderizableDisplayList() :
61  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.enter();
74  obj.dls_to_delete.push_back(m_dl);
75  obj.dls_to_delete_cs.leave();
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 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, let's do it here, for example:
93  if (!obj.dls_to_delete.empty())
94  {
95  obj.dls_to_delete_cs.enter();
96 #if MRPT_HAS_OPENGL_GLUT
97  for (size_t i=0;i<obj.dls_to_delete.size();i++)
98  glDeleteLists(obj.dls_to_delete[i], 1);
99 #endif
100  obj.dls_to_delete.clear();
101  obj.dls_to_delete_cs.leave();
102  }
103 
105  {
106  m_dl = glGenLists(1); // Assign list ID upon first usage.
107  if (glGetError()!= GL_NO_ERROR)
108  std::cerr << "glGenLists: Error" << std::endl;
109  }
110 
111  if (m_dl_recreate)
112  {
113  m_dl_recreate = false;
115  if (glGetError()!= GL_NO_ERROR)
116  std::cerr << "glNewList: Error" << std::endl;
117 
118  // Call derived class:
119  render_dl();
120 
121  glEndList();
122  if (glGetError()!= GL_NO_ERROR)
123  std::cerr << "glEndList: Error" << std::endl;
124  }
125 
126  // Call the list:
127  glCallList(m_dl);
128  glGetError(); // Clear any error flags that may remain: this is because it seems FBO rendering lead to "errors" but it actually renders OK...
129  }
130 #endif
131 }
132 
Recursive mutex: allow recursive locks by the owner thread.
bool m_dl_recreate
If using display lists, this is true when the list must be updated (the object changes, it's the first rendering, etc...).
void deleteSingleton()
void enter() const MRPT_OVERRIDE
Enter. the calling thread already possesses this critical section (it would be a dead-lock).
GLvoid *typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1745
GLAPI GLuint GLAPIENTRY glGenLists(GLsizei range)
std::vector< unsigned int > dls_to_delete
static TAuxDLData * m_pInstance
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:44
GLAPI void GLAPIENTRY glDeleteLists(GLuint list, GLsizei range)
void render() const MRPT_OVERRIDE
Render the object, regenerating the display list if needed, otherwise just calling it...
unsigned int m_dl
Display list ID, for derived classes that want to use it (it'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's display lists.
#define IMPLEMENTS_VIRTUAL_SERIALIZABLE(class_name, base_class_name, NameSpace)
This must be inserted as implementation of some required members for virtual CSerializable classes: ...
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 ...
GLhandleARB obj
Definition: glew.h:3276
GLAPI void GLAPIENTRY glNewList(GLuint list, GLenum mode)
#define GL_NO_ERROR
Definition: glew.h:322
#define INVALID_DISPLAY_LIST_ID
mrpt::synch::CCriticalSectionRecursive dls_to_delete_cs
static TAuxDLData & getSingleton()
GLAPI GLenum GLAPIENTRY glGetError(void)
GLAPI void GLAPIENTRY glEndList(void)
#define GL_COMPILE
Definition: glew.h:584
void leave() const MRPT_OVERRIDE
Leave. the calling thread is not the current owner of the critical section.
GLAPI void GLAPIENTRY glCallList(GLuint list)



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018