mirror of
https://github.com/ApfelTeeSaft/WV-Core.git
synced 2026-08-26 19:43:26 +00:00
VBO, EBO, and VAO abstractions
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <wv/wvpch.h>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <wv/rendering/VertexBuffer.h>
|
||||
#include <wv/rendering/ElementBuffer.h>
|
||||
|
||||
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<VertexBuffer> m_vertexBuffer;
|
||||
std::unique_ptr<ElementBuffer> m_elementBuffer;
|
||||
|
||||
unsigned int m_vao;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <wv/wvpch.h>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include <wv/rendering/ElementBuffer.h>
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <wv/rendering/VertexArrayObject.h>
|
||||
|
||||
namespace WillowVox
|
||||
{
|
||||
VertexArrayObject::VertexArrayObject()
|
||||
{
|
||||
glGenVertexArrays(1, &m_vao);
|
||||
glBindVertexArray(m_vao);
|
||||
|
||||
m_vertexBuffer = std::make_unique<VertexBuffer>();
|
||||
m_vertexBuffer->Bind();
|
||||
m_elementBuffer = std::make_unique<ElementBuffer>();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#include <wv/rendering/VertexBuffer.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user