fix: texture prasing

This commit is contained in:
2025-10-13 20:56:26 +02:00
parent c1a5be6d90
commit c30419899d
2 changed files with 57 additions and 59 deletions

View File

@@ -27,21 +27,8 @@ Buffer::Buffer(const std::vector<Vertex>& vertices,
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint16_t), glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint16_t),
indices.data(), GL_STATIC_DRAW); indices.data(), GL_STATIC_DRAW);
// Attribute 0: position (vec3) // Note: Vertex attributes are set up by Shader::setup_attribs() during rendering
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), // This allows the shader to dynamically query and set the correct attribute locations
reinterpret_cast<void*>(0));
glEnableVertexAttribArray(0);
// Attribute 1: color (vec3)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<void*>(sizeof(glm::vec3)));
glEnableVertexAttribArray(1);
// Attribute 2: tex_coord (vec2)
glVertexAttribPointer(
2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<void*>(sizeof(glm::vec3) * 2));
glEnableVertexAttribArray(2);
Logger::info(sstr("Buffer created with ", vertices.size(), Logger::info(sstr("Buffer created with ", vertices.size(),
" vertices and ", indices.size(), " indices")); " vertices and ", indices.size(), " indices"));

View File

@@ -30,46 +30,51 @@ Vertex create_vertex(const tinyobj::attrib_t& attrib,
Vertex vertex; Vertex vertex;
// Position // Position
vertex.position.x = attrib.vertices[3 * idx.vertex_index + 0]; if (idx.vertex_index >= 0) {
vertex.position.y = attrib.vertices[3 * idx.vertex_index + 1]; vertex.position.x = attrib.vertices[3 * idx.vertex_index + 0];
vertex.position.z = attrib.vertices[3 * idx.vertex_index + 2]; vertex.position.y = attrib.vertices[3 * idx.vertex_index + 1];
vertex.position.z = attrib.vertices[3 * idx.vertex_index + 2];
}
// Color (default to white) // Color (default to white)
vertex.color = glm::vec3(1.0f, 1.0f, 1.0f); vertex.color = glm::vec3(1.0f, 1.0f, 1.0f);
// Texture coordinates // Texture coordinates (default to 0,0)
vertex.tex_coord = glm::vec2(0.0f, 0.0f);
if (idx.texcoord_index >= 0) { if (idx.texcoord_index >= 0) {
vertex.tex_coord.x = vertex.tex_coord.x =
attrib.texcoords[2 * idx.texcoord_index + 0]; attrib.texcoords[2 * idx.texcoord_index + 0];
vertex.tex_coord.y = 1.0f vertex.tex_coord.y =
- attrib.texcoords[2 * idx.texcoord_index + 1]; // Flip Y attrib.texcoords[2 * idx.texcoord_index + 1]; // No Y-flip
} }
return vertex; return vertex;
} }
void group_vertices_by_material( void process_shape_simple(const tinyobj::shape_t& shape,
const tinyobj::shape_t& shape, const tinyobj::attrib_t& attrib, const tinyobj::attrib_t& attrib,
std::unordered_map<int, std::vector<Vertex>>& out_vertices, std::vector<Vertex>& out_vertices,
std::unordered_map<int, std::vector<uint16_t>>& out_indices) std::vector<uint16_t>& out_indices)
{ {
size_t index_offset = 0; // Process all indices directly - one vertex per index
for (size_t f = 0; f < shape.mesh.num_face_vertices.size(); ++f) { for (size_t i = 0; i < shape.mesh.indices.size(); ++i) {
int fv = shape.mesh.num_face_vertices[f]; const tinyobj::index_t& idx = shape.mesh.indices[i];
int material_id = shape.mesh.material_ids[f]; Vertex vertex = create_vertex(attrib, idx);
for (size_t v = 0; v < static_cast<size_t>(fv); ++v) { // Debug: Print first 3 vertices
const tinyobj::index_t& idx = if (i < 3) {
shape.mesh.indices[index_offset + v]; Logger::info(sstr(" Vertex ", i, ": pos(",
vertex.position.x, ",",
Vertex vertex = create_vertex(attrib, idx); vertex.position.y, ",",
out_vertices[material_id].push_back(vertex); vertex.position.z, ") tex(",
out_indices[material_id].push_back( vertex.tex_coord.x, ",",
static_cast<uint16_t>( vertex.tex_coord.y, ") texidx=",
out_vertices[material_id].size() - 1)); idx.texcoord_index));
} }
index_offset += fv; out_vertices.push_back(vertex);
out_indices.push_back(
static_cast<uint16_t>(out_vertices.size() - 1));
} }
} }
@@ -113,22 +118,26 @@ std::shared_ptr<Texture> load_material_texture(int material_id,
return texture; return texture;
} }
void create_buffers_for_materials( // Create a single buffer for the shape with first material
const std::unordered_map<int, std::vector<Vertex>>& material_vertices, void create_buffer_simple(const tinyobj::shape_t& shape,
const std::unordered_map<int, std::vector<uint16_t>>& material_indices, const std::vector<Vertex>& vertices,
LoadContext& context, std::shared_ptr<Mesh>& mesh) const std::vector<uint16_t>& indices, LoadContext& context,
std::shared_ptr<Mesh>& mesh)
{ {
for (const auto& [material_id, vertices] : material_vertices) { auto buffer = std::make_shared<Buffer>(vertices, indices);
const auto& indices = material_indices.at(material_id);
auto buffer = std::make_shared<Buffer>(vertices, indices); // Use first material ID from the shape
auto texture = load_material_texture(material_id, context); int material_id = -1;
auto material_shader = if (!shape.mesh.material_ids.empty() && shape.mesh.material_ids[0] >= 0) {
context.shader ? context.shader : state::default_shader; material_id = shape.mesh.material_ids[0];
Material mat(texture, material_shader);
mesh->add_buffer(buffer, mat);
} }
auto texture = load_material_texture(material_id, context);
auto material_shader =
context.shader ? context.shader : state::default_shader;
Material mat(texture, material_shader);
mesh->add_buffer(buffer, mat);
} }
} // namespace } // namespace
@@ -174,15 +183,17 @@ std::shared_ptr<Mesh> Mesh::load(const char* filename,
// Process each shape // Process each shape
auto mesh = std::make_shared<Mesh>(); auto mesh = std::make_shared<Mesh>();
for (const auto& shape : shapes) { for (const auto& shape : shapes) {
Logger::info(sstr("Processing shape: ", shape.name)); Logger::info(sstr("Processing shape: ", shape.name, " with ",
shape.mesh.indices.size(), " indices"));
std::unordered_map<int, std::vector<Vertex>> material_vertices; std::vector<Vertex> vertices;
std::unordered_map<int, std::vector<uint16_t>> material_indices; std::vector<uint16_t> indices;
group_vertices_by_material(shape, attrib, material_vertices, process_shape_simple(shape, attrib, vertices, indices);
material_indices); create_buffer_simple(shape, vertices, indices, context, mesh);
create_buffers_for_materials(material_vertices,
material_indices, context, mesh); Logger::info(sstr("Created buffer with ", vertices.size(),
" vertices"));
} }
Logger::info(sstr("Mesh loaded successfully with ", mesh->num_buffers(), Logger::info(sstr("Mesh loaded successfully with ", mesh->num_buffers(),