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
VertexArray.cpp
Go to the documentation of this file.
1
7
8#include <glm/mat4x4.hpp>
9
11
12#include "VertexArray.hpp"
13
14namespace galaxy
15{
16 namespace graphics
17 {
19 : m_id {0}
20 , m_instances {1}
21 {
22 glCreateVertexArrays(1, &m_id);
23 }
24
26 {
27 if (this->m_id != 0)
28 {
29 glDeleteVertexArrays(1, &this->m_id);
30 }
31
32 this->m_id = v.m_id;
33 this->m_vbo = std::move(v.m_vbo);
34 this->m_instances = v.m_instances;
35
36 v.m_id = 0;
37 }
38
40 {
41 if (this != &v)
42 {
43 if (this->m_id != 0)
44 {
45 glDeleteVertexArrays(1, &this->m_id);
46 }
47
48 this->m_id = v.m_id;
49 this->m_vbo = std::move(v.m_vbo);
50 this->m_instances = v.m_instances;
51
52 v.m_id = 0;
53 }
54
55 return *this;
56 }
57
59 {
60 if (m_id != 0)
61 {
62 glDeleteVertexArrays(1, &m_id);
63 }
64 }
65
66 void VertexArray::buffer(std::span<Vertex> vertices, std::span<unsigned int> indicies)
67 {
68 m_vbo.buffer(vertices, indicies);
69
70 // Bind vertex to 0 (vertex buffer bind point, different from attribute bind point).
71 glVertexArrayVertexBuffer(m_id, static_cast<unsigned int>(BufferBinding::VERTEX_BUFFER_POINT), m_vbo.id(), 0, sizeof(Vertex));
72 glVertexArrayElementBuffer(m_id, m_vbo.id());
73
74 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(AttributeBinding::POSITION_POINT)); // Pos
75 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(AttributeBinding::TEXEL_POINT)); // Texels
76
77 // bind point, size (i.e. vec2, vec3, etc) floats not unsigned ints, not normalized, offset in data structure.
78 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(AttributeBinding::POSITION_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_pos));
79 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(AttributeBinding::TEXEL_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_texels));
80
81 // VAO, attribute bind point, vertex buffer bind point.
82 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(AttributeBinding::POSITION_POINT), static_cast<unsigned int>(BufferBinding::VERTEX_BUFFER_POINT));
83 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(AttributeBinding::TEXEL_POINT), static_cast<unsigned int>(BufferBinding::VERTEX_BUFFER_POINT));
84 }
85
86 void VertexArray::buffer(const int vertex_count, std::span<unsigned int> indicies)
87 {
88 m_vbo.buffer(vertex_count, indicies);
89
90 // Bind vertex to 0 (, different from attribute bind point).
91 glVertexArrayVertexBuffer(m_id, static_cast<unsigned int>(BufferBinding::VERTEX_BUFFER_POINT), m_vbo.id(), 0, sizeof(Vertex));
92 glVertexArrayElementBuffer(m_id, m_vbo.id());
93
94 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(AttributeBinding::POSITION_POINT)); // Pos
95 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(AttributeBinding::TEXEL_POINT)); // Texels
96
97 // bind point, size (i.e. vec2, vec3, etc) floats not unsigned ints, not normalized, offset in data structure.
98 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(AttributeBinding::POSITION_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_pos));
99 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(AttributeBinding::TEXEL_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_texels));
100
101 // VAO, attribute bind point, vertex buffer bind point.
102 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(AttributeBinding::POSITION_POINT), static_cast<unsigned int>(BufferBinding::VERTEX_BUFFER_POINT));
103 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(AttributeBinding::TEXEL_POINT), static_cast<unsigned int>(BufferBinding::VERTEX_BUFFER_POINT));
104 }
105
106 void VertexArray::sub_buffer(const unsigned int index, std::span<Vertex> vertices)
107 {
108 m_vbo.sub_buffer(index, vertices);
109 }
110
112 {
113 m_instances = ib.amount();
114
115 glVertexArrayVertexBuffer(m_id, static_cast<unsigned int>(BufferBinding::INSTANCE_BUFFER_POINT), ib.id(), 0, sizeof(glm::mat4));
116
117 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT));
118 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 1u);
119 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 2u);
120 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 3u);
121
122 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT), 4, GL_FLOAT, GL_FALSE, 0);
123 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 1u, 4, GL_FLOAT, GL_FALSE, (sizeof(glm::vec4)));
124 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 2u, 4, GL_FLOAT, GL_FALSE, (2 * sizeof(glm::vec4)));
125 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 3u, 4, GL_FLOAT, GL_FALSE, (3 * sizeof(glm::vec4)));
126
127 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT), static_cast<unsigned int>(BufferBinding::INSTANCE_BUFFER_POINT));
128 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 1u, static_cast<unsigned int>(BufferBinding::INSTANCE_BUFFER_POINT));
129 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 2u, static_cast<unsigned int>(BufferBinding::INSTANCE_BUFFER_POINT));
130 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 3u, static_cast<unsigned int>(BufferBinding::INSTANCE_BUFFER_POINT));
131
132 glVertexArrayBindingDivisor(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT), 1);
133 glVertexArrayBindingDivisor(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 1u, 1);
134 glVertexArrayBindingDivisor(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 2u, 1);
135 glVertexArrayBindingDivisor(m_id, static_cast<unsigned int>(AttributeBinding::OFFSET_POINT) + 3u, 1);
136 }
137
139 {
140 glBindVertexArray(m_id);
141 }
142
144 {
145 glBindVertexArray(0);
146 }
147
149 {
150 return m_vbo.count();
151 }
152
154 {
155 return m_vbo.offset();
156 }
157
159 {
160 return m_instances;
161 }
162
164 {
165 return m_vbo;
166 }
167
168 unsigned int VertexArray::id() const
169 {
170 return m_id;
171 }
172 } // namespace graphics
173} // namespace galaxy
thread_local const float vertices[]
Video.cpp galaxy.
Definition Video.cpp:19
Abstraction for OpenGL vertex buffer objects.
unsigned int id() const
Get OpenGL handle.
int amount() const
Get the amount of instances.
Abstraction for OpenGL vertex array objects.
VertexArray & operator=(VertexArray &&)
Move assignment operator.
VertexBuffer m_vbo
Vertex buffer.
int m_instances
Number of instances.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex array object.
unsigned int m_id
VAO object.
int count() const
Get the index count.
void * offset()
Gets index offset.
int instances() const
Number of instances to render.
void unbind()
Unbind this vertex array.
void set_instanced(const InstanceBuffer &ib)
Set this vertex array to use a specific instance buffer.
void bind()
Bind this vertex array.
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex array.
VertexBuffer & vbo()
Get vertex buffer.
unsigned int id() const
Get vertex array handle.
Abstraction for OpenGL vertex buffer objects.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex buffer object.
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex object.
unsigned int id() const
Get OpenGL handle.
void * offset()
Gets index offset.
int count() const
Get the index count.
@ OFFSET_POINT
Instance offset data.
@ INSTANCE_BUFFER_POINT
Instance offsets.
Animated.cpp galaxy.
Definition Animated.cpp:16
Represents a single vertex point.
Definition Vertex.hpp:25