light/Engine/src/Platform/GraphicsAPI/OpenGL/glBuffers.cpp

96 lines
2 KiB
C++
Raw Normal View History

2021-05-28 05:12:23 +04:30
#include "ltpch.h"
2021-05-28 13:13:45 +04:30
#include "glBuffers.h"
2021-05-28 05:12:23 +04:30
#include <glad/glad.h>
namespace Light {
//** VERTEX_BUFFER **//
glVertexBuffer::glVertexBuffer(float* vertices, unsigned int count)
2021-05-28 05:12:23 +04:30
{
glCreateBuffers(1, &m_BufferID);
glNamedBufferData(m_BufferID, count * sizeof(float), vertices, GL_DYNAMIC_DRAW);
2021-05-28 05:12:23 +04:30
}
glVertexBuffer::~glVertexBuffer()
{
glDeleteBuffers(1, &m_BufferID);
}
void* glVertexBuffer::Map()
{
return glMapNamedBuffer(m_BufferID, GL_WRITE_ONLY);
}
void glVertexBuffer::UnMap()
{
glUnmapNamedBuffer(m_BufferID);
}
2021-05-28 05:12:23 +04:30
void glVertexBuffer::Bind()
{
glBindBuffer(GL_ARRAY_BUFFER, m_BufferID);
}
void glVertexBuffer::UnBind()
{
glBindBuffer(GL_ARRAY_BUFFER, NULL);
}
//** INDEX_BUFFER **//
glIndexBuffer::glIndexBuffer(unsigned int* indices, unsigned int count)
2021-05-28 13:13:45 +04:30
{
// generate indices if not provided
bool hasIndices = !!indices;
if (!hasIndices)
{
// check
if (count % 6 != 0)
{
LT_ENGINE_WARN("glIndexBuffer::dxIndexBuffer: 'indices' can only be null if count is multiple of 6");
LT_ENGINE_WARN("glIndexBuffer::glIndexBuffer: adding {} to 'count' -> {}", (6 - (count % 6)), count + (6 - (count % 6)));
count = count + (6 - (count % 6));
}
// create indices
indices = new unsigned int[count];
unsigned int offset = 0;
for (unsigned int i = 0; i < count; i += 6)
{
indices[i + 0] = offset + 0;
indices[i + 1] = offset + 1;
indices[i + 2] = offset + 2;
indices[i + 3] = offset + 2;
indices[i + 4] = offset + 3;
indices[i + 5] = offset + 0;
offset += 4;
}
}
// create buffer
2021-05-28 13:13:45 +04:30
glCreateBuffers(1, &m_BufferID);
glNamedBufferData(m_BufferID, count * sizeof(unsigned int), indices, GL_STATIC_DRAW);
// delete indices
if (!hasIndices)
delete[] indices;
2021-05-28 13:13:45 +04:30
}
glIndexBuffer::~glIndexBuffer()
{
glDeleteBuffers(1, &m_BufferID);
}
void glIndexBuffer::Bind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_BufferID);
}
void glIndexBuffer::UnBind()
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
}
2021-05-28 05:12:23 +04:30
}