83 lines
2.3 KiB
C++
83 lines
2.3 KiB
C++
#include "MetaCoreRender/MetaCoreMesh.h"
|
|
|
|
#include <glad/glad.h>
|
|
|
|
namespace MetaCore {
|
|
|
|
MetaCoreMesh::~MetaCoreMesh() {
|
|
Shutdown();
|
|
}
|
|
|
|
bool MetaCoreMesh::Build(const std::vector<MetaCoreVertex>& vertices, const std::vector<std::uint32_t>& indices, MetaCorePrimitiveType primitiveType) {
|
|
Shutdown();
|
|
|
|
PrimitiveType_ = primitiveType;
|
|
IndexCount_ = static_cast<std::uint32_t>(indices.size());
|
|
|
|
glGenVertexArrays(1, &VertexArrayHandle_);
|
|
glGenBuffers(1, &VertexBufferHandle_);
|
|
glGenBuffers(1, &IndexBufferHandle_);
|
|
|
|
glBindVertexArray(VertexArrayHandle_);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferHandle_);
|
|
glBufferData(
|
|
GL_ARRAY_BUFFER,
|
|
static_cast<GLsizeiptr>(vertices.size() * sizeof(MetaCoreVertex)),
|
|
vertices.data(),
|
|
GL_STATIC_DRAW
|
|
);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferHandle_);
|
|
glBufferData(
|
|
GL_ELEMENT_ARRAY_BUFFER,
|
|
static_cast<GLsizeiptr>(indices.size() * sizeof(std::uint32_t)),
|
|
indices.data(),
|
|
GL_STATIC_DRAW
|
|
);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(MetaCoreVertex), reinterpret_cast<const void*>(offsetof(MetaCoreVertex, Position)));
|
|
|
|
glEnableVertexAttribArray(1);
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(MetaCoreVertex), reinterpret_cast<const void*>(offsetof(MetaCoreVertex, Normal)));
|
|
|
|
glEnableVertexAttribArray(2);
|
|
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(MetaCoreVertex), reinterpret_cast<const void*>(offsetof(MetaCoreVertex, Color)));
|
|
|
|
glBindVertexArray(0);
|
|
return true;
|
|
}
|
|
|
|
void MetaCoreMesh::Shutdown() {
|
|
if (IndexBufferHandle_ != 0) {
|
|
glDeleteBuffers(1, &IndexBufferHandle_);
|
|
IndexBufferHandle_ = 0;
|
|
}
|
|
|
|
if (VertexBufferHandle_ != 0) {
|
|
glDeleteBuffers(1, &VertexBufferHandle_);
|
|
VertexBufferHandle_ = 0;
|
|
}
|
|
|
|
if (VertexArrayHandle_ != 0) {
|
|
glDeleteVertexArrays(1, &VertexArrayHandle_);
|
|
VertexArrayHandle_ = 0;
|
|
}
|
|
|
|
IndexCount_ = 0;
|
|
}
|
|
|
|
void MetaCoreMesh::Draw() const {
|
|
glBindVertexArray(VertexArrayHandle_);
|
|
glDrawElements(
|
|
PrimitiveType_ == MetaCorePrimitiveType::Triangles ? GL_TRIANGLES : GL_LINES,
|
|
static_cast<GLsizei>(IndexCount_),
|
|
GL_UNSIGNED_INT,
|
|
nullptr
|
|
);
|
|
glBindVertexArray(0);
|
|
}
|
|
|
|
} // namespace MetaCore
|