mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-03-23 15:50:20 +00:00
56 lines
1.5 KiB
C++
56 lines
1.5 KiB
C++
#include "rendering/mesh.hpp"
|
|
|
|
namespace wowee {
|
|
namespace rendering {
|
|
|
|
Mesh::~Mesh() {
|
|
destroy();
|
|
}
|
|
|
|
void Mesh::create(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices) {
|
|
indexCount = indices.size();
|
|
|
|
glGenVertexArrays(1, &VAO);
|
|
glGenBuffers(1, &VBO);
|
|
glGenBuffers(1, &EBO);
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint32_t), indices.data(), GL_STATIC_DRAW);
|
|
|
|
// Position
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
|
|
|
|
// Normal
|
|
glEnableVertexAttribArray(1);
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
|
|
|
|
// TexCoord
|
|
glEnableVertexAttribArray(2);
|
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texCoord));
|
|
|
|
glBindVertexArray(0);
|
|
}
|
|
|
|
void Mesh::destroy() {
|
|
if (VAO) glDeleteVertexArrays(1, &VAO);
|
|
if (VBO) glDeleteBuffers(1, &VBO);
|
|
if (EBO) glDeleteBuffers(1, &EBO);
|
|
VAO = VBO = EBO = 0;
|
|
}
|
|
|
|
void Mesh::draw() const {
|
|
if (VAO && indexCount > 0) {
|
|
glBindVertexArray(VAO);
|
|
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
|
|
glBindVertexArray(0);
|
|
}
|
|
}
|
|
|
|
} // namespace rendering
|
|
} // namespace wowee
|