From 2cb73fa85d8a8cfba3b858c2f4fc32c8835bee2a Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sat, 13 Aug 2016 02:52:36 +0900 Subject: [PATCH] Fix the ordefing of a constructor call of `vertex_index`. --- experimental/tinyobj_loader_opt.h | 19 ++++++++++--------- experimental/viewer.cc | 25 ++++++++++++++----------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/experimental/tinyobj_loader_opt.h b/experimental/tinyobj_loader_opt.h index 9fa91e1..2088a6e 100644 --- a/experimental/tinyobj_loader_opt.h +++ b/experimental/tinyobj_loader_opt.h @@ -969,7 +969,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len, // vertex if (token[0] == 'v' && IS_SPACE((token[1]))) { token += 2; - float x, y, z; + float x = 0.0f, y = 0.0f, z = 0.0f; parseFloat3(&x, &y, &z, &token); command->vx = x; command->vy = y; @@ -981,7 +981,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len, // normal if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) { token += 3; - float x, y, z; + float x = 0.0f, y = 0.0f, z = 0.0f; parseFloat3(&x, &y, &z, &token); command->nx = x; command->ny = y; @@ -993,7 +993,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len, // texcoord if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) { token += 3; - float x, y; + float x = 0.0f, y = 0.0f; parseFloat2(&x, &y, &token); command->tx = x; command->ty = y; @@ -1360,10 +1360,11 @@ bool parseObj(attrib_t *attrib, std::vector *shapes, const char *buf, num_f += command_count[t].num_f; num_faces += command_count[t].num_faces; } - // std::cout << "# v " << num_v << std::endl; - // std::cout << "# vn " << num_vn << std::endl; - // std::cout << "# vt " << num_vt << std::endl; - // std::cout << "# f " << num_f << std::endl; + + //std::cout << "# v " << num_v << std::endl; + //std::cout << "# vn " << num_vn << std::endl; + //std::cout << "# vt " << num_vt << std::endl; + //std::cout << "# f " << num_f << std::endl; // 4. merge // @todo { parallelize merge. } @@ -1442,9 +1443,9 @@ bool parseObj(attrib_t *attrib, std::vector *shapes, const char *buf, for (size_t k = 0; k < commands[t][i].f.size(); k++) { vertex_index &vi = commands[t][i].f[k]; int v_idx = fixIndex(vi.v_idx, v_count); - int vn_idx = fixIndex(vi.vn_idx, n_count); int vt_idx = fixIndex(vi.vt_idx, t_count); - attrib->faces[f_count + k] = vertex_index(v_idx, vn_idx, vt_idx); + int vn_idx = fixIndex(vi.vn_idx, n_count); + attrib->faces[f_count + k] = vertex_index(v_idx, vt_idx, vn_idx); } attrib->material_ids[face_count] = material_id; attrib->face_num_verts[face_count] = commands[t][i].f.size(); diff --git a/experimental/viewer.cc b/experimental/viewer.cc index 5c9cc49..e64f31d 100644 --- a/experimental/viewer.cc +++ b/experimental/viewer.cc @@ -234,6 +234,9 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n bmin[0] = bmin[1] = bmin[2] = std::numeric_limits::max(); bmax[0] = bmax[1] = bmax[2] = -std::numeric_limits::max(); + //std::cout << "vertices.size() = " << attrib.vertices.size() << std::endl; + //std::cout << "normals.size() = " << attrib.normals.size() << std::endl; + { DrawObject o; std::vector vb; // pos(3float), normal(3float), color(3float) @@ -268,18 +271,18 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n float n[3][3]; if (attrib.normals.size() > 0) { - int f0 = idx0.vn_idx; - int f1 = idx1.vn_idx; - int f2 = idx2.vn_idx; + int nf0 = idx0.vn_idx; + int nf1 = idx1.vn_idx; + int nf2 = idx2.vn_idx; - if (f0 >= 0 && f1 >= 0 && f2 >= 0) { - assert(3*f0+2 < attrib.normals.size()); - assert(3*f1+2 < attrib.normals.size()); - assert(3*f2+2 < attrib.normals.size()); + if (nf0 >= 0 && nf1 >= 0 && nf2 >= 0) { + assert(3*nf0+2 < attrib.normals.size()); + assert(3*nf1+2 < attrib.normals.size()); + assert(3*nf2+2 < attrib.normals.size()); for (int k = 0; k < 3; k++) { - n[0][k] = attrib.normals[3*f0+k]; - n[1][k] = attrib.normals[3*f1+k]; - n[2][k] = attrib.normals[3*f2+k]; + n[0][k] = attrib.normals[3*nf0+k]; + n[1][k] = attrib.normals[3*nf1+k]; + n[2][k] = attrib.normals[3*nf2+k]; } } else { // compute geometric normal @@ -304,7 +307,7 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n // Use normal as color. float c[3] = {n[k][0], n[k][1], n[k][2]}; float len2 = c[0] * c[0] + c[1] * c[1] + c[2] * c[2]; - if (len2 > 0.0f) { + if (len2 > 1.0e-6f) { float len = sqrtf(len2); c[0] /= len;