Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201//// Created by kam on 17.6.2024.//
#include "include/Mesh.h"#include <iostream>#include <utility>
#include <assimp/Importer.hpp>#include <assimp/scene.h>#include <assimp/postprocess.h>
using namespace Snail;
Mesh::Instance::Instance(isize size, u32 id, u32 indices, u32 vertices, u32 normals, u32 tex_coords) : size{size}, id{id}, indices{indices}, vertices{vertices}, normals{normals}, tex_coords{tex_coords}{}
void Mesh::Instance::render() const { glBindVertexArray(id); glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, nullptr); //glDrawArrays(GL_LINES, 0, vertices.size()); glBindVertexArray(0);}
Mesh::Mesh(std::vector<f32> vertices, std::vector<u32> indices, std::vector<f32> normals, std::vector<f32> tex_coords) : vertices{std::move(vertices)}, indices{std::move(indices)}, normals{std::move(normals)}, tex_coords{std::move(tex_coords)}{}
Mesh::Mesh(const char* filename) : Mesh() { Assimp::Importer import; const aiScene* scn = import.ReadFile(filename, aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_FlipUVs);
if(!scn || scn->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scn->mRootNode) { std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl; return; }
aiNode* n = scn->mRootNode;
if (n->mNumMeshes > 1) std::cout << "WARNING::ASSIMP::Only one mesh allowed to be imported" << std::endl;
aiMesh* m = scn->mMeshes[0];
this->vertices.reserve(m->mNumVertices * 3); this->normals.reserve(m->mNumVertices * 3); this->tex_coords.reserve(m->mNumVertices * 2); this->indices.reserve(m->mNumFaces * 3);
for (int i = 0; i < m->mNumVertices; ++i) { vertices.push_back(m->mVertices[i].x); vertices.push_back(m->mVertices[i].y); vertices.push_back(m->mVertices[i].z); normals.push_back(m->mNormals[i].x); normals.push_back(m->mNormals[i].y); normals.push_back(m->mNormals[i].z);
if (m->mTextureCoords[0]) { tex_coords.push_back(m->mTextureCoords[0][i].x); tex_coords.push_back(m->mTextureCoords[0][i].y); } else { tex_coords.push_back(0); tex_coords.push_back(0); } }
for (int i = 0; i < m->mNumFaces; ++i) { aiFace f = m->mFaces[i]; for (int j = 0; j < f.mNumIndices; ++j) { indices.push_back(f.mIndices[j]); } }}
Mesh::Mesh(const std::string& filename) : Mesh(filename.c_str()) {}
auto Mesh::get_centroid() const -> glm::vec3 { glm::vec3 centroid(0.f); for (int i = 0; i < vertices.size();) { centroid.x += vertices[i++]; centroid.y += vertices[i++]; centroid.z += vertices[i++]; } return 3.f * centroid / static_cast<float>(vertices.size());}
auto Mesh::get_vertices() -> std::vector<f32> & { return vertices;}
auto Mesh::get_indices() -> std::vector<u32> & { return indices;}
auto Mesh::get_normals() -> std::vector<f32> & { return normals;}
auto Mesh::get_tex_coords() -> std::vector<f32> & { return tex_coords;}
auto Mesh::set_vertices(std::vector<f32> vertices) -> void { this->vertices = std::move(vertices);}
auto Mesh::set_indices(std::vector<u32> indices) -> void { this->indices = std::move(indices);}
auto Mesh::set_tex_coords(std::vector<f32> tex_coords) -> void { this->tex_coords = std::move(tex_coords);}
auto Mesh::set_normals(std::vector<f32> normals) -> void { this->normals = std::move(normals);}
Mesh::Instance Mesh::to_gpu() { u32 vao, vbo[4];
glGenVertexArrays(1, &vao); glBindVertexArray(vao);
glGenBuffers(4, vbo);
// Indices glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<isize>(indices.size() * sizeof(s32)),indices.data(), GL_STATIC_DRAW);
// Vertices glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); glBufferData(GL_ARRAY_BUFFER, static_cast<isize>(vertices.size() * sizeof(f32)),vertices.data(), GL_STATIC_DRAW); glEnableVertexAttribArray(0);
// Normals glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr); glBufferData(GL_ARRAY_BUFFER, static_cast<isize>(normals.size() * sizeof(f32)),normals.data(), GL_STATIC_DRAW); glEnableVertexAttribArray(1);
// Tex coords glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, nullptr); glBufferData(GL_ARRAY_BUFFER, static_cast<isize>(tex_coords.size() * sizeof(f32)),tex_coords.data(), GL_STATIC_DRAW); glEnableVertexAttribArray(2);
glBindVertexArray(0);
return {static_cast<isize>(indices.size()), vao, vbo[0], vbo[1], vbo[2], vbo[3]};}
void Mesh::update(Instance instance) { s32 len;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, instance.indices); glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &len); if (len == sizeof(s32) * indices.size()) { glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast<isize>(indices.size() * sizeof(s32)), indices.data()); } else { instance.size = static_cast<isize>(indices.size()); glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<isize>(indices.size() * sizeof(s32)), indices.data(), GL_STATIC_DRAW); } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, instance.vertices); glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &len); if (len == sizeof(s32) * vertices.size()) { glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<isize>(vertices.size() * sizeof(f32)), vertices.data()); } else { glBufferData(GL_ARRAY_BUFFER, static_cast<isize>(vertices.size() * sizeof(f32)),vertices.data(), GL_STATIC_DRAW); }
glBindBuffer(GL_ARRAY_BUFFER, instance.normals); glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &len); if (len == sizeof(s32) * normals.size()) { glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<isize>(normals.size() * sizeof(f32)), normals.data()); } else { glBufferData(GL_ARRAY_BUFFER, static_cast<isize>(normals.size() * sizeof(f32)),normals.data(), GL_STATIC_DRAW); }
glBindBuffer(GL_ARRAY_BUFFER, instance.tex_coords); glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &len); if (len == sizeof(s32) * tex_coords.size()) { glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<isize>(tex_coords.size() * sizeof(f32)), tex_coords.data()); } else { glBufferData(GL_ARRAY_BUFFER, static_cast<isize>(tex_coords.size() * sizeof(f32)),tex_coords.data(), GL_STATIC_DRAW); }}