MRPT  2.0.0
COpenGLBuffer.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, 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 #pragma once
10 
11 #include <memory>
12 #include <thread>
13 
14 namespace mrpt::opengl
15 {
16 /** A wrapper for an OpenGL buffer object.
17  * Refer to docs for glGenBuffers() and glBufferData().
18  *
19  * \ingroup mrpt_opengl_grp
20  */
22 {
23  public:
24  enum class Type : unsigned int
25  {
26  Vertex = 0x8892, // GL_ARRAY_BUFFER (Default)
27  ElementIndex = 0x8893, // GL_ELEMENT_ARRAY_BUFFER
28  PixelPack = 0x88EB, // GL_PIXEL_PACK_BUFFER
29  PixelUnpack = 0x88EC // GL_PIXEL_UNPACK_BUFFER
30  };
31 
32  enum class Usage : unsigned int
33  {
34  StreamDraw = 0x88E0, // GL_STREAM_DRAW
35  StreamRead = 0x88E1, // GL_STREAM_READ
36  StreamCopy = 0x88E2, // GL_STREAM_COPY
37  StaticDraw = 0x88E4, // GL_STATIC_DRAW (Default)
38  StaticRead = 0x88E5, // GL_STATIC_READ
39  StaticCopy = 0x88E6, // GL_STATIC_COPY
40  DynamicDraw = 0x88E8, // GL_DYNAMIC_DRAW
41  DynamicRead = 0x88E9, // GL_DYNAMIC_READ
42  DynamicCopy = 0x88EA // GL_DYNAMIC_COPY
43  };
44 
45  explicit COpenGLBuffer(const Type type);
47  ~COpenGLBuffer() = default;
48 
49  Type type() const { return m_impl->type; }
50 
51  Usage usage() const { return m_impl->usage; }
52  void setUsage(const Usage u) { m_impl->usage = u; }
53 
54  /** Actually create the buffer, destroying any previously existing buffer.
55  * Call after setting the type and usage. \sa allocate()
56  */
57  void create() { m_impl->create(); }
58 
59  /** Calls create() only if the buffer has not been created yet. */
60  void createOnce()
61  {
62  if (!isCreated()) create();
63  }
64  bool isCreated() const { return m_impl->created; }
65 
66  /** Automatically called upon destructor, no need for the user to call it in
67  * normal situations. */
68  void destroy() { m_impl->destroy(); }
69 
70  void bind() { m_impl->bind(); }
71  void release() { m_impl->bind(); }
72 
73  unsigned int bufferId() const { return m_impl->buffer_id; }
74 
75  /** Reserves byteCount bytes in the buffer and copy to it the provided data.
76  * create() and bind() must be called before using this method.
77  */
78  void allocate(const void* data, int byteCount)
79  {
80  m_impl->allocate(data, byteCount);
81  }
82 
83  private:
84  struct RAII_Impl
85  {
87  ~RAII_Impl();
88 
91 
92  void create();
93  void destroy();
94  void bind();
95  void release();
96  void allocate(const void* data, int byteCount);
97 
98  bool created = false;
99  unsigned int buffer_id = 0;
100  std::thread::id created_from;
101  };
103 };
104 
105 // For use in glVertexAttribPointer()
106 #define BUFFER_OFFSET(offset) (reinterpret_cast<GLvoid*>(offset))
107 
108 } // namespace mrpt::opengl
void createOnce()
Calls create() only if the buffer has not been created yet.
Definition: COpenGLBuffer.h:60
void allocate(const void *data, int byteCount)
Reserves byteCount bytes in the buffer and copy to it the provided data.
Definition: COpenGLBuffer.h:78
A wrapper for an OpenGL buffer object.
Definition: COpenGLBuffer.h:21
void destroy()
Automatically called upon destructor, no need for the user to call it in normal situations.
Definition: COpenGLBuffer.h:68
void create()
Actually create the buffer, destroying any previously existing buffer.
Definition: COpenGLBuffer.h:57
unsigned int bufferId() const
Definition: COpenGLBuffer.h:73
std::shared_ptr< RAII_Impl > m_impl
void allocate(const void *data, int byteCount)
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void setUsage(const Usage u)
Definition: COpenGLBuffer.h:52
static struct FontData data
Definition: gltext.cpp:144



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020