fix: drawing issue
This commit is contained in:
49
src/mesh.cpp
49
src/mesh.cpp
@@ -39,39 +39,26 @@ Vertex create_vertex(const tinyobj::attrib_t& attrib,
|
|||||||
// 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 (default to 0,0)
|
// Texture coordinates
|
||||||
vertex.tex_coord = glm::vec2(0.0f, 0.0f);
|
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 = attrib.texcoords[2 * idx.texcoord_index + 1];
|
||||||
vertex.tex_coord.y =
|
|
||||||
attrib.texcoords[2 * idx.texcoord_index + 1]; // No Y-flip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return vertex;
|
return vertex;
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_shape_simple(const tinyobj::shape_t& shape,
|
void process_shape(const tinyobj::shape_t& shape,
|
||||||
const tinyobj::attrib_t& attrib,
|
const tinyobj::attrib_t& attrib,
|
||||||
std::vector<Vertex>& out_vertices,
|
std::vector<Vertex>& out_vertices,
|
||||||
std::vector<uint16_t>& out_indices)
|
std::vector<uint16_t>& out_indices)
|
||||||
{
|
{
|
||||||
// Process all indices directly - one vertex per index
|
// Process all indices - create one vertex per index
|
||||||
for (size_t i = 0; i < shape.mesh.indices.size(); ++i) {
|
for (size_t i = 0; i < shape.mesh.indices.size(); ++i) {
|
||||||
const tinyobj::index_t& idx = shape.mesh.indices[i];
|
const tinyobj::index_t& idx = shape.mesh.indices[i];
|
||||||
Vertex vertex = create_vertex(attrib, idx);
|
Vertex vertex = create_vertex(attrib, idx);
|
||||||
|
|
||||||
// Debug: Print first 3 vertices
|
|
||||||
if (i < 3) {
|
|
||||||
Logger::info(sstr(" Vertex ", i, ": pos(",
|
|
||||||
vertex.position.x, ",",
|
|
||||||
vertex.position.y, ",",
|
|
||||||
vertex.position.z, ") tex(",
|
|
||||||
vertex.tex_coord.x, ",",
|
|
||||||
vertex.tex_coord.y, ") texidx=",
|
|
||||||
idx.texcoord_index));
|
|
||||||
}
|
|
||||||
|
|
||||||
out_vertices.push_back(vertex);
|
out_vertices.push_back(vertex);
|
||||||
out_indices.push_back(
|
out_indices.push_back(
|
||||||
static_cast<uint16_t>(out_vertices.size() - 1));
|
static_cast<uint16_t>(out_vertices.size() - 1));
|
||||||
@@ -118,11 +105,11 @@ std::shared_ptr<Texture> load_material_texture(int material_id,
|
|||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a single buffer for the shape with first material
|
void create_buffer_for_shape(const tinyobj::shape_t& shape,
|
||||||
void create_buffer_simple(const tinyobj::shape_t& shape,
|
const std::vector<Vertex>& vertices,
|
||||||
const std::vector<Vertex>& vertices,
|
const std::vector<uint16_t>& indices,
|
||||||
const std::vector<uint16_t>& indices, LoadContext& context,
|
LoadContext& context,
|
||||||
std::shared_ptr<Mesh>& mesh)
|
std::shared_ptr<Mesh>& mesh)
|
||||||
{
|
{
|
||||||
auto buffer = std::make_shared<Buffer>(vertices, indices);
|
auto buffer = std::make_shared<Buffer>(vertices, indices);
|
||||||
|
|
||||||
@@ -183,17 +170,13 @@ 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, " with ",
|
Logger::info(sstr("Processing shape: ", shape.name));
|
||||||
shape.mesh.indices.size(), " indices"));
|
|
||||||
|
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
std::vector<uint16_t> indices;
|
std::vector<uint16_t> indices;
|
||||||
|
|
||||||
process_shape_simple(shape, attrib, vertices, indices);
|
process_shape(shape, attrib, vertices, indices);
|
||||||
create_buffer_simple(shape, vertices, indices, context, mesh);
|
create_buffer_for_shape(shape, vertices, 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(),
|
||||||
|
|||||||
Reference in New Issue
Block a user