2021-01-13 10:42:57 -06:00
|
|
|
#include "Mesh.h"
|
|
|
|
|
2021-01-17 14:36:38 -06:00
|
|
|
Mesh::Mesh() {
|
|
|
|
material = new Material();
|
|
|
|
}
|
|
|
|
|
|
|
|
Mesh::~Mesh() {
|
2021-01-18 18:25:47 -06:00
|
|
|
if (material) {
|
|
|
|
delete material;
|
|
|
|
}
|
2021-01-17 14:36:38 -06:00
|
|
|
}
|
2021-01-13 10:42:57 -06:00
|
|
|
|
|
|
|
Mesh::Mesh(VertexList vertices, IndexList indices) {
|
|
|
|
this->vertices = vertices;
|
|
|
|
this->indices = indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::SetupMesh() {
|
|
|
|
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[0], GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index),
|
|
|
|
&indices[0], GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// Vertex positions
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0);
|
2021-01-17 14:36:38 -06:00
|
|
|
// Vertex UV
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float)));
|
|
|
|
// TODO normals
|
2021-01-13 10:42:57 -06:00
|
|
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
2021-01-18 18:25:47 -06:00
|
|
|
Mesh* Mesh::FromFile(const char *filename) {
|
|
|
|
// HOCUS: https://assimp-docs.readthedocs.io/en/latest/usage/use_the_lib.html
|
|
|
|
Assimp::Importer importer;
|
|
|
|
|
|
|
|
const aiScene* scene = importer.ReadFile(
|
|
|
|
filename, // Read the file
|
|
|
|
aiProcess_Triangulate | // We only do triangles
|
|
|
|
aiProcess_GenNormals | // We want normals precalculated
|
|
|
|
aiProcess_GenUVCoords // We want UV mappings precalculated
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!scene) {
|
|
|
|
std::cerr << importer.GetErrorString() << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
aiNode *root = scene->mRootNode;
|
|
|
|
aiMesh *mesh_to_import = scene->mMeshes[root->mMeshes[0]];
|
|
|
|
|
|
|
|
return Util::aiMesh2Mesh(mesh_to_import);
|
2021-01-18 13:31:09 -06:00
|
|
|
}
|
|
|
|
|
2021-01-13 10:42:57 -06:00
|
|
|
void Mesh::Draw() {
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|