GlistEngine
Loading...
Searching...
No Matches
gUbo.h
Go to the documentation of this file.
1//
2// Created by irrel on 21.12.2023.
3//
4
5#pragma once
6
7#include "gObject.h"
8#include "gRenderer.h"
9#include "gRenderObject.h"
10
11template<typename T>
12class gUbo : public gRenderObject {
13public:
14 template<typename ...Args>
15 gUbo(Args&&... args, int bindingpoint, int usage = GL_DYNAMIC_DRAW) : id(GL_NONE), size(sizeof(T)), bindingpoint(bindingpoint) {
16 data = new T(std::forward<Args>(args)...);
17 id = renderer->genBuffers();
18 renderer->setBufferData(id, nullptr, size, usage);
19 // define the range of the buffer that links to a uniform binding point
20 renderer->setBufferRange(bindingpoint, id, 0, size);
21 update();
22 }
23
25 // See gVbo::clear: no renderer past engine shutdown, and nothing to give back.
26 if (id != GL_NONE && renderer != nullptr) {
27 renderer->deleteBuffer(id);
28 }
29 delete data;
30 }
31
32 void bind() const {
33 renderer->bindBuffer(GL_UNIFORM_BUFFER, id);
34 }
35
36 void unbind() const {
37 renderer->unbindBuffer(GL_UNIFORM_BUFFER);
38 }
39
40 void update() {
41 update(0, size);
42 }
43
44 void update(int offset, int length) {
45 void* ptr = reinterpret_cast<char*>(data) + offset;
46 renderer->bufSubData(id, offset, length, ptr);
47 }
48
49 T* getData() {
50 return data;
51 }
52
53 void setData(T* newObject) {
54 data = newObject;
55 update();
56 }
57
58 GLuint getId() const {
59 return id;
60 }
61
62 int getBindingPoint() const {
63 return bindingpoint;
64 }
65
66 int getSize() const {
67 return size;
68 }
69private:
70 GLuint id;
71 int size;
72 T* data;
73 int bindingpoint;
74};
void bind() const
Definition gUbo.h:32
GLuint getId() const
Definition gUbo.h:58
int getSize() const
Definition gUbo.h:66
void update(int offset, int length)
Definition gUbo.h:44
void setData(T *newObject)
Definition gUbo.h:53
~gUbo()
Definition gUbo.h:24
gUbo(Args &&... args, int bindingpoint, int usage=GL_DYNAMIC_DRAW)
Definition gUbo.h:15
int getBindingPoint() const
Definition gUbo.h:62
void unbind() const
Definition gUbo.h:36
void update()
Definition gUbo.h:40
T * getData()
Definition gUbo.h:49
gRenderer * renderer