added new cmake setup, graphics, vendors, obj importer, etc.

This commit is contained in:
Daniel Ledda
2022-12-29 12:57:19 +01:00
parent 783f9ee055
commit e83185f011
30 changed files with 18711 additions and 116 deletions

60
src/gfx/Mesh.cpp Normal file
View File

@@ -0,0 +1,60 @@
#include <iostream>
#include "Mesh.h"
#include "loaders/tinyobj.h"
auto Mesh::init(const char* obj_file) -> void {
auto reader = tinyobj::ObjReader();
auto success = reader.ParseFromFile(obj_file);
std::cout << reader.Error() << std::endl;
auto attrib = reader.GetAttrib();
auto indices_t = reader.GetShapes().at(0).mesh.indices;
auto indices = std::vector<unsigned int>(indices_t.size());
for (int i = 0; i < indices_t.size(); i++) {
indices[i] = indices_t[i].vertex_index;
}
num_indices = indices.size();
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo_xyz);
glGenBuffers(1, &vbo_uv);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz);
glBufferData(GL_ARRAY_BUFFER, attrib.vertices.size() * sizeof(float), attrib.vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_uv);
glBufferData(GL_ARRAY_BUFFER, attrib.texcoords.size() * sizeof(float), attrib.texcoords.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
}
auto Mesh::init(const LeddaGeometry::Shape* shape) -> void {
num_indices = shape->indices_size;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo_xyz);
glGenBuffers(1, &vbo_uv);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz);
glBufferData(GL_ARRAY_BUFFER, shape->xyz_size * sizeof(float), shape->xyz, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_uv);
glBufferData(GL_ARRAY_BUFFER, shape->uv_size * sizeof(float), shape->uv, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape->indices_size * sizeof(unsigned int), shape->indices, GL_STATIC_DRAW);
}