Skip to content

Commit 2cb73fa

Browse files
committed
Fix the ordefing of a constructor call of vertex_index.
1 parent 5a832b4 commit 2cb73fa

2 files changed

Lines changed: 25 additions & 21 deletions

File tree

experimental/tinyobj_loader_opt.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
969969
// vertex
970970
if (token[0] == 'v' && IS_SPACE((token[1]))) {
971971
token += 2;
972-
float x, y, z;
972+
float x = 0.0f, y = 0.0f, z = 0.0f;
973973
parseFloat3(&x, &y, &z, &token);
974974
command->vx = x;
975975
command->vy = y;
@@ -981,7 +981,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
981981
// normal
982982
if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) {
983983
token += 3;
984-
float x, y, z;
984+
float x = 0.0f, y = 0.0f, z = 0.0f;
985985
parseFloat3(&x, &y, &z, &token);
986986
command->nx = x;
987987
command->ny = y;
@@ -993,7 +993,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
993993
// texcoord
994994
if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) {
995995
token += 3;
996-
float x, y;
996+
float x = 0.0f, y = 0.0f;
997997
parseFloat2(&x, &y, &token);
998998
command->tx = x;
999999
command->ty = y;
@@ -1360,10 +1360,11 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
13601360
num_f += command_count[t].num_f;
13611361
num_faces += command_count[t].num_faces;
13621362
}
1363-
// std::cout << "# v " << num_v << std::endl;
1364-
// std::cout << "# vn " << num_vn << std::endl;
1365-
// std::cout << "# vt " << num_vt << std::endl;
1366-
// std::cout << "# f " << num_f << std::endl;
1363+
1364+
//std::cout << "# v " << num_v << std::endl;
1365+
//std::cout << "# vn " << num_vn << std::endl;
1366+
//std::cout << "# vt " << num_vt << std::endl;
1367+
//std::cout << "# f " << num_f << std::endl;
13671368

13681369
// 4. merge
13691370
// @todo { parallelize merge. }
@@ -1442,9 +1443,9 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
14421443
for (size_t k = 0; k < commands[t][i].f.size(); k++) {
14431444
vertex_index &vi = commands[t][i].f[k];
14441445
int v_idx = fixIndex(vi.v_idx, v_count);
1445-
int vn_idx = fixIndex(vi.vn_idx, n_count);
14461446
int vt_idx = fixIndex(vi.vt_idx, t_count);
1447-
attrib->faces[f_count + k] = vertex_index(v_idx, vn_idx, vt_idx);
1447+
int vn_idx = fixIndex(vi.vn_idx, n_count);
1448+
attrib->faces[f_count + k] = vertex_index(v_idx, vt_idx, vn_idx);
14481449
}
14491450
attrib->material_ids[face_count] = material_id;
14501451
attrib->face_num_verts[face_count] = commands[t][i].f.size();

experimental/viewer.cc

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n
234234
bmin[0] = bmin[1] = bmin[2] = std::numeric_limits<float>::max();
235235
bmax[0] = bmax[1] = bmax[2] = -std::numeric_limits<float>::max();
236236

237+
//std::cout << "vertices.size() = " << attrib.vertices.size() << std::endl;
238+
//std::cout << "normals.size() = " << attrib.normals.size() << std::endl;
239+
237240
{
238241
DrawObject o;
239242
std::vector<float> vb; // pos(3float), normal(3float), color(3float)
@@ -268,18 +271,18 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n
268271
float n[3][3];
269272

270273
if (attrib.normals.size() > 0) {
271-
int f0 = idx0.vn_idx;
272-
int f1 = idx1.vn_idx;
273-
int f2 = idx2.vn_idx;
274-
275-
if (f0 >= 0 && f1 >= 0 && f2 >= 0) {
276-
assert(3*f0+2 < attrib.normals.size());
277-
assert(3*f1+2 < attrib.normals.size());
278-
assert(3*f2+2 < attrib.normals.size());
274+
int nf0 = idx0.vn_idx;
275+
int nf1 = idx1.vn_idx;
276+
int nf2 = idx2.vn_idx;
277+
278+
if (nf0 >= 0 && nf1 >= 0 && nf2 >= 0) {
279+
assert(3*nf0+2 < attrib.normals.size());
280+
assert(3*nf1+2 < attrib.normals.size());
281+
assert(3*nf2+2 < attrib.normals.size());
279282
for (int k = 0; k < 3; k++) {
280-
n[0][k] = attrib.normals[3*f0+k];
281-
n[1][k] = attrib.normals[3*f1+k];
282-
n[2][k] = attrib.normals[3*f2+k];
283+
n[0][k] = attrib.normals[3*nf0+k];
284+
n[1][k] = attrib.normals[3*nf1+k];
285+
n[2][k] = attrib.normals[3*nf2+k];
283286
}
284287
} else {
285288
// compute geometric normal
@@ -304,7 +307,7 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n
304307
// Use normal as color.
305308
float c[3] = {n[k][0], n[k][1], n[k][2]};
306309
float len2 = c[0] * c[0] + c[1] * c[1] + c[2] * c[2];
307-
if (len2 > 0.0f) {
310+
if (len2 > 1.0e-6f) {
308311
float len = sqrtf(len2);
309312

310313
c[0] /= len;

0 commit comments

Comments
 (0)