diff --git a/CMakeLists.txt b/CMakeLists.txt index 3df0744..68ab259 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,9 +30,12 @@ add_library(WVCore STATIC src/assets/AssetManager.cpp + src/rendering/ElementBuffer.cpp src/rendering/Renderer.cpp src/rendering/Shader.cpp src/rendering/Texture.cpp + src/rendering/VertexArrayObject.cpp + src/rendering/VertexBuffer.cpp src/rendering/Window.cpp ${glad_SOURCE_DIR}/src/glad.c diff --git a/include/wv/rendering/ElementBuffer.h b/include/wv/rendering/ElementBuffer.h new file mode 100644 index 0000000..60caf9f --- /dev/null +++ b/include/wv/rendering/ElementBuffer.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include + +namespace WillowVox +{ + enum class ElementBufferAttribType + { + UINT32, + UINT16, + UINT8 + }; + + class ElementBuffer + { + public: + ElementBuffer(); + ElementBuffer(ElementBuffer&& other) noexcept; + ~ElementBuffer(); + + void Bind(); + void BufferData(ElementBufferAttribType type, uint32_t numElements, void* data); + void Draw(); + private: + unsigned int m_ebo; + uint32_t m_elements; + ElementBufferAttribType m_type; + }; +} \ No newline at end of file diff --git a/include/wv/rendering/VertexArrayObject.h b/include/wv/rendering/VertexArrayObject.h new file mode 100644 index 0000000..81f3419 --- /dev/null +++ b/include/wv/rendering/VertexArrayObject.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +namespace WillowVox +{ + class VertexArrayObject + { + public: + VertexArrayObject(); + ~VertexArrayObject(); + + void Bind(); + void Draw(); + + void BufferVertexData(std::size_t size, void* data); + void BufferElementData(ElementBufferAttribType type, uint32_t numElements, void* data); + + void SetAttribPointer(uint32_t index, std::size_t attribSize, VertexBufferAttribType attribType, bool normalized, std::size_t vertexSize, std::size_t offset); + + private: + std::unique_ptr m_vertexBuffer; + std::unique_ptr m_elementBuffer; + + unsigned int m_vao; + }; +} \ No newline at end of file diff --git a/include/wv/rendering/VertexBuffer.h b/include/wv/rendering/VertexBuffer.h new file mode 100644 index 0000000..3baaa1b --- /dev/null +++ b/include/wv/rendering/VertexBuffer.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +namespace WillowVox +{ + enum class VertexBufferAttribType + { + FLOAT64, + FLOAT32, + FLOAT16, + INT32, + UINT32, + INT16, + UINT16, + INT8, + UINT8 + }; + + class VertexBuffer + { + public: + VertexBuffer(); + VertexBuffer(VertexBuffer&& other) noexcept; + ~VertexBuffer(); + + void Bind(); + void BufferData(std::size_t size, void* data); + void SetAttribPointer(uint32_t index, std::size_t attribSize, VertexBufferAttribType attribType, bool normalized, std::size_t vertexSize, std::size_t offset); + private: + unsigned int m_vbo; + }; +} \ No newline at end of file diff --git a/src/rendering/ElementBuffer.cpp b/src/rendering/ElementBuffer.cpp new file mode 100644 index 0000000..3a51efa --- /dev/null +++ b/src/rendering/ElementBuffer.cpp @@ -0,0 +1,63 @@ +#include + +namespace WillowVox +{ + ElementBuffer::ElementBuffer() + { + glGenBuffers(1, &m_ebo); + } + + ElementBuffer::ElementBuffer(ElementBuffer&& other) noexcept + : m_ebo(other.m_ebo) + { + other.m_ebo = 0; + } + + ElementBuffer::~ElementBuffer() + { + glDeleteBuffers(1, &m_ebo); + } + + void ElementBuffer::Bind() + { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo); + } + + void ElementBuffer::BufferData(ElementBufferAttribType type, uint32_t numElements, void* data) + { + m_type = type; + m_elements = numElements; + Bind(); + + switch (m_type) + { + case ElementBufferAttribType::UINT32: + glBufferData(GL_ELEMENT_ARRAY_BUFFER, numElements * 4, data, GL_STATIC_DRAW); + break; + case ElementBufferAttribType::UINT16: + glBufferData(GL_ELEMENT_ARRAY_BUFFER, numElements * 2, data, GL_STATIC_DRAW); + break; + case ElementBufferAttribType::UINT8: + glBufferData(GL_ELEMENT_ARRAY_BUFFER, numElements, data, GL_STATIC_DRAW); + break; + + } + } + + void ElementBuffer::Draw() + { + switch (m_type) + { + case ElementBufferAttribType::UINT32: + glDrawElements(GL_TRIANGLES, m_elements, GL_UNSIGNED_INT, 0); + break; + case ElementBufferAttribType::UINT16: + glDrawElements(GL_TRIANGLES, m_elements, GL_UNSIGNED_SHORT, 0); + break; + case ElementBufferAttribType::UINT8: + glDrawElements(GL_TRIANGLES, m_elements, GL_UNSIGNED_BYTE, 0); + break; + + } + } +} \ No newline at end of file diff --git a/src/rendering/VertexArrayObject.cpp b/src/rendering/VertexArrayObject.cpp new file mode 100644 index 0000000..de80dc3 --- /dev/null +++ b/src/rendering/VertexArrayObject.cpp @@ -0,0 +1,50 @@ +#include + +namespace WillowVox +{ + VertexArrayObject::VertexArrayObject() + { + glGenVertexArrays(1, &m_vao); + glBindVertexArray(m_vao); + + m_vertexBuffer = std::make_unique(); + m_vertexBuffer->Bind(); + m_elementBuffer = std::make_unique(); + m_elementBuffer->Bind(); + } + + VertexArrayObject::~VertexArrayObject() + { + glDeleteVertexArrays(1, &m_vao); + } + + void VertexArrayObject::Bind() + { + glBindVertexArray(m_vao); + } + + void VertexArrayObject::Draw() + { + Bind(); + m_elementBuffer->Draw(); + } + + void VertexArrayObject::BufferVertexData(std::size_t size, void* data) + { + Bind(); + m_vertexBuffer->BufferData(size, data); + } + + void VertexArrayObject::BufferElementData(ElementBufferAttribType type, uint32_t numElements, void* data) + { + Bind(); + m_elementBuffer->BufferData(type, numElements, data); + } + + void VertexArrayObject::SetAttribPointer(uint32_t index, std::size_t attribSize, VertexBufferAttribType attribType, bool normalized, std::size_t vertexSize, std::size_t offset) + { + Bind(); + m_vertexBuffer->SetAttribPointer(index, attribSize, attribType, normalized, vertexSize, offset); + } + +} \ No newline at end of file diff --git a/src/rendering/VertexBuffer.cpp b/src/rendering/VertexBuffer.cpp new file mode 100644 index 0000000..03815b5 --- /dev/null +++ b/src/rendering/VertexBuffer.cpp @@ -0,0 +1,67 @@ +#include + +namespace WillowVox +{ + VertexBuffer::VertexBuffer() + { + glGenBuffers(1, &m_vbo); + } + + VertexBuffer::VertexBuffer(VertexBuffer&& other) noexcept + : m_vbo(other.m_vbo) + { + other.m_vbo = 0; + } + + VertexBuffer::~VertexBuffer() + { + glDeleteBuffers(1, &m_vbo); + } + + void VertexBuffer::Bind() + { + glBindBuffer(GL_ARRAY_BUFFER, m_vbo); + } + + void VertexBuffer::BufferData(std::size_t size, void* data) + { + Bind(); + glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); + } + + void VertexBuffer::SetAttribPointer(uint32_t index, std::size_t attribSize, VertexBufferAttribType attribType, bool normalized, std::size_t vertexSize, std::size_t offset) + { + Bind(); + switch (attribType) + { + case VertexBufferAttribType::FLOAT64: + glVertexAttribPointer(index, attribSize, GL_DOUBLE, normalized, vertexSize, (void*)offset); + break; + case VertexBufferAttribType::FLOAT32: + glVertexAttribPointer(index, attribSize, GL_FLOAT, normalized, vertexSize, (void*)offset); + break; + case VertexBufferAttribType::FLOAT16: + glVertexAttribPointer(index, attribSize, GL_HALF_FLOAT, normalized, vertexSize, (void*)offset); + break; + case VertexBufferAttribType::INT32: + glVertexAttribPointer(index, attribSize, GL_INT, normalized, vertexSize, (void*)offset); + break; + case VertexBufferAttribType::UINT32: + glVertexAttribPointer(index, attribSize, GL_UNSIGNED_INT, normalized, vertexSize, (void*)offset); + break; + case VertexBufferAttribType::INT16: + glVertexAttribPointer(index, attribSize, GL_SHORT, normalized, vertexSize, (void*)offset); + break; + case VertexBufferAttribType::UINT16: + glVertexAttribPointer(index, attribSize, GL_UNSIGNED_SHORT, normalized, vertexSize, (void*)offset); + break; + case VertexBufferAttribType::INT8: + glVertexAttribPointer(index, attribSize, GL_BYTE, normalized, vertexSize, (void*)offset); + break; + case VertexBufferAttribType::UINT8: + glVertexAttribPointer(index, attribSize, GL_UNSIGNED_BYTE, normalized, vertexSize, (void*)offset); + break; + } + glEnableVertexAttribArray(index); + } +} \ No newline at end of file