Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Ikbenmathijs opened this issue May 11, 2023 · 0 comments
Labels
tutorial An issue relating to the tutorial

Comments

@Ikbenmathijs
Copy link

Ikbenmathijs commented May 11, 2023

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

@KyleMayes KyleMayes added the tutorial An issue relating to the tutorial label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tutorial An issue relating to the tutorial
Projects
None yet
Development

No branches or pull requests

2 participants