vulkanalia icon indicating copy to clipboard operation
vulkanalia copied to clipboard

Model importer in the tutorial doesn't work if the model has 2 vertices in the same place in the UV map

Open Ikbenmathijs opened this issue 1 year ago • 0 comments

tobj by default also uses an index array for the texture coordinates aswell, I rewrote the model importer to account for it

for model in &models {
        for (i, index) in model.mesh.indices.iter().enumerate() {
            let pos_offset = (3 * index) as usize;
            let tex_coord_offset = (2 * model.mesh.texcoord_indices[i]) as usize;
            let normal_offset = (3 * model.mesh.normal_indices[i]) as usize;



            let vertex = Vertex {
                pos: glm::vec3(
                    model.mesh.positions[pos_offset],
                    model.mesh.positions[pos_offset + 1],
                    model.mesh.positions[pos_offset + 2]
                ),
                color: glm::vec3(1.0, 0.0, 0.0),
                tex_coord: glm::vec2(
                    model.mesh.texcoords[tex_coord_offset],
                    1.0 - model.mesh.texcoords[tex_coord_offset + 1]
                ),
                normal: glm::vec3(
                    model.mesh.normals[normal_offset],
                    model.mesh.normals[normal_offset + 1],
                    model.mesh.normals[normal_offset + 2]
                )
            };

            if let Some(index) = unique_verticies.get(&vertex) {
                data.indicies.push(*index);
            } else {
                let index = data.vertices.len();
                data.vertices.push(vertex);
                unique_verticies.insert(vertex, index as u32);
                data.indicies.push(index as u32);
            }

        }
    }

I discovered this because the normals wouldn't import correctly with the sample model when I tried to add that later on, since the normals also use an index array. The viking room model doesn't have 2 vertices in the same place in the UV map, but I've tried other models and it didn't work with that

Ikbenmathijs avatar May 11 '23 20:05 Ikbenmathijs