Untitled Game engine no.5  1.0
GLBuffer.hpp
1 #ifndef GLBUFFER_H
2 #define GLBUFFER_H
3 #include <cstddef>
4 #include "Buffer.hpp"
5 #include "GLUtil.hpp"
6 namespace Engine {
10  class GLVertexBuffer : public VertexBuffer {
11  private:
12  unsigned int m_id = 0;
13  size_t m_size{0};
14  size_t m_offset{0};
15 
16  public:
17  GLVertexBuffer(std::size_t size){
18  // create buffer
19  GLCall( glGenBuffers(1, &m_id) );
20  // bind it to array
21  GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_id) );
22  m_size = size;
23  GLCall( glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_DYNAMIC_DRAW) );
24  };
25 
26  ~GLVertexBuffer() {
27  GLCall( glDeleteBuffers(1, &m_id) );
28  };
29 
30  void Bind() override {
31  GLCall( glBindBuffer(GL_ARRAY_BUFFER, m_id) );
32  };
33 
34  void Unbind() override {
35  GLCall( glBindBuffer(GL_ARRAY_BUFFER, 0) );
36  };
37 
38  void Load(float verts[], std::size_t size) override {
39  // if size is zero then we will take this to mean that we are reseting
40  if (size == 0) {
41  m_offset = 0;
42  return;
43  }
44  GLCall( glBufferSubData(GL_ARRAY_BUFFER, m_offset, size, verts) );
45  m_offset += size;
46  };
47 
48  size_t GetSize() override { return m_size; };
49  };
50 
51  class GLIndexBuffer : public IndexBuffer {
52  private:
53  unsigned int m_id = 0;
54  size_t m_size = 0;
55  public:
56  GLIndexBuffer() {
57  // create buffer
58  GLCall( glGenBuffers(1, &m_id) );
59  // bind it to array
60  GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id) );
61  };
62 
63  ~GLIndexBuffer(){
64  GLCall( glDeleteBuffers(1, &m_id) );
65  };
66 
67  void Bind() override {
68  GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id) );
69  }
70 
71  void Unbind() override {
72  GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
73  }
74 
75  void Load(unsigned int inds[], std::size_t size) override {
76  m_size = size;
77  GLCall( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id) );
78  GLCall( glBufferData(GL_ELEMENT_ARRAY_BUFFER,
79  size,
80  inds, GL_DYNAMIC_DRAW) );
81  };
82 
83  size_t GetSize() override { return m_size; };
84  };
85 
86 }
87 #endif
Engine::GLVertexBuffer::Unbind
void Unbind() override
Binds nothing for drawing.
Definition: GLBuffer.hpp:34
Engine::VertexBuffer
Interface for a buffer of vertices.
Definition: Buffer.hpp:101
Engine::GLVertexBuffer::Bind
void Bind() override
Binds this vertex buffer for drawing.
Definition: GLBuffer.hpp:30
Engine::GLVertexBuffer::Load
void Load(float verts[], std::size_t size) override
Definition: GLBuffer.hpp:38
Engine::GLIndexBuffer::Bind
void Bind() override
Binds this vertex buffer for drawing.
Definition: GLBuffer.hpp:67
Engine::GLIndexBuffer::Unbind
void Unbind() override
Binds nothing for drawing.
Definition: GLBuffer.hpp:71
Engine::GLIndexBuffer
Definition: GLBuffer.hpp:51
Engine::GLVertexBuffer
Definition: GLBuffer.hpp:10
Engine
Definition: Animation.hpp:14
Engine::IndexBuffer
Interface for a buffer of indices.
Definition: Buffer.hpp:123
Engine::GLIndexBuffer::Load
void Load(unsigned int inds[], std::size_t size) override
Definition: GLBuffer.hpp:75