MRPT  2.0.4
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  };
102  std::shared_ptr<RAII_Impl> m_impl;
103 };
104 
105 // For use in glVertexAttribPointer()
106 #define BUFFER_OFFSET(offset) (reinterpret_cast<GLvoid*>(offset))
107 
108 } // namespace mrpt::opengl
mrpt::opengl::internal::data
static struct FontData data
Definition: gltext.cpp:144
mrpt::opengl::COpenGLBuffer::isCreated
bool isCreated() const
Definition: COpenGLBuffer.h:64
mrpt::opengl::COpenGLBuffer::Type::ElementIndex
@ ElementIndex
mrpt::opengl::COpenGLBuffer::RAII_Impl::created_from
std::thread::id created_from
Definition: COpenGLBuffer.h:100
mrpt::opengl::COpenGLBuffer::createOnce
void createOnce()
Calls create() only if the buffer has not been created yet.
Definition: COpenGLBuffer.h:60
mrpt::opengl::COpenGLBuffer::RAII_Impl::RAII_Impl
RAII_Impl(COpenGLBuffer::Type t)
Definition: COpenGLBuffer.cpp:28
mrpt::opengl::COpenGLBuffer::~COpenGLBuffer
~COpenGLBuffer()=default
mrpt::opengl::COpenGLBuffer::Usage::StaticDraw
@ StaticDraw
mrpt::opengl::COpenGLBuffer::release
void release()
Definition: COpenGLBuffer.h:71
mrpt::opengl::COpenGLBuffer::RAII_Impl::type
COpenGLBuffer::Type type
Definition: COpenGLBuffer.h:89
mrpt::opengl::COpenGLBuffer::RAII_Impl::create
void create()
Definition: COpenGLBuffer.cpp:35
mrpt::opengl::COpenGLBuffer::RAII_Impl::buffer_id
unsigned int buffer_id
Definition: COpenGLBuffer.h:99
mrpt::opengl::COpenGLBuffer::allocate
void allocate(const void *data, int byteCount)
Reserves byteCount bytes in the buffer and copy to it the provided data.
Definition: COpenGLBuffer.h:78
mrpt::opengl::COpenGLBuffer::Usage
Usage
Definition: COpenGLBuffer.h:33
mrpt::opengl::COpenGLBuffer::RAII_Impl::bind
void bind()
Definition: COpenGLBuffer.cpp:83
mrpt::opengl::COpenGLBuffer::Usage::DynamicDraw
@ DynamicDraw
mrpt::opengl::COpenGLBuffer::Usage::StaticRead
@ StaticRead
mrpt::opengl::COpenGLBuffer::RAII_Impl::destroy
void destroy()
Definition: COpenGLBuffer.cpp:47
mrpt::opengl::COpenGLBuffer::create
void create()
Actually create the buffer, destroying any previously existing buffer.
Definition: COpenGLBuffer.h:57
mrpt::opengl::COpenGLBuffer::Type::Vertex
@ Vertex
mrpt::opengl::COpenGLBuffer::RAII_Impl::usage
COpenGLBuffer::Usage usage
Definition: COpenGLBuffer.h:90
mrpt::opengl::COpenGLBuffer::Usage::StaticCopy
@ StaticCopy
mrpt::opengl::COpenGLBuffer::RAII_Impl::created
bool created
Definition: COpenGLBuffer.h:98
mrpt::opengl::COpenGLBuffer::Usage::StreamRead
@ StreamRead
mrpt::opengl::COpenGLBuffer::m_impl
std::shared_ptr< RAII_Impl > m_impl
Definition: COpenGLBuffer.h:102
mrpt::opengl::COpenGLBuffer::bufferId
unsigned int bufferId() const
Definition: COpenGLBuffer.h:73
mrpt::opengl::COpenGLBuffer::RAII_Impl::release
void release()
Definition: COpenGLBuffer.cpp:91
mrpt::opengl::COpenGLBuffer::Type::PixelUnpack
@ PixelUnpack
mrpt::opengl::COpenGLBuffer
A wrapper for an OpenGL buffer object.
Definition: COpenGLBuffer.h:22
mrpt::opengl::COpenGLBuffer::setUsage
void setUsage(const Usage u)
Definition: COpenGLBuffer.h:52
mrpt::opengl::COpenGLBuffer::COpenGLBuffer
COpenGLBuffer()
Definition: COpenGLBuffer.h:46
mrpt::opengl::COpenGLBuffer::Usage::DynamicCopy
@ DynamicCopy
mrpt::opengl::COpenGLBuffer::Usage::StreamCopy
@ StreamCopy
mrpt::opengl::COpenGLBuffer::Usage::DynamicRead
@ DynamicRead
mrpt::opengl::COpenGLBuffer::Type::PixelPack
@ PixelPack
mrpt::opengl::COpenGLBuffer::Type
Type
Definition: COpenGLBuffer.h:25
mrpt::opengl::COpenGLBuffer::type
Type type() const
Definition: COpenGLBuffer.h:49
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:14
mrpt::opengl::COpenGLBuffer::destroy
void destroy()
Automatically called upon destructor, no need for the user to call it in normal situations.
Definition: COpenGLBuffer.h:68
mrpt::opengl::COpenGLBuffer::RAII_Impl::allocate
void allocate(const void *data, int byteCount)
Definition: COpenGLBuffer.cpp:101
mrpt::opengl::COpenGLBuffer::usage
Usage usage() const
Definition: COpenGLBuffer.h:51
mrpt::opengl::COpenGLBuffer::RAII_Impl
Definition: COpenGLBuffer.h:85
mrpt::opengl::COpenGLBuffer::Usage::StreamDraw
@ StreamDraw
mrpt::opengl::COpenGLBuffer::RAII_Impl::~RAII_Impl
~RAII_Impl()
Definition: COpenGLBuffer.cpp:29
mrpt::opengl::COpenGLBuffer::bind
void bind()
Definition: COpenGLBuffer.h:70



Page generated by Doxygen 1.8.18 for MRPT 2.0.4 at Thu Sep 24 07:14:18 UTC 2020