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 {
|
|
|
|
|
2021-06-14 17:02:56 +04:30
|
|
|
glVertexBuffer::glVertexBuffer(float* vertices, unsigned int count)
|
2021-05-28 05:12:23 +04:30
|
|
|
{
|
|
|
|
glCreateBuffers(1, &m_BufferID);
|
2021-06-13 19:35:48 +04:30
|
|
|
glNamedBufferData(m_BufferID, count * sizeof(float), vertices, GL_DYNAMIC_DRAW);
|
2021-05-28 05:12:23 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
glVertexBuffer::~glVertexBuffer()
|
|
|
|
{
|
|
|
|
glDeleteBuffers(1, &m_BufferID);
|
|
|
|
}
|
|
|
|
|
2021-06-13 19:35:48 +04:30
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-06-14 17:02:56 +04:30
|
|
|
glIndexBuffer::glIndexBuffer(unsigned int* indices, unsigned int count)
|
2021-05-28 13:13:45 +04:30
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
// generate indices if not provided
|
2021-06-14 17:02:56 +04:30
|
|
|
bool hasIndices = !!indices;
|
|
|
|
if (!hasIndices)
|
|
|
|
{
|
2021-06-19 15:12:42 +04:30
|
|
|
// check
|
2021-06-14 17:02:56 +04:30
|
|
|
if (count % 6 != 0)
|
|
|
|
{
|
|
|
|
LT_ENGINE_WARN("glIndexBuffer::glIndexBuffer: count should be divisible by 6 when no indices is provided");
|
|
|
|
LT_ENGINE_WARN("glIndexBuffer::glIndexBuffer: adding {} to count -> {}", (6 - (count % 6)), count + (6 - (count % 6)));
|
|
|
|
count = count + (6 - (count % 6));
|
|
|
|
}
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// create indices
|
2021-06-14 17:02:56 +04:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// create buffer
|
2021-05-28 13:13:45 +04:30
|
|
|
glCreateBuffers(1, &m_BufferID);
|
2021-06-13 19:35:48 +04:30
|
|
|
glNamedBufferData(m_BufferID, count * sizeof(unsigned int), indices, GL_STATIC_DRAW);
|
2021-06-14 17:02:56 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// delete indices
|
2021-06-14 17:02:56 +04:30
|
|
|
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
|
|
|
}
|