galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
VertexBatch.cpp
Go to the documentation of this file.
1
7
8#include "VertexBatch.hpp"
9
10namespace galaxy
11{
12 VertexBatch::VertexBatch(const int max, const int vertex_count, const std::vector<unsigned int>& indices) noexcept
13 : m_vertex_count {0}
14 , m_index_count {0}
15 , m_count {0}
16 {
17 m_index_count = static_cast<int>(indices.size());
18 m_vertex_length = max * vertex_count;
19 m_index_length = max * m_index_count;
20
21 m_vertices.resize(m_vertex_length, {});
22 m_vao.reserve(m_vertex_length, m_index_length);
23
24 // Calculate fixed indices and push once.
25 std::vector<unsigned int> index_data;
26 index_data.reserve(m_index_length);
27
28 auto offset = 0;
29 for (auto i = 0; i < max; ++i)
30 {
31 for (auto j = 0; j < m_index_count; ++j)
32 {
33 index_data.emplace_back(indices[j] + offset);
34 }
35
36 // Each set of 6 indices needs to be offset by vertex_count per renderable (max in this case).
37 offset += vertex_count;
38 }
39
40 m_vao.sub_buffer_indices(0, m_index_length, index_data);
41 }
42
44 {
45 this->m_vertex_count = vb.m_vertex_count;
46 this->m_index_count = vb.m_index_count;
47 this->m_count = vb.m_count;
48 this->m_vertex_length = vb.m_vertex_length;
49 this->m_index_length = vb.m_index_length;
50 this->m_vertices = std::move(vb.m_vertices);
51 this->m_vao = std::move(vb.m_vao);
52 }
53
55 {
56 if (this != &vb)
57 {
58 this->m_vertex_count = vb.m_vertex_count;
59 this->m_index_count = vb.m_index_count;
60 this->m_count = vb.m_count;
61 this->m_vertex_length = vb.m_vertex_length;
62 this->m_index_length = vb.m_index_length;
63 this->m_vertices = std::move(vb.m_vertices);
64 this->m_vao = std::move(vb.m_vao);
65 }
66
67 return *this;
68 }
69
71 {
72 }
73
74 void VertexBatch::prepare() noexcept
75 {
76 m_count = 0;
78 }
79
80 void VertexBatch::push(const std::vector<Vertex>& vertices) noexcept
81 {
82 for (auto i = 0; i < vertices.size(); ++i)
83 {
84 m_vertices[m_vertex_count + i].m_pos = vertices[i].m_pos;
85 m_vertices[m_vertex_count + i].m_texels = vertices[i].m_texels;
86 m_vertices[m_vertex_count + i].m_index = vertices[i].m_index;
87 }
88
89 m_vertex_count += static_cast<int>(vertices.size());
90 m_count += m_index_count; // each set of 6 indices is 1 renderable to draw.
91 }
92
97
98 void VertexBatch::bind() const noexcept
99 {
100 m_vao.bind();
101 }
102
103 int VertexBatch::count() const noexcept
104 {
105 return m_count;
106 }
107
108 void* VertexBatch::offset() noexcept
109 {
110 return m_vao.offset();
111 }
112
114 {
115 return m_vao;
116 }
117} // namespace galaxy
thread_local const float vertices[]
Video.cpp galaxy.
Definition Video.cpp:19
thread_local const unsigned int indices[]
Definition Video.cpp:21
Abstraction for OpenGL vertex array objects.
void bind() const
Bind this vertex array.
void * offset() noexcept
Gets index offset.
void sub_buffer_vertices(const unsigned int vi, const int vertex_size, const std::span< Vertex > vertices) const
Sub-buffer vertex buffer.
A group of vertices for multiple renderables batched together.
int m_vertex_count
Amount of vertices to flush.
void bind() const noexcept
Bind this batchs vertex array object.
VertexBatch()=delete
Constructor.
std::vector< Vertex > m_vertices
CPU side reserved vertices.
VertexArray m_vao
VAO object for GPU data.
void push(const std::vector< Vertex > &vertices) noexcept
Add vertex and index data to batch.
VertexBatch & operator=(VertexBatch &&) noexcept
Move assignment operator.
void flush() noexcept
Copy all data to GPU.
int count() const noexcept
Get the index count.
void prepare() noexcept
Sets up counters to begin pushing vertices.
~VertexBatch() noexcept
Destructor.
int m_count
Amount of renderables to draw.
void * offset() noexcept
Gets index offset.
VertexArray & vao() noexcept
Gets internal VAO.
Animated.cpp galaxy.
Definition Animated.cpp:16