This commit is contained in:
Daniel Ledda
2025-01-12 23:40:39 +01:00
parent 25384c743d
commit c0deceec65
9 changed files with 247 additions and 98 deletions

View File

@@ -1,40 +1,39 @@
#include <iostream>
#include <vector>
#include "Mesh.h"
#define TINYOBJLOADER_IMPLEMENTATION
#include "../lib/loaders/tinyobj.h"
#include "../lib/djstdlib/core.h"
Mesh createMesh(const char* obj_file) {
Scratch temp = scratchStart(0, 0);
Mesh result = {0};
tinyobj::ObjReader reader = tinyobj::ObjReader();
bool success = reader.ParseFromFile(obj_file);
std::cout << reader.Error() << std::endl;
log("%s\n", reader.Error().c_str());
const tinyobj::attrib_t attrib = reader.GetAttrib();
std::vector<tinyobj::index_t> indices_t = reader.GetShapes().at(0).mesh.indices;
std::vector<uint32> indices = std::vector<uint32>(indices_t.size());
list<uint32> indices = PushFullList(temp.arena, uint32, 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());
list<real32> vertices = PushFullList(temp.arena, real32, 3*indices_t.size());
list<real32> normals = PushFullList(temp.arena, real32, 3*indices_t.size());
list<real32> texcoords = PushFullList(temp.arena, real32, 2*indices_t.size());
for (int i = 0; i < indices_t.size(); 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];
vertices.data[3*i] = attrib.vertices[3*vertex_data.vertex_index];
vertices.data[3*i+1] = attrib.vertices[3*vertex_data.vertex_index + 1];
vertices.data[3*i+2] = attrib.vertices[3*vertex_data.vertex_index + 2];
normals[3*i] = attrib.normals[3*vertex_data.normal_index];
normals[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1];
normals[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2];
normals.data[3*i] = attrib.normals[3*vertex_data.normal_index];
normals.data[3*i+1] = attrib.normals[3*vertex_data.normal_index + 1];
normals.data[3*i+2] = attrib.normals[3*vertex_data.normal_index + 2];
texcoords[2*i] = attrib.texcoords[2*vertex_data.texcoord_index];
texcoords[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1];
texcoords.data[2*i] = attrib.texcoords[2*vertex_data.texcoord_index];
texcoords.data[2*i+1] = attrib.texcoords[2*vertex_data.texcoord_index + 1];
indices[i] = i;
indices.data[i] = i;
}
result.num_indices = indices_t.size();
@@ -47,22 +46,24 @@ Mesh createMesh(const char* obj_file) {
glBindVertexArray(result.vao);
glBindBuffer(GL_ARRAY_BUFFER, result.vbo_xyz);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.length * 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, result.vbo_uv);
glBufferData(GL_ARRAY_BUFFER, texcoords.size() * sizeof(float), texcoords.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, texcoords.length * 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, result.vbo_norm);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float), normals.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, normals.length * 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);
scratchEnd(temp);
return result;
}