light/modules/engine/src/platform/graphics/opengl/buffers.cpp

131 lines
2.9 KiB
C++
Raw Normal View History

2025-07-05 13:28:41 +03:30
#include <engine/platform/graphics/opengl/buffers.hpp>
#include <glad/gl.h>
2021-05-28 05:12:23 +04:30
namespace Light {
2022-03-07 21:57:00 +03:30
//==================== CONSTANT_BUFFER ====================//
glConstantBuffer::glConstantBuffer(ConstantBufferIndex index, unsigned int size)
: m_buffer_id(NULL)
, m_index(static_cast<int>(index))
2022-03-07 21:57:00 +03:30
{
glCreateBuffers(1, &m_buffer_id);
glNamedBufferData(m_buffer_id, size, nullptr, GL_DYNAMIC_DRAW);
2022-03-07 21:57:00 +03:30
bind();
2022-03-07 21:57:00 +03:30
}
glConstantBuffer::~glConstantBuffer()
{
glDeleteBuffers(1, &m_buffer_id);
2022-03-07 21:57:00 +03:30
}
void glConstantBuffer::bind()
2022-03-07 21:57:00 +03:30
{
glBindBufferBase(GL_UNIFORM_BUFFER, m_index, m_buffer_id);
2022-03-07 21:57:00 +03:30
}
void *glConstantBuffer::map()
2022-03-07 21:57:00 +03:30
{
void *map = glMapNamedBuffer(m_buffer_id, GL_WRITE_ONLY);
2022-03-07 21:57:00 +03:30
return map;
}
void glConstantBuffer::un_map()
2022-03-07 21:57:00 +03:30
{
glUnmapNamedBuffer(m_buffer_id);
2022-03-07 21:57:00 +03:30
}
//==================== CONSTANT_BUFFER ====================//
//==================== VERTEX_BUFFER ====================//
2025-07-05 13:28:41 +03:30
glVertexBuffer::glVertexBuffer(float *vertices, unsigned int stride, unsigned int count)
: m_buffer_id(NULL)
2022-03-07 21:57:00 +03:30
{
glCreateBuffers(1, &m_buffer_id);
glNamedBufferData(m_buffer_id, stride * count, vertices, GL_DYNAMIC_DRAW);
2022-03-07 21:57:00 +03:30
}
glVertexBuffer::~glVertexBuffer()
{
glDeleteBuffers(1, &m_buffer_id);
2022-03-07 21:57:00 +03:30
}
void glVertexBuffer::bind()
2022-03-07 21:57:00 +03:30
{
glBindBuffer(GL_ARRAY_BUFFER, m_buffer_id);
2022-03-07 21:57:00 +03:30
}
void glVertexBuffer::un_bind()
2022-03-07 21:57:00 +03:30
{
glBindBuffer(GL_ARRAY_BUFFER, NULL);
}
void *glVertexBuffer::map()
2022-03-07 21:57:00 +03:30
{
return glMapNamedBuffer(m_buffer_id, GL_WRITE_ONLY);
2022-03-07 21:57:00 +03:30
}
void glVertexBuffer::un_map()
2022-03-07 21:57:00 +03:30
{
glUnmapNamedBuffer(m_buffer_id);
2022-03-07 21:57:00 +03:30
}
//==================== VERTEX_BUFFER ====================//
//==================== INDEX_BUFFER ====================//
glIndexBuffer::glIndexBuffer(unsigned int *indices, unsigned int count): m_buffer_id(NULL)
2022-03-07 21:57:00 +03:30
{
// generate indices if not provided
bool hasIndices = !!indices;
if (!hasIndices)
{
2022-03-07 21:57:00 +03:30
// check
if (count % 6 != 0)
{
lt_log(warn, "'indices' can only be null if count is multiple of 6");
lt_log(warn, "Adding {} to 'count' -> {}", (6 - (count % 6)), count + (6 - (count % 6)));
2022-03-07 21:57:00 +03:30
count = count + (6 - (count % 6));
}
2022-03-07 21:57:00 +03:30
// create indices
2025-07-05 13:28:41 +03:30
indices = new unsigned int[count];
2022-03-07 21:57:00 +03:30
unsigned int offset = 0u;
for (unsigned int i = 0u; i < count; i += 6u)
{
indices[i + 0] = offset + 0u;
indices[i + 1] = offset + 1u;
indices[i + 2] = offset + 2u;
2021-05-28 05:12:23 +04:30
2022-03-07 21:57:00 +03:30
indices[i + 3] = offset + 2u;
indices[i + 4] = offset + 3u;
indices[i + 5] = offset + 0u;
2021-05-28 05:12:23 +04:30
2022-03-07 21:57:00 +03:30
offset += 4u;
}
2022-03-07 21:57:00 +03:30
}
2022-03-07 21:57:00 +03:30
// create buffer
glCreateBuffers(1, &m_buffer_id);
glNamedBufferData(m_buffer_id, count * sizeof(unsigned int), indices, GL_STATIC_DRAW);
2022-03-07 21:57:00 +03:30
// delete indices
if (!hasIndices)
delete[] indices;
}
2021-05-28 13:13:45 +04:30
2022-03-07 21:57:00 +03:30
glIndexBuffer::~glIndexBuffer()
{
glDeleteBuffers(1, &m_buffer_id);
2022-03-07 21:57:00 +03:30
}
2021-05-28 13:13:45 +04:30
void glIndexBuffer::bind()
2022-03-07 21:57:00 +03:30
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_buffer_id);
2022-03-07 21:57:00 +03:30
}
2021-05-28 13:13:45 +04:30
void glIndexBuffer::un_bind()
2022-03-07 21:57:00 +03:30
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
}
//==================== INDEX_BUFFER ====================//
2021-05-28 13:13:45 +04:30
2022-03-07 21:57:00 +03:30
} // namespace Light