thisistherk / fast_obj

Fast C OBJ parser
MIT License
616 stars 46 forks source link

bug - off by one #33

Closed zmn28hgbn59kcmlpio8unfh7fdre523esd28q9a closed 2 years ago

zmn28hgbn59kcmlpio8unfh7fdre523esd28q9a commented 2 years ago

The off by one bug is affecting vertex position, texcoords and normals count. Another bug is fastobj report illum (in mtl file) to be 0 regardless of its value.

here is a sample cube (.obj) from blender, it has 8 vertices, 14 texcoords, and 6 normals:

mtllib untitled.mtl o Cube v 1.000000 1.000000 -1.000000 v 1.000000 -1.000000 -1.000000 v 1.000000 1.000000 1.000000 v 1.000000 -1.000000 1.000000 v -1.000000 1.000000 -1.000000 v -1.000000 -1.000000 -1.000000 v -1.000000 1.000000 1.000000 v -1.000000 -1.000000 1.000000 vt 0.875000 0.500000 vt 0.625000 0.750000 vt 0.625000 0.500000 vt 0.375000 1.000000 vt 0.375000 0.750000 vt 0.625000 0.000000 vt 0.375000 0.250000 vt 0.375000 0.000000 vt 0.375000 0.500000 vt 0.125000 0.750000 vt 0.125000 0.500000 vt 0.625000 0.250000 vt 0.875000 0.750000 vt 0.625000 1.000000 vn 0.0000 1.0000 0.0000 vn 0.0000 0.0000 1.0000 vn -1.0000 0.0000 0.0000 vn 0.0000 -1.0000 0.0000 vn 1.0000 0.0000 0.0000 vn 0.0000 0.0000 -1.0000 usemtl Material s off f 5/1/1 3/2/1 1/3/1 f 3/2/2 8/4/2 4/5/2 f 7/6/3 6/7/3 8/8/3 f 2/9/4 8/10/4 6/11/4 f 1/3/5 4/5/5 2/9/5 f 5/12/6 2/9/6 6/7/6 f 5/1/1 7/13/1 3/2/1 f 3/2/2 7/14/2 8/4/2 f 7/6/3 5/12/3 6/7/3 f 2/9/4 4/5/4 8/10/4 f 1/3/5 3/2/5 4/5/5 f 5/12/6 1/3/6 2/9/6

and here is the mtl, notice the illum is 2:

newmtl Material Ns 323.999994 Ka 1.000000 1.000000 1.000000 Kd 0.800000 0.800000 0.800000 Ks 0.500000 0.500000 0.500000 Ke 0.000000 0.000000 0.000000 Ni 1.450000 d 1.000000 illum 2

finally the test program and its output: the test:


#include <stdio.h>

#define FAST_OBJ_IMPLEMENTATION
#include "fast_obj.h"

int main(int argc, char **argv)
{
    static fastObjMesh *mesh;
    mesh = fast_obj_read("untitled.obj");
    if (mesh) {
        fprintf(stdout, "vertices count: %i\n", mesh->position_count);
        fprintf(stdout, "texture coords count: %i\n", mesh->texcoord_count);
        fprintf(stdout, "normals count: %i\n", mesh->normal_count);
        fprintf(stdout, "face count: %i\n", mesh->face_count);
        fprintf(stdout, "index count: %i\n", mesh->index_count);
        for (int i=0;i<mesh->index_count;++i)
            fprintf(stdout, "\tindices p:%i t:%i n:%i\n", mesh->indices[i].p, mesh->indices[i].t, mesh->indices[i].n);
        fprintf(stdout, "material count: %i\n", mesh->material_count);
        for (int i=0;i<mesh->material_count;++i) {
            fprintf(stdout, "\tambient: %f, %f, %f\n", mesh->materials[i].Ka[0], mesh->materials[i].Ka[1], mesh->materials[i].Ka[2]);
            fprintf(stdout, "\tdiffuse: %f, %f, %f\n", mesh->materials[i].Kd[0], mesh->materials[i].Kd[1], mesh->materials[i].Kd[2]);
            fprintf(stdout, "\tspecular: %f, %f, %f\n", mesh->materials[i].Ks[0], mesh->materials[i].Ks[1], mesh->materials[i].Ks[2]);
            fprintf(stdout, "\temission: %f, %f, %f\n", mesh->materials[i].Ke[0], mesh->materials[i].Ke[1], mesh->materials[i].Ke[2]);
            fprintf(stdout, "\ttransmittance: %f, %f, %f\n", mesh->materials[i].Kt[0], mesh->materials[i].Kt[1], mesh->materials[i].Kt[2]);
            fprintf(stdout, "\tshininess: %f\n", mesh->materials[i].Ns);
            fprintf(stdout, "\trefraction index: %f\n", mesh->materials[i].Ni);
            fprintf(stdout, "\ttransmission filter: %f, %f, %f\n", mesh->materials[i].Tf[0], mesh->materials[i].Tf[1], mesh->materials[i].Tf[2]);
            fprintf(stdout, "\tdisolve: %f\n", mesh->materials[i].d);
            fprintf(stdout, "\tillum: %i\n", mesh->materials[i].illum);
        }
        fprintf(stdout, "object count: %i\n", mesh->object_count);
        fprintf(stdout, "group count: %i\n", mesh->group_count);
        fast_obj_destroy(mesh);
    }
    return 0;
}
/*`

the output, notice vertex position/texcoords/normals count are off by one, and illum reported as 0:
vertices count: 9
texture coords count: 15
normals count: 7
face count: 12
index count: 36
    indices p:5 t:1 n:1
    indices p:3 t:2 n:1
    indices p:1 t:3 n:1
    indices p:3 t:2 n:2
    indices p:8 t:4 n:2
    indices p:4 t:5 n:2
    indices p:7 t:6 n:3
    indices p:6 t:7 n:3
    indices p:8 t:8 n:3
    indices p:2 t:9 n:4
    indices p:8 t:10 n:4
    indices p:6 t:11 n:4
    indices p:1 t:3 n:5
    indices p:4 t:5 n:5
    indices p:2 t:9 n:5
    indices p:5 t:12 n:6
    indices p:2 t:9 n:6
    indices p:6 t:7 n:6
    indices p:5 t:1 n:1
    indices p:7 t:13 n:1
    indices p:3 t:2 n:1
    indices p:3 t:2 n:2
    indices p:7 t:14 n:2
    indices p:8 t:4 n:2
    indices p:7 t:6 n:3
    indices p:5 t:12 n:3
    indices p:6 t:7 n:3
    indices p:2 t:9 n:4
    indices p:4 t:5 n:4
    indices p:8 t:10 n:4
    indices p:1 t:3 n:5
    indices p:3 t:2 n:5
    indices p:4 t:5 n:5
    indices p:5 t:12 n:6
    indices p:1 t:3 n:6
    indices p:2 t:9 n:6
material count: 1
    ambient: 1.000000, 1.000000, 1.000000
    diffuse: 0.800000, 0.800000, 0.800000
    specular: 0.500000, 0.500000, 0.500000
    emission: 0.000000, 0.000000, 0.000000
    transmittance: 0.000000, 0.000000, 0.000000
    shininess: 324.000000
    refraction index: 1.450000
    transmission filter: 1.000000, 1.000000, 1.000000
    disolve: 1.000000
    illum: 0
object count: 1
group count: 1*/
zmn28hgbn59kcmlpio8unfh7fdre523esd28q9a commented 2 years ago

sorry, it was my first use and i didnt read the description quite well; not a bug, closing.