66 lines
1.3 KiB
C++
66 lines
1.3 KiB
C++
#include <deerith/graphics/mesh.h>
|
|
#include <glad/gl.h>
|
|
|
|
deerith::Mesh2D::Mesh2D(
|
|
const glm::vec2 *vertices,
|
|
std::size_t vertex_count,
|
|
const unsigned int *indices,
|
|
std::size_t index_count)
|
|
: index_count(index_count)
|
|
{
|
|
glGenVertexArrays(1, &vao);
|
|
glGenBuffers(1, &vbo);
|
|
glGenBuffers(1, &ebo);
|
|
|
|
glBindVertexArray(vao);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
glBufferData(
|
|
GL_ARRAY_BUFFER,
|
|
vertex_count * sizeof(glm::vec2),
|
|
vertices,
|
|
GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
|
glBufferData(
|
|
GL_ELEMENT_ARRAY_BUFFER,
|
|
index_count * sizeof(unsigned int),
|
|
indices,
|
|
GL_STATIC_DRAW);
|
|
|
|
// Position: vec2
|
|
glVertexAttribPointer(
|
|
0,
|
|
2,
|
|
GL_FLOAT,
|
|
GL_FALSE,
|
|
2 * sizeof(float),
|
|
nullptr);
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glBindVertexArray(0);
|
|
}
|
|
|
|
deerith::Mesh2D::~Mesh2D()
|
|
{
|
|
glDeleteVertexArrays(1, &vao);
|
|
glDeleteBuffers(1, &vbo);
|
|
glDeleteBuffers(1, &ebo);
|
|
}
|
|
|
|
void deerith::Mesh2D::bind() const
|
|
{
|
|
glBindVertexArray(vao);
|
|
}
|
|
|
|
void deerith::Mesh2D::draw() const
|
|
{
|
|
glBindVertexArray(vao);
|
|
|
|
glDrawElements(
|
|
GL_TRIANGLES,
|
|
static_cast<GLsizei>(index_count),
|
|
GL_UNSIGNED_INT,
|
|
nullptr);
|
|
} |