Skip to content

Commit

Permalink
Mesh class implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fonzy1243 committed Apr 22, 2024
1 parent b2db585 commit 0c69013
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/graphics/mesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <gl_errors.h>
#include <mesh.h>

Mesh::Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices) {
this->vertices = vertices;
this->indices = indices;

this->setup_mesh();
}

void Mesh::setup_mesh() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glCheckError();

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glCheckError();

glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0],
GL_STATIC_DRAW);
glCheckError();

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int),
&indices[0], GL_STATIC_DRAW);
glCheckError();

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
glCheckError();

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(void *)offsetof(Vertex, normal));
glCheckError();

glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(void *)offsetof(Vertex, color));
glCheckError();

glBindVertexArray(0);
}

void Mesh::draw(Shader &shader) {
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glCheckError();
glBindVertexArray(0);
}

0 comments on commit 0c69013

Please sign in to comment.