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
VertexBuffer.cpp
Go to the documentation of this file.
1
7
8#include <glad/glad.h>
9
10#include "VertexBuffer.hpp"
11
12namespace galaxy
13{
14 namespace graphics
15 {
17 : m_id {0}
18 , m_offset {0}
19 , m_count {0}
20 {
21 glCreateBuffers(1, &m_id);
22 }
23
25 {
26 this->m_id = v.m_id;
27 this->m_offset = v.m_offset;
28 this->m_count = v.m_count;
29
30 v.m_id = 0;
31 }
32
34 {
35 if (this != &v)
36 {
37 this->m_id = v.m_id;
38 this->m_offset = v.m_offset;
39 this->m_count = v.m_count;
40
41 v.m_id = 0;
42 }
43
44 return *this;
45 }
46
48 {
49 if (m_id != 0)
50 {
51 glDeleteBuffers(1, &m_id);
52 }
53 }
54
55 void VertexBuffer::buffer(std::span<Vertex> vertices, std::span<unsigned int> indicies)
56 {
57 const auto ind_len = indicies.size_bytes();
58 m_offset = vertices.size_bytes();
59 m_count = static_cast<int>(indicies.size());
60
61 glNamedBufferData(m_id, ind_len + m_offset, nullptr, GL_DYNAMIC_DRAW);
62 glNamedBufferSubData(m_id, m_offset, ind_len, indicies.data());
63 glNamedBufferSubData(m_id, 0, m_offset, vertices.data());
64 }
65
66 void VertexBuffer::buffer(const int vertex_count, std::span<unsigned int> indicies)
67 {
68 const auto ind_len = indicies.size_bytes();
69 m_offset = vertex_count * sizeof(Vertex);
70 m_count = static_cast<int>(indicies.size());
71
72 glNamedBufferData(m_id, ind_len + m_offset, nullptr, GL_DYNAMIC_DRAW);
73 glNamedBufferSubData(m_id, m_offset, ind_len, indicies.data());
74 glNamedBufferSubData(m_id, 0, m_offset, nullptr);
75 }
76
77 void VertexBuffer::sub_buffer(const unsigned int index, std::span<Vertex> vertices)
78 {
79 glNamedBufferSubData(m_id, index * sizeof(Vertex), vertices.size_bytes(), vertices.data());
80 }
81
83 {
84 auto size = 0;
85
86 glGetNamedBufferParameteriv(m_id, GL_BUFFER_SIZE, &size);
87 glNamedBufferData(m_id, size, nullptr, GL_DYNAMIC_DRAW);
88 }
89
91 {
92 return m_count;
93 }
94
96 {
97 return (void*)m_offset;
98 }
99
100 unsigned int VertexBuffer::id() const
101 {
102 return m_id;
103 }
104 } // namespace graphics
105} // namespace galaxy
thread_local const float vertices[]
Video.cpp galaxy.
Definition Video.cpp:19
Abstraction for OpenGL vertex buffer objects.
void clear()
Clear buffer data.
std::size_t m_offset
Index buffer offset.
VertexBuffer & operator=(VertexBuffer &&)
Move assignment operator.
unsigned int m_id
ID returned by OpenGL when generating buffer.
int m_count
Index buffer count.
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.
Animated.cpp galaxy.
Definition Animated.cpp:16
Represents a single vertex point.
Definition Vertex.hpp:25