Twinklebear / tobj

Tiny OBJ Loader in Rust
MIT License
235 stars 47 forks source link

Parser is finding the wrong number of texture coordinates #29

Closed calvin-godfrey closed 4 years ago

calvin-godfrey commented 4 years ago

I have a model I'm trying to render that I found from here. It says in the obj that there are 36525 texture coordinates, and I verified that by counting the number of occurrences of vt. However, when I load it with let obj_res: Result<(Vec<tobj::Model>, Vec<tobj::Material>), tobj::LoadError> = tobj::load_obj(path, true), the resulting mesh has a total of 73060 individual coordinates, which make for a total of 36530 texture coordinates, off by 5.

I'm going to continue debugging in case there's something I'm doing wrong, but I wanted to post it here.

calvin-godfrey commented 4 years ago

I did some editing to the source code and it's a little weirder than I first thought. I added a print statement right before the return in load_obj_buf, and it prints out the expected length (73050). Then in my calling code I have:

    let obj_res: Result<(Vec<tobj::Model>, Vec<tobj::Material>), tobj::LoadError> = tobj::load_obj(path, true);    
    match obj_res {
        Ok((models, mats)) => {
            let model = &models[0];
            let mesh = &model.mesh;
            println!("HERE: {}", mesh.texcoords.len());

and that print statement prints out 73060, the wrong number. I also verified that models.len() is 1, so I'm still not sure where the problem is, but it appears to be in my code.

Edit: After further debugging, the problem, if there is one, seems to be in add_vertex which is adding the extra coordinates. It could be that I'm using this the wrong way, but I wrote my own obj parser in C and it was never necessary to create new coordinates.

calvin-godfrey commented 4 years ago

This morning I did more testing, and fixed the issue. When trying to read the pixel corresponding to given uv coordinates, I mixed up which corner of the image is (0,0). Works fine now!