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
24 {
25 if (this->m_id != 0)
26 {
27 glDeleteVertexArrays(1, &this->m_id);
28 }
29
30 this->m_id = v.m_id;
31 this->m_vbo = std::move(v.m_vbo);
32
33 v.m_id = 0;
34 }
35
37 {
38 if (this != &v)
39 {
40 if (this->m_id != 0)
41 {
42 glDeleteVertexArrays(1, &this->m_id);
43 }
44
45 this->m_id = v.m_id;
46 this->m_vbo = std::move(v.m_vbo);
47
48 v.m_id = 0;
49 }
50
51 return *this;
52 }
53
55 {
56 if (m_id != 0)
57 {
58 glDeleteVertexArrays(1, &m_id);
59 m_id = 0;
60 }
61 }
62
63 void VertexArray::buffer(std::span<Vertex> vertices, std::span<unsigned int> indicies)
64 {
65 m_vbo.buffer(vertices, indicies);
66
67 // Bind vertex to 0 (vertex buffer bind point, different from attribute bind point).
68 glVertexArrayVertexBuffer(m_id, static_cast<unsigned int>(GLBufferBinding::VERTEX_BUFFER_POINT), m_vbo.id(), 0, sizeof(Vertex));
69 glVertexArrayElementBuffer(m_id, m_vbo.id());
70
71 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT)); // Pos
72 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT)); // Texels
73
74 // bind point, size (i.e. vec2, vec3, etc) floats not unsigned ints, not normalized, offset in data structure.
75 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_pos));
76 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_texels));
77
78 // VAO, attribute bind point, vertex buffer bind point.
79 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT), static_cast<unsigned int>(GLBufferBinding::VERTEX_BUFFER_POINT));
80 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT), static_cast<unsigned int>(GLBufferBinding::VERTEX_BUFFER_POINT));
81 }
82
83 void VertexArray::buffer(const int vertex_count, std::span<unsigned int> indicies)
84 {
85 m_vbo.buffer(vertex_count, indicies);
86
87 // Bind vertex to 0 (, different from attribute bind point).
88 glVertexArrayVertexBuffer(m_id, static_cast<unsigned int>(GLBufferBinding::VERTEX_BUFFER_POINT), m_vbo.id(), 0, sizeof(Vertex));
89 glVertexArrayElementBuffer(m_id, m_vbo.id());
90
91 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT)); // Pos
92 glEnableVertexArrayAttrib(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT)); // Texels
93
94 // bind point, size (i.e. vec2, vec3, etc) floats not unsigned ints, not normalized, offset in data structure.
95 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_pos));
96 glVertexArrayAttribFormat(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT), 2, GL_FLOAT, GL_FALSE, offsetof(Vertex, m_texels));
97
98 // VAO, attribute bind point, vertex buffer bind point.
99 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(GLAttributeBinding::POSITION_POINT), static_cast<unsigned int>(GLBufferBinding::VERTEX_BUFFER_POINT));
100 glVertexArrayAttribBinding(m_id, static_cast<unsigned int>(GLAttributeBinding::TEXEL_POINT), static_cast<unsigned int>(GLBufferBinding::VERTEX_BUFFER_POINT));
101 }
102
103 void VertexArray::sub_buffer(const unsigned int index, std::span<Vertex> vertices)
104 {
105 m_vbo.sub_buffer(index, vertices);
106 }
107
108 void VertexArray::bind() const
109 {
110 glBindVertexArray(m_id);
111 }
112
114 {
115 glBindVertexArray(0);
116 }
117
118 int VertexArray::count() const noexcept
119 {
120 return m_vbo.count();
121 }
122
123 void* VertexArray::offset() noexcept
124 {
125 return m_vbo.offset();
126 }
127
129 {
130 return m_vbo;
131 }
132
133 unsigned int VertexArray::id() const noexcept
134 {
135 return m_id;
136 }
137} // namespace galaxy
thread_local const float vertices[]
Video.cpp galaxy.
Definition Video.cpp:19
Abstraction for OpenGL vertex array objects.
VertexArray()
Constructor.
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex array.
void bind() const
Bind this vertex array.
~VertexArray()
Destructor.
VertexBuffer m_vbo
Vertex buffer.
VertexBuffer & vbo() noexcept
Get vertex buffer.
int count() const noexcept
Get the index count.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex array object.
VertexArray & operator=(VertexArray &&) noexcept
Move assignment operator.
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.
Abstraction for OpenGL vertex buffer objects.
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex buffer object.
int count() const noexcept
Get the index count.
void * offset() noexcept
Gets index offset.
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex object.
unsigned int id() const noexcept
Get OpenGL handle.
Animated.cpp galaxy.
Definition Animated.cpp:16
@ POSITION_POINT
Position data.
@ VERTEX_BUFFER_POINT
Normal VBO.
Represents a single vertex point.
Definition Vertex.hpp:23