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