Kale
Loading...
Searching...
No Matches
Buffer.hpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
19#ifdef KALE_OPENGL
20
22
23#include <vector>
24#include <array>
25#include <algorithm>
26
27#include <glad/glad.h>
28
29namespace Kale::OpenGL {
30
34 enum class BufferType : GLenum {
35 ElementBuffer = GL_ELEMENT_ARRAY_BUFFER,
36 VertexBuffer = GL_ARRAY_BUFFER,
37 TextureBuffer = GL_TEXTURE_BUFFER
38 };
39
43 enum class BufferUsage : GLenum {
44 Static = GL_STATIC_DRAW,
45 Dynamic = GL_DYNAMIC_DRAW
46 };
47
51 template <typename T>
52 class Buffer {
53 private:
54
58 unsigned int buffer;
59
64
65 public:
66
70 std::vector<T> data;
71
77 bind();
78 glBufferData(getEnumValue(type), sizeof(T) * data.size(), data.data(), getEnumValue(usage));
79 }
80
84 void updateBuffer() {
85 bind();
86 glBufferSubData(getEnumValue<BufferType>(type), 0, sizeof(T) * data.size(), data.data());
87 }
88
94 glGenBuffers(1, &buffer);
95 bind();
96 }
97
104 template <size_t N> Buffer(BufferType type, BufferUsage usage, const std::array<T, N>& data) : type(type),
105 data(data.begin(), data.end()) {
106 glGenBuffers(1, &buffer);
107 allocBuffer(usage);
108 }
109
116 Buffer(BufferType type, BufferUsage usage, const std::vector<T>& dat) : type(type), data(dat) {
117 glGenBuffers(1, &buffer);
118 allocBuffer(usage);
119 }
120
127 Buffer(BufferType type, BufferUsage usage, std::vector<T>&& dat) : type(type), data(std::move(dat)) {
128 glGenBuffers(1, &buffer);
129 allocBuffer(usage);
130 }
131
139 Buffer(BufferType type, BufferUsage usage, const T* arr, size_t n) : type(type), data(arr, arr + n) {
140 glGenBuffers(1, &buffer);
141 allocBuffer(usage);
142 }
143
147 Buffer(const Buffer& other) = delete;
148
152 void operator=(const Buffer& other) = delete;
153
158 glDeleteBuffers(1, &buffer);
159 }
160
164 [[nodiscard]] size_t size() const {
165 return data.size();
166 }
167
173 [[nodiscard]] const T& operator[](size_t i) const {
174 return data[i];
175 }
176
182 [[nodiscard]] T& operator[](size_t i) {
183 return data[i];
184 }
185
189 typename std::vector<T>::iterator begin() {
190 return data.begin();
191 }
192
196 typename std::vector<T>::iterator end() {
197 return data.end();
198 }
199
203 typename std::vector<T>::const_iterator cbegin() const {
204 return data.cbegin();
205 }
206
210 typename std::vector<T>::const_iterator cend() const {
211 return data.cend();
212 }
213
217 void bind() const {
218 glBindBuffer(getEnumValue<BufferType>(type), buffer);
219 }
220
226 template <size_t N> void modify(size_t i, const std::array<T, N>& val) {
227 std::copy(data.begin() + i, data.begin() + i + N, val.data());
228 bind();
229 glBufferSubData(getEnumValue<BufferType>(type), sizeof(T) * i, sizeof(T) * N, data.data());
230 }
231
237 void modify(size_t i, const std::vector<T>& val) {
238 std::copy(data.begin() + i, data.begin() + i + val.size(), val.data());
239 bind();
240 glBufferSubData(getEnumValue<BufferType>(type), sizeof(T) * i, sizeof(T) * val.size(), data.data());
241 }
242
249 void modify(size_t i, const T* arr, size_t n) {
250 std::copy(data.begin() + i, data.begin() + i + n, arr);
251 bind();
252 glBufferSubData(getEnumValue<BufferType>(type), sizeof(T) * i, sizeof(T) * n, data.data());
253 }
254
260 void modify(size_t i, T val) {
261 data[i] = val;
262 bind();
263 glBufferSubData(getEnumValue<BufferType>(type), sizeof(T) * i, sizeof(T) * val.size(), data.data());
264 }
265
271 void resize(BufferUsage usage, size_t newSize) {
272 data.resize(newSize);
273 allocBuffer(usage);
274 }
275
281 template <size_t N> void resize(BufferUsage usage, const std::array<T, N>& arr) {
282 data.resize(N);
283 std::copy(data.begin(), data.end(), arr.data());
284 allocBuffer(usage);
285 }
286
292 void resize(BufferUsage usage, const std::vector<T>& vec) {
293 data = vec;
294 allocBuffer(usage);
295 }
296
302 void resize(BufferUsage usage, std::vector<T>&& vec) {
303 data = std::move(vec);
304 allocBuffer(usage);
305 }
306
313 void resize(BufferUsage usage, const T* arr, size_t n) {
314 data.clear();
315 data.insert(data.begin(), arr, arr + n);
316 allocBuffer(usage);
317 }
318
319 };
320}
321
322#endif
void resize(BufferUsage usage, const std::array< T, N > &arr)
Definition Buffer.hpp:281
std::vector< T >::const_iterator cend() const
Definition Buffer.hpp:210
void resize(BufferUsage usage, std::vector< T > &&vec)
Definition Buffer.hpp:302
Buffer(BufferType type, BufferUsage usage, std::vector< T > &&dat)
Definition Buffer.hpp:127
std::vector< T >::iterator end()
Definition Buffer.hpp:196
void modify(size_t i, const std::vector< T > &val)
Definition Buffer.hpp:237
void allocBuffer(BufferUsage usage)
Definition Buffer.hpp:76
Buffer(BufferType type, BufferUsage usage, const std::array< T, N > &data)
Definition Buffer.hpp:104
void resize(BufferUsage usage, size_t newSize)
Definition Buffer.hpp:271
T & operator[](size_t i)
Definition Buffer.hpp:182
void modify(size_t i, T val)
Definition Buffer.hpp:260
Buffer(const Buffer &other)=delete
Buffer(BufferType type)
Definition Buffer.hpp:93
void resize(BufferUsage usage, const std::vector< T > &vec)
Definition Buffer.hpp:292
Buffer(BufferType type, BufferUsage usage, const T *arr, size_t n)
Definition Buffer.hpp:139
void modify(size_t i, const std::array< T, N > &val)
Definition Buffer.hpp:226
void bind() const
Definition Buffer.hpp:217
void resize(BufferUsage usage, const T *arr, size_t n)
Definition Buffer.hpp:313
size_t size() const
Definition Buffer.hpp:164
void modify(size_t i, const T *arr, size_t n)
Definition Buffer.hpp:249
std::vector< T > data
Definition Buffer.hpp:70
void operator=(const Buffer &other)=delete
unsigned int buffer
Definition Buffer.hpp:58
std::vector< T >::iterator begin()
Definition Buffer.hpp:189
Buffer(BufferType type, BufferUsage usage, const std::vector< T > &dat)
Definition Buffer.hpp:116
const T & operator[](size_t i) const
Definition Buffer.hpp:173
BufferType type
Definition Buffer.hpp:63
std::vector< T >::const_iterator cbegin() const
Definition Buffer.hpp:203
GLenum getEnumValue(T value)
Definition Utils.hpp:31