massive rewrite

This commit is contained in:
Lauchmelder 2021-12-24 14:20:53 +01:00
parent 4c1c151422
commit 5811d3676a
18 changed files with 442 additions and 151 deletions

37
src/Buffer.cpp Normal file
View file

@ -0,0 +1,37 @@
#include "..\include\lol\Buffer.hpp"
#pragma once
namespace lol
{
Buffer::Buffer(BufferType type) :
id(0), type(type)
{
glGenBuffers(1, &id);
glBindBuffer(NATIVE(type), id);
}
Buffer::~Buffer()
{
glDeleteBuffers(1, &id);
}
void* Buffer::Map(Access access)
{
return glMapBuffer(NATIVE(type), NATIVE(access));
}
void Buffer::Unmap()
{
glUnmapBuffer(NATIVE(type));
}
void Buffer::Bind()
{
glBindBuffer(NATIVE(type), id);
}
void Buffer::Unbind()
{
glBindBuffer(NATIVE(type), 0);
}
}

View file

@ -5,12 +5,14 @@ namespace lol
void Drawable::Draw(const CameraBase& camera) const
{
shader->Use();
shader->Bind();
vao->Bind();
PreRender(camera);
vao->Render(static_cast<unsigned int>(type));
glDrawElements(NATIVE(type), vao->GetIndexCount(), GL_UNSIGNED_INT, nullptr);
}
void Drawable::SetPrimitiveType(PrimitiveType type)
void Drawable::SetDrawMode(DrawMode type)
{
this->type = type;
}

View file

@ -11,7 +11,7 @@ inline
namespace lol
{
UniqueShader::UniqueShader(const std::string& vertexShader, const std::string& fragmentShader) :
Shader::Shader(const std::string& vertexShader, const std::string& fragmentShader) :
id(0)
{
GLint success;
@ -71,17 +71,22 @@ namespace lol
glDeleteShader(vertexShaderID);
}
UniqueShader::~UniqueShader()
Shader::~Shader()
{
glDeleteProgram(id);
}
void UniqueShader::Use()
void Shader::Bind()
{
glUseProgram(id);
}
void UniqueShader::SetUniform(const std::string& name, const glm::mat4& value)
void Shader::Unbind()
{
glUseProgram(0);
}
void Shader::SetUniform(const std::string& name, const glm::mat4& value)
{
GLint location = glGetUniformLocation(id, name.c_str());
if (location == -1)
@ -90,7 +95,7 @@ namespace lol
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value));
}
void UniqueShader::SetUniform(const std::string& name, const glm::vec4& value)
void Shader::SetUniform(const std::string& name, const glm::vec4& value)
{
GLint location = glGetUniformLocation(id, name.c_str());
if (location == -1)

View file

@ -7,6 +7,14 @@ namespace lol
UniqueTexture::UniqueTexture() :
textureID(0)
{
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_UNSIGNED_BYTE, data);
}
UniqueTexture::~UniqueTexture()
{
glDeleteTextures(1, &textureID);
}
}

View file

@ -6,59 +6,58 @@
namespace lol
{
UniqueVertexArrayObject::~UniqueVertexArrayObject()
VertexArray::VertexArray()
{
glDeleteBuffers(1, &ebo);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
glGenVertexArrays(1, &id);
}
void UniqueVertexArrayObject::Render(GLenum mode)
VertexArray::VertexArray(const std::shared_ptr<VertexBuffer>& vertexBuffer, const std::shared_ptr<ElementBuffer>& elementBuffer) :
id(0)
{
assert(vao != 0);
glGenVertexArrays(1, &id);
glBindVertexArray(vao);
// GLenum result = glGetError();
glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0);
SetVertexBuffer(vertexBuffer);
SetElementBuffer(elementBuffer);
}
VertexArray::~VertexArray()
{
glDeleteVertexArrays(1, &id);
}
UniqueVertexArrayObject::UniqueVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) :
vao(0), vbo(0), ebo(0), indexCount(indices.size())
void VertexArray::SetVertexBuffer(const std::shared_ptr<VertexBuffer>& buffer)
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
// Determing native OpenGL GLenum depending on specified usage
GLenum bufferUsage;
switch (usage)
{
case Usage::Static: bufferUsage = GL_STATIC_DRAW; break;
case Usage::Dynamic: bufferUsage = GL_DYNAMIC_DRAW; break;
case Usage::Stream: bufferUsage = GL_STREAM_DRAW; break;
default: // Forgot to add a usage case to this switch
assert("Unknown buffer usage" == "");
break;
}
// Create VBO
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), (const void*)(vertices.data()), bufferUsage);
// Create EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), (const void*)(indices.data()), bufferUsage);
glBindVertexArray(id);
buffer->Bind();
// Set up pipeline layout
unsigned int index = 0;
const BufferLayout& layout = buffer->GetLayout();
for (const VertexAttribute& attribute : layout)
{
glVertexAttribPointer(index, attribute.size, attribute.type, attribute.normalized, attribute.stride, attribute.pointer);
glEnableVertexAttribArray(index);
glVertexAttribPointer(index, attribute.size, NATIVE(attribute.type), attribute.normalized, layout.GetStride(), (void*)(attribute.offset));
index++;
}
vertexBuffer = buffer;
}
void VertexArray::SetElementBuffer(const std::shared_ptr<ElementBuffer>& buffer)
{
glBindVertexArray(id);
buffer->Bind();
elementBuffer = buffer;
}
void VertexArray::Bind()
{
glBindVertexArray(id);
}
void Unbind()
{
glBindVertexArray(0);
}
}

View file

@ -0,0 +1,10 @@
#include <lol/buffers/ElementBuffer.hpp>
namespace lol
{
ElementBuffer::ElementBuffer(size_t count, unsigned int* elements, Usage usage) :
Buffer(BufferType::ElementArray), count(count)
{
glBufferData(NATIVE(type), count * sizeof(unsigned int), elements, NATIVE(usage));
}
}

View file

@ -0,0 +1,23 @@
#include <lol/buffers/VertexBuffer.hpp>
#pragma once
namespace lol
{
BufferLayout::BufferLayout(const std::initializer_list<VertexAttribute>& attributes) :
layout(attributes), stride(0)
{
// Calculate stride and offsets of elements
for (VertexAttribute& attribute : layout)
{
attribute.offset += stride;
stride += (attribute.size * SizeOf(attribute.type));
}
}
VertexBuffer::VertexBuffer(size_t size, float* data, Usage usage) :
Buffer(BufferType::Array), layout{}
{
glBufferData(NATIVE(type), size * sizeof(float), (void*)data, NATIVE(usage));
}
}