This commit is contained in:
Daniel Ledda
2025-01-04 19:19:01 +01:00
parent 256292c20d
commit 50f4501c86
11 changed files with 3213 additions and 1484 deletions

View File

@@ -1,23 +1,28 @@
#include <iostream>
#include <vector>
#include "Mesh.h"
#define TINYOBJLOADER_IMPLEMENTATION
#include "../lib/loaders/tinyobj.h"
#include "../lib/djstdlib/core.h"
auto Mesh::init(const char* obj_file) -> void {
auto reader = tinyobj::ObjReader();
auto success = reader.ParseFromFile(obj_file);
Mesh createMesh(const char* obj_file) {
Mesh result = {0};
tinyobj::ObjReader reader = tinyobj::ObjReader();
bool success = reader.ParseFromFile(obj_file);
std::cout << reader.Error() << std::endl;
auto attrib = reader.GetAttrib();
const tinyobj::attrib_t attrib = reader.GetAttrib();
auto indices_t = reader.GetShapes().at(0).mesh.indices;
auto indices = std::vector<unsigned int>(indices_t.size());
std::vector<tinyobj::index_t> indices_t = reader.GetShapes().at(0).mesh.indices;
std::vector<uint32> indices = std::vector<uint32>(indices_t.size());
auto vertices = std::vector<float>(3*indices_t.size());
auto normals = std::vector<float>(3*indices_t.size());
auto texcoords = std::vector<float>(2*indices_t.size());
std::vector<real32> vertices = std::vector<real32>(3*indices_t.size());
std::vector<real32> normals = std::vector<real32>(3*indices_t.size());
std::vector<real32> texcoords = std::vector<real32>(2*indices_t.size());
for (int i = 0; i < indices_t.size(); i++) {
auto vertex_data = indices_t[i];
tinyobj::index_t vertex_data = indices_t[i];
vertices[3*i] = attrib.vertices[3*vertex_data.vertex_index];
vertices[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
vertices[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2];
@@ -32,54 +37,59 @@ auto Mesh::init(const char* obj_file) -> void {
indices[i] = i;
}
num_indices = indices_t.size();
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo_xyz);
glGenBuffers(1, &vbo_uv);
glGenBuffers(1, &vbo_norm);
result.num_indices = indices_t.size();
glGenVertexArrays(1, &result.vao);
glGenBuffers(1, &result.vbo_xyz);
glGenBuffers(1, &result.vbo_uv);
glGenBuffers(1, &result.vbo_norm);
//glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindVertexArray(result.vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz);
glBindBuffer(GL_ARRAY_BUFFER, result.vbo_xyz);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), 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);
glBindBuffer(GL_ARRAY_BUFFER, result.vbo_uv);
glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(float), texcoords.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vbo_norm);
glBindBuffer(GL_ARRAY_BUFFER, result.vbo_norm);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(2);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
return result;
}
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);
Mesh createMesh(const Shape* shape) {
Mesh result = {0};
glBindVertexArray(vao);
result.num_indices = shape->indices_size;
glGenVertexArrays(1, &result.vao);
glGenBuffers(1, &result.vbo_xyz);
glGenBuffers(1, &result.vbo_uv);
glGenBuffers(1, &result.ebo);
glBindBuffer(GL_ARRAY_BUFFER, vbo_xyz);
glBindVertexArray(result.vao);
glBindBuffer(GL_ARRAY_BUFFER, result.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);
glBindBuffer(GL_ARRAY_BUFFER, result.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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape->indices_size * sizeof(unsigned int), shape->indices, GL_STATIC_DRAW);
return result;
}