Untitled Game engine no.5  1.0
Buffer.hpp
1 #ifndef BUFFER_H
2 #define BUFFER_H
3 #include "pch.hpp"
4 #include "Base.hpp"
5 #include "Shader.hpp"
6 #include "Mesh.hpp"
7 
8 namespace Engine {
11  struct BufferElement {
13  std::string name;
18  uint32_t offset;
20  uint32_t size;
21  bool normalized;
22 
27  BufferElement(ShaderDataType type, const std::string& name, bool norm = false)
28  : name(name), type(type),
29  size(ShaderDataTypeSize(type)),
30  offset(0), normalized(norm) {
31 
32  }
33 
35  uint32_t GetComponentCount() const {
36  switch(type) {
37  case ShaderDataType::Int:
38  case ShaderDataType::Bool:
39  case ShaderDataType::Float: return 1;
40  case ShaderDataType::Int2:
41  case ShaderDataType::Float2: return 2;
42  case ShaderDataType::Int3:
43  case ShaderDataType::Float3: return 3;
44  case ShaderDataType::Int4:
45  case ShaderDataType::Float4: return 4;
46  case ShaderDataType::Mat3: return 3 * 3;
47  case ShaderDataType::Mat4: return 4 * 4;
48  default: Log::Warn("BUFFER ELEMENTS","Unknown ShaderDataType"); return 0;
49  }
50  return 0;
51  }
52 
53 
54  };
55 
56 
58  class BufferLayout {
59  private:
61  std::vector<BufferElement> m_Elements;
63  uint32_t m_Stride = 0;
64 
65  public:
67  inline const std::vector<BufferElement>& GetElements() const { return m_Elements; }
69  inline const uint32_t GetStride() const { return m_Stride; }
70 
73  BufferLayout(const std::initializer_list<BufferElement>& elements)
74  : m_Elements(elements) {
75  CalculateOffsetsAndStride();
76  }
79 
81  std::vector<BufferElement>::iterator begin() { return m_Elements.begin(); }
83  std::vector<BufferElement>::iterator end() { return m_Elements.end(); }
84 
85  private:
87  void CalculateOffsetsAndStride() {
88  uint32_t offset = 0;
89  m_Stride = 0;
90  for (auto& e : m_Elements) {
91  e.offset = offset;
92  offset += e.size;
93  m_Stride += e.size;
94  }
95  }
96 
97 
98  };
99 
101  class VertexBuffer {
102 
103  public:
107  virtual ~VertexBuffer(){};
108 
110  virtual void Bind() = 0;
112  virtual void Unbind() = 0;
113 
117  virtual void Load(float verts[], std::size_t size) = 0;
118 
119  virtual std::size_t GetSize() = 0;
120  };
121 
123  class IndexBuffer {
124  public:
128  virtual ~IndexBuffer(){};
129 
131  virtual void Bind() = 0;
133  virtual void Unbind() = 0;
134 
138  virtual void Load(unsigned int inds[], std::size_t size) = 0;
139 
140  virtual std::size_t GetSize() = 0;
141  };
142 }
143 #endif
144 
Engine::BufferElement::type
ShaderDataType type
Definition: Buffer.hpp:16
Engine::BufferLayout
Constructs the layout of a vertex buffer.
Definition: Buffer.hpp:58
Engine::BufferElement::name
std::string name
Name of this element.
Definition: Buffer.hpp:13
Engine::BufferLayout::BufferLayout
BufferLayout()
Creates an empty buffer layout.
Definition: Buffer.hpp:78
Engine::VertexBuffer::~VertexBuffer
virtual ~VertexBuffer()
Constructor.
Definition: Buffer.hpp:107
Engine::BufferElement
Definition: Buffer.hpp:11
Engine::IndexBuffer::Load
virtual void Load(unsigned int inds[], std::size_t size)=0
Engine::VertexBuffer::VertexBuffer
VertexBuffer()
Constructor.
Definition: Buffer.hpp:105
Engine::VertexBuffer
Interface for a buffer of vertices.
Definition: Buffer.hpp:101
Engine::VertexBuffer::Unbind
virtual void Unbind()=0
Binds nothing for drawing.
Engine::ShaderDataType
ShaderDataType
data type enum for shader parameter
Definition: Shader.hpp:11
Engine::VertexBuffer::Bind
virtual void Bind()=0
Binds this vertex buffer for drawing.
Engine::IndexBuffer::Bind
virtual void Bind()=0
Binds this vertex buffer for drawing.
Engine::VertexBuffer::Load
virtual void Load(float verts[], std::size_t size)=0
Engine::IndexBuffer::IndexBuffer
IndexBuffer()
Constructor.
Definition: Buffer.hpp:126
Engine::BufferLayout::GetElements
const std::vector< BufferElement > & GetElements() const
Definition: Buffer.hpp:67
Engine::BufferLayout::GetStride
const uint32_t GetStride() const
@retuns the stride of the elements of this buffer
Definition: Buffer.hpp:69
Engine::BufferElement::GetComponentCount
uint32_t GetComponentCount() const
Get the number of elements in this component.
Definition: Buffer.hpp:35
Engine::IndexBuffer::~IndexBuffer
virtual ~IndexBuffer()
Destructor.
Definition: Buffer.hpp:128
Engine::BufferLayout::BufferLayout
BufferLayout(const std::initializer_list< BufferElement > &elements)
Definition: Buffer.hpp:73
Engine::BufferElement::offset
uint32_t offset
Offset of component in the vertex.
Definition: Buffer.hpp:18
Engine::BufferLayout::end
std::vector< BufferElement >::iterator end()
Wrapper around the elements vectory.
Definition: Buffer.hpp:83
Engine::IndexBuffer::Unbind
virtual void Unbind()=0
Binds nothing for drawing.
Engine::BufferElement::BufferElement
BufferElement(ShaderDataType type, const std::string &name, bool norm=false)
Definition: Buffer.hpp:27
Engine
Definition: Animation.hpp:14
Engine::BufferElement::size
uint32_t size
Size of the datatype element.
Definition: Buffer.hpp:20
Engine::IndexBuffer
Interface for a buffer of indices.
Definition: Buffer.hpp:123
Engine::BufferLayout::begin
std::vector< BufferElement >::iterator begin()
Wrapper around the elements vectory.
Definition: Buffer.hpp:81