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
12
13#include "VertexArray.hpp"
14
15namespace galaxy
16{
18 : m_id {0}
19 {
20 glCreateVertexArrays(1, &m_id);
21
22 // Bind vertex to 0 (vertex buffer bind point, different from attribute bind point).
23 // Then we bind the vbo as our ebo, since we are using a shared buffer.
24 glVertexArrayVertexBuffer(m_id, static_cast<unsigned int>(GLBufferLocation::VERTEX_BUFFER_POINT), m_vbo.id(), 0, sizeof(Vertex));
25 glVertexArrayElementBuffer(m_id, m_vbo.id());
26
27 // Enable each binding point for the vertex attributes.
28 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT));
29 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT));
30 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(GLAttributeBinding::INDEX_POINT));
31
32 // Specify each binding point for the vertex attributes.
33 // size (i.e. vec2, vec3, etc) floats not unsigned ints, not normalized, offset in data structure.
34 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT), 3, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_pos));
35 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_texels));
36 glVertexArrayAttribIFormat(m_id, static_cast<unsigned int>(GLAttributeBinding::INDEX_POINT), 1, GL_UNSIGNED_INT, offsetof(Vertex, m_index));
37
38 // VAO, attribute bind point, vertex buffer bind point.
39 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT), static_cast<unsigned int>(GLBufferLocation::VERTEX_BUFFER_POINT));
40 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT), static_cast<unsigned int>(GLBufferLocation::VERTEX_BUFFER_POINT));
41 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(GLAttributeBinding::INDEX_POINT), static_cast<unsigned int>(GLBufferLocation::VERTEX_BUFFER_POINT));
42 }
43
45 {
46 if (this->m_id != 0)
47 {
48 glDeleteVertexArrays(1, &this->m_id);
49 }
50
51 this->m_id = v.m_id;
52 this->m_vbo = std::move(v.m_vbo);
53
54 v.m_id = 0;
55 }
56
58 {
59 if (this != &v)
60 {
61 if (this->m_id != 0)
62 {
63 glDeleteVertexArrays(1, &this->m_id);
64 }
65
66 this->m_id = v.m_id;
67 this->m_vbo = std::move(v.m_vbo);
68
69 v.m_id = 0;
70 }
71
72 return *this;
73 }
74
76 {
77 if (m_id != 0)
78 {
79 glDeleteVertexArrays(1, &m_id);
80 }
81 }
82
83 void VertexArray::buffer(std::span<Vertex> vertices, std::span<unsigned int> indices)
84 {
86 }
87
88 void VertexArray::reserve(const int vertex_count, const int index_count)
89 {
90 m_vbo.reserve(vertex_count, index_count);
91 }
92
94 const unsigned int vi,
95 const int vertex_size,
96 const std::span<Vertex> vertices,
97 const unsigned int ei,
98 const int index_size,
99 std::span<unsigned int> indices
100 )
101 {
102 m_vbo.sub_buffer(vi, vertex_size, vertices, ei, index_size, indices);
103 }
104
105 void VertexArray::sub_buffer_vertices(const unsigned int vi, const int vertex_size, const std::span<Vertex> vertices) const
106 {
107 m_vbo.sub_buffer_vertices(vi, vertex_size, vertices);
108 }
109
110 void VertexArray::sub_buffer_indices(const unsigned int ei, const int index_size, std::span<unsigned int> indices) const
111 {
112 m_vbo.sub_buffer_indices(ei, index_size, indices);
113 }
114
115 void VertexArray::erase(const unsigned int vi, const int vertex_count, const unsigned int ei, const int index_count)
116 {
117 m_vbo.erase(vi, vertex_count, ei, index_count);
118 }
119
121 {
122 m_vbo.clear();
123 }
124
125 void VertexArray::bind() const
126 {
127 glBindVertexArray(m_id);
128 }
129
131 {
132 glBindVertexArray(0);
133 }
134
135 int VertexArray::count() const noexcept
136 {
137 return m_vbo.count();
138 }
139
140 void* VertexArray::offset() noexcept
141 {
142 return m_vbo.offset();
143 }
144
146 {
147 return m_vbo;
148 }
149
150 unsigned int VertexArray::id() const noexcept
151 {
152 return m_id;
153 }
154} // 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.
VertexArray()
Constructor.
void bind() const
Bind this vertex array.
~VertexArray()
Destructor.
VertexBuffer m_vbo
Vertex buffer.
VertexBuffer & vbo() noexcept
Get vertex buffer.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indices)
Create vertex buffer.
void sub_buffer(const unsigned int vi, const int vertex_size, const std::span< Vertex > vertices, const unsigned int ei, const int index_size, std::span< unsigned int > indices)
Sub-buffer vertex buffer.
int count() const noexcept
Get the index count.
void sub_buffer_indices(const unsigned int ei, const int index_size, std::span< unsigned int > indices) const
Sub-buffer element/index buffer.
VertexArray & operator=(VertexArray &&) noexcept
Move assignment operator.
void erase(const unsigned int vi, const int vertex_count, const unsigned int ei, const int index_count)
Erase a specfic segment of data.
void unbind() const
Unbind this vertex array.
unsigned int m_id
VAO object.
unsigned int id() const noexcept
Get vertex array handle.
void * offset() noexcept
Gets index offset.
void clear()
Clear buffer data.
void reserve(const int vertex_count, const int index_count)
Create vertex buffer without uploading.
void sub_buffer_vertices(const unsigned int vi, const int vertex_size, const std::span< Vertex > vertices) const
Sub-buffer vertex buffer.
Abstraction for OpenGL vertex buffer objects.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indices)
Create vertex buffer.
int count() const noexcept
Get the index count.
void sub_buffer_indices(const unsigned int ei, const int index_size, std::span< unsigned int > indices) const
Sub-buffer element/index buffer.
void sub_buffer_vertices(const unsigned int vi, const int vertex_size, const std::span< Vertex > vertices) const
Sub-buffer vertex buffer.
void sub_buffer(const unsigned int vi, const int vertex_size, const std::span< Vertex > vertices, const unsigned int ei, const int index_size, std::span< unsigned int > indices) const
Sub-buffer vertex buffer.
void clear() const
Clear buffer data.
void erase(const unsigned int vi, const int vertex_count, const unsigned int ei, const int index_count) const
Erase a specfic segment of data.
void * offset() noexcept
Gets index offset.
void reserve(const int vertex_count, const int index_count)
Create vertex buffer without uploading.
unsigned int id() const noexcept
Get OpenGL handle.
Animated.cpp galaxy.
Definition Animated.cpp:16
@ INDEX_POINT
Renderable uniform data index.
@ POSITION_POINT
Position data.
@ VERTEX_BUFFER_POINT
Normal VBO.
Represents a single vertex point.
Definition Vertex.hpp:24