sculpto
gl_buffer.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file gl_buffer.cpp
3 * \brief Opengl buffer class implementation module.
4 *
5 * \author Sabitov Kirill
6 * \date 27 June 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10#include "gl_buffer.h"
11
13{
14 this->Size = Size;
15
16 glGenBuffers(1, &Id);
17 glBindBuffer(GL_UNIFORM_BUFFER, Id);
18 glBufferData(GL_UNIFORM_BUFFER, Size, nullptr, GL_STATIC_DRAW);
19 glBindBuffer(GL_UNIFORM_BUFFER, 0);
20
21 SCL_CORE_SUCCES("OpenGL Constant buffer with id {} and size {} created.", Id, Size);
22}
23
25{
26 this->Size = Size;
27
28 glGenBuffers(1, &Id);
29 glBindBuffer(GL_UNIFORM_BUFFER, Id);
30 glBufferData(GL_UNIFORM_BUFFER, Size, Data, GL_STATIC_DRAW);
31 glBindBuffer(GL_UNIFORM_BUFFER, 0);
32
33 SCL_CORE_SUCCES("OpenGL Constant buffer with id {} and size {} created.", Id, Size);
34}
35
37{
38 Free();
39}
40
41void scl::gl_constant_buffer::Bind(u32 BindingPoint) const
42{
43 this->BindingPoint = BindingPoint;
44 if (Id != 0) glBindBufferBase(GL_UNIFORM_BUFFER, BindingPoint, Id);
45}
46
48{
49 glBindBufferBase(GL_UNIFORM_BUFFER, BindingPoint, 0);
50 this->BindingPoint = 0;
51}
52
54{
55 if (Id != 0)
56 {
57 SCL_CORE_ASSERT(this->Size == Size, "Constant buffer size can't be changed.");
58
59 glBindBuffer(GL_UNIFORM_BUFFER, Id);
60 glBufferSubData(GL_UNIFORM_BUFFER, 0, Size, Data);
61 glBindBuffer(GL_UNIFORM_BUFFER, 0);
62 }
63}
64
66{
67 if (Id != 0)
68 {
69 glDeleteBuffers(1, &Id);
70
71 SCL_CORE_INFO("OpenGL Constant buffer with id {} freed.", Id);
72 Id = 0, BindingPoint = 0, Size = 0;
73 }
74}
75
77 vertex_buffer(VertexLayout)
78{
79 this->VerticesCount = Count;
80
81 glGenBuffers(1, &Id);
82 glBindBuffer(GL_ARRAY_BUFFER, Id);
83 glBufferData(GL_ARRAY_BUFFER, Count, nullptr, GL_DYNAMIC_DRAW);
84 glBindBuffer(GL_ARRAY_BUFFER, 0);
85
86 SCL_CORE_SUCCES("OpenGL Vertex buffer with id {} and {} verices created.", Id, Count);
87}
88
89scl::gl_vertex_buffer::gl_vertex_buffer(const void *Vertices, u32 Count, const vertex_layout &VertexLayout) :
90 vertex_buffer(VertexLayout)
91{
92 this->VerticesCount = Count;
93
94 glCreateBuffers(1, &Id);
95 glBindBuffer(GL_ARRAY_BUFFER, Id);
96 glBufferData(GL_ARRAY_BUFFER, (u64)Count * VertexLayout.GetSize(), Vertices, GL_STATIC_DRAW);
97 glBindBuffer(GL_ARRAY_BUFFER, 0);
98
99 SCL_CORE_SUCCES("OpenGL Vertex buffer with id {} and {} verices created.", Id, Count);
100}
101
103{
104 Free();
105}
106
108{
109 if (Id != 0) glBindBuffer(GL_ARRAY_BUFFER, Id);
110}
111
113{
114 glBindBuffer(GL_ARRAY_BUFFER, 0);
115}
116
117void scl::gl_vertex_buffer::Update(const void *Vertices, u32 Count)
118{
119 if (Id != 0)
120 {
121 SCL_CORE_ASSERT(Count == this->VerticesCount, "Vertices count can't be changed.");
122
123 glBindBuffer(GL_ARRAY_BUFFER, Id);
124 glBufferSubData(GL_ARRAY_BUFFER, 0, (u64)Count * VertexLayout.GetSize(), Vertices);
125 glBindBuffer(GL_ARRAY_BUFFER, 0);
126 }
127}
128
130{
131 if (Id != 0)
132 {
133 glDeleteBuffers(1, &Id);
134
135 SCL_CORE_INFO("OpenGL Vertex buffer with id {} freed.", Id);
136 Id = 0, VerticesCount = 0;
137 }
138}
139
141{
142 this->IndicesCount = Count;
143
144 glCreateBuffers(1, &Id);
145 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Id);
146 glBufferData(GL_ELEMENT_ARRAY_BUFFER, Count * sizeof(u32), Indices, GL_STATIC_DRAW);
147
148 SCL_CORE_SUCCES("OpenGL Index buffer with id {} and {} indices created.", Id, Count);
149}
150
152{
153 Free();
154}
155
157{
158 if (Id != 0) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Id);
159}
160
162{
163 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
164}
165
167{
168 if (Id != 0)
169 {
170 SCL_CORE_ASSERT(this->IndicesCount == Count, "Indices count cant'be changed.");
171
172 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Id);
173 glBufferData(GL_ELEMENT_ARRAY_BUFFER, Count * sizeof(u32), Indices, GL_STATIC_DRAW);
174 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
175 }
176}
177
179{
180 if (Id != 0)
181 {
182 glDeleteBuffers(1, &Id);
183
184 SCL_CORE_INFO("OpenGL Index buffer with id {} freed.", Id);
185 Id = 0, IndicesCount = 0;
186 }
187}
#define SCL_CORE_ASSERT(expr,...)
Definition: assert.h:69
void Update(void *Data, u32 Size) override
Definition: gl_buffer.cpp:53
gl_constant_buffer(u32 Size)
Definition: gl_buffer.cpp:12
void Free() override
Definition: gl_buffer.cpp:65
void Unbind() const override
Definition: gl_buffer.cpp:47
~gl_constant_buffer() override
Definition: gl_buffer.cpp:36
void Bind(u32 BindingPoint) const override
Definition: gl_buffer.cpp:41
void Free() override
Definition: gl_buffer.cpp:178
~gl_index_buffer() override
Definition: gl_buffer.cpp:151
void Unbind() const override
Definition: gl_buffer.cpp:161
void Update(u32 *Indices, u32 Count) override
Definition: gl_buffer.cpp:166
void Bind() const override
Definition: gl_buffer.cpp:156
gl_index_buffer(u32 *Indices, u32 Count)
Definition: gl_buffer.cpp:140
void Update(const void *Vertices, u32 Count) override
Definition: gl_buffer.cpp:117
void Bind() const override
Definition: gl_buffer.cpp:107
gl_vertex_buffer(u32 Count, const vertex_layout &VertexLayout)
Definition: gl_buffer.cpp:76
void Free() override
Definition: gl_buffer.cpp:129
void Unbind() const override
Definition: gl_buffer.cpp:112
vertex_layout VertexLayout
Definition: buffer.h:103
u32 GetSize() const
Definition: vertex.h:46
#define SCL_CORE_SUCCES(...)
Definition: log.h:42
#define SCL_CORE_INFO(...)
Definition: log.h:41
uint32_t u32
Definition: math_common.h:21
uint64_t u64
Definition: math_common.h:23
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.