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
10
#include "
galaxy/graphics/gl/Enums.hpp
"
11
12
#include "
VertexArray.hpp
"
13
14
namespace
galaxy
15
{
16
namespace
graphics
17
{
18
VertexArray::VertexArray
()
19
: m_id {0}
20
, m_instances {1}
21
{
22
glCreateVertexArrays(1, &
m_id
);
23
}
24
25
VertexArray::VertexArray
(
VertexArray
&& v)
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
39
VertexArray
&
VertexArray::operator=
(
VertexArray
&& v)
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
58
VertexArray::~VertexArray
()
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
111
void
VertexArray::set_instanced
(
const
InstanceBuffer
& ib)
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
138
void
VertexArray::bind
()
139
{
140
glBindVertexArray(
m_id
);
141
}
142
143
void
VertexArray::unbind
()
144
{
145
glBindVertexArray(0);
146
}
147
148
int
VertexArray::count
()
const
149
{
150
return
m_vbo
.
count
();
151
}
152
153
void
*
VertexArray::offset
()
154
{
155
return
m_vbo
.
offset
();
156
}
157
158
int
VertexArray::instances
()
const
159
{
160
return
m_instances
;
161
}
162
163
VertexBuffer
&
VertexArray::vbo
()
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
Enums.hpp
VertexArray.hpp
vertices
thread_local const float vertices[]
Video.cpp galaxy.
Definition
Video.cpp:19
galaxy::graphics::InstanceBuffer
Abstraction for OpenGL vertex buffer objects.
Definition
InstanceBuffer.hpp:23
galaxy::graphics::InstanceBuffer::id
unsigned int id() const
Get OpenGL handle.
Definition
InstanceBuffer.cpp:74
galaxy::graphics::InstanceBuffer::amount
int amount() const
Get the amount of instances.
Definition
InstanceBuffer.cpp:69
galaxy::graphics::VertexArray
Abstraction for OpenGL vertex array objects.
Definition
VertexArray.hpp:22
galaxy::graphics::VertexArray::operator=
VertexArray & operator=(VertexArray &&)
Move assignment operator.
Definition
VertexArray.cpp:39
galaxy::graphics::VertexArray::m_vbo
VertexBuffer m_vbo
Vertex buffer.
Definition
VertexArray.hpp:145
galaxy::graphics::VertexArray::~VertexArray
~VertexArray()
Destructor.
Definition
VertexArray.cpp:58
galaxy::graphics::VertexArray::m_instances
int m_instances
Number of instances.
Definition
VertexArray.hpp:150
galaxy::graphics::VertexArray::buffer
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex array object.
Definition
VertexArray.cpp:66
galaxy::graphics::VertexArray::m_id
unsigned int m_id
VAO object.
Definition
VertexArray.hpp:140
galaxy::graphics::VertexArray::count
int count() const
Get the index count.
Definition
VertexArray.cpp:148
galaxy::graphics::VertexArray::offset
void * offset()
Gets index offset.
Definition
VertexArray.cpp:153
galaxy::graphics::VertexArray::instances
int instances() const
Number of instances to render.
Definition
VertexArray.cpp:158
galaxy::graphics::VertexArray::unbind
void unbind()
Unbind this vertex array.
Definition
VertexArray.cpp:143
galaxy::graphics::VertexArray::set_instanced
void set_instanced(const InstanceBuffer &ib)
Set this vertex array to use a specific instance buffer.
Definition
VertexArray.cpp:111
galaxy::graphics::VertexArray::bind
void bind()
Bind this vertex array.
Definition
VertexArray.cpp:138
galaxy::graphics::VertexArray::sub_buffer
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex array.
Definition
VertexArray.cpp:106
galaxy::graphics::VertexArray::vbo
VertexBuffer & vbo()
Get vertex buffer.
Definition
VertexArray.cpp:163
galaxy::graphics::VertexArray::VertexArray
VertexArray()
Constructor.
Definition
VertexArray.cpp:18
galaxy::graphics::VertexArray::id
unsigned int id() const
Get vertex array handle.
Definition
VertexArray.cpp:168
galaxy::graphics::VertexBuffer
Abstraction for OpenGL vertex buffer objects.
Definition
VertexBuffer.hpp:23
galaxy::graphics::VertexBuffer::buffer
void buffer(std::span< Vertex > vertices, std::span< unsigned int > indicies)
Create vertex buffer object.
Definition
VertexBuffer.cpp:55
galaxy::graphics::VertexBuffer::sub_buffer
void sub_buffer(const unsigned int index, std::span< Vertex > vertices)
Sub-buffer vertex object.
Definition
VertexBuffer.cpp:77
galaxy::graphics::VertexBuffer::id
unsigned int id() const
Get OpenGL handle.
Definition
VertexBuffer.cpp:100
galaxy::graphics::VertexBuffer::offset
void * offset()
Gets index offset.
Definition
VertexBuffer.cpp:95
galaxy::graphics::VertexBuffer::count
int count() const
Get the index count.
Definition
VertexBuffer.cpp:90
galaxy::graphics::AttributeBinding::OFFSET_POINT
@ OFFSET_POINT
Instance offset data.
galaxy::graphics::AttributeBinding::TEXEL_POINT
@ TEXEL_POINT
Texel data.
galaxy::graphics::AttributeBinding::POSITION_POINT
@ POSITION_POINT
Position data.
galaxy::graphics::BufferBinding::INSTANCE_BUFFER_POINT
@ INSTANCE_BUFFER_POINT
Instance offsets.
galaxy::graphics::BufferBinding::VERTEX_BUFFER_POINT
@ VERTEX_BUFFER_POINT
Normal VBO.
galaxy
Animated.cpp galaxy.
Definition
Animated.cpp:16
galaxy::graphics::Vertex
Represents a single vertex point.
Definition
Vertex.hpp:25
galaxy
src
galaxy
graphics
gl
VertexArray.cpp
Generated on Fri Sep 12 2025 07:29:47 for galaxy by
1.12.0