From d392282f02c001cfab961d9d916ca464679d42cf Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sun, 15 May 2016 18:49:06 +0900 Subject: [PATCH] Fix parser. --- experimental/optimized-parse.cc | 39 +++++++++++++++++++++++++++------ experimental/viewer.cc | 16 +++++++++----- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/experimental/optimized-parse.cc b/experimental/optimized-parse.cc index c27ceae..6894184 100644 --- a/experimental/optimized-parse.cc +++ b/experimental/optimized-parse.cc @@ -495,7 +495,7 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) { // NOTE: Don't use powf here, it will absolutely murder precision. // pow(10.0, -read) - double frac_value = 10.0; + double frac_value = 1.0; for (int f = 0; f < read; f++) { frac_value *= 0.1; } @@ -762,7 +762,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len) // group name if (token[0] == 'g' && IS_SPACE((token[1]))) { - std::vector names; + ShortString names[16]; int num_names = 0; @@ -857,6 +857,13 @@ static inline bool is_line_ending(const char* p, size_t i, size_t end_i) void parse(std::vector &vertices, std::vector &normals, std::vector &texcoords, std::vector &faces, const char* buf, size_t len, int req_num_threads) { + vertices.clear(); + normals.clear(); + texcoords.clear(); + faces.clear(); + + if (len < 1) return; + std::vector newline_marker(len, 0); auto num_threads = (req_num_threads < 0) ? std::thread::hardware_concurrency() : req_num_threads; @@ -1047,11 +1054,29 @@ void parse(std::vector &vertices, std::vector &normals, std::vecto int v_size = vertices.size() / 3; int vn_size = normals.size() / 3; int vt_size = texcoords.size() / 2; - for (size_t k = 0; k < commands[t][i].f.size(); k++) { - int v_idx = fixIndex(commands[t][i].f[k].v_idx, v_size); - int vn_idx = fixIndex(commands[t][i].f[k].vn_idx, vn_size); - int vt_idx = fixIndex(commands[t][i].f[k].vt_idx, vt_size); - faces.emplace_back(std::move(vertex_index(v_idx, vn_idx, vt_idx))); + + // triangulate. + { + vertex_index i0 = commands[t][i].f[0]; + vertex_index i1(-1); + vertex_index i2 = commands[t][i].f[1]; + + for (size_t k = 2; k < commands[t][i].f.size(); k++) { + i1 = i2; + i2 = commands[t][i].f[k]; + int v_idx = fixIndex(i0.v_idx, v_size); + int vn_idx = fixIndex(i0.vn_idx, vn_size); + int vt_idx = fixIndex(i0.vt_idx, vt_size); + faces.emplace_back(vertex_index(v_idx, vn_idx, vt_idx)); + v_idx = fixIndex(i1.v_idx, v_size); + vn_idx = fixIndex(i1.vn_idx, vn_size); + vt_idx = fixIndex(i1.vt_idx, vt_size); + faces.emplace_back(vertex_index(v_idx, vn_idx, vt_idx)); + v_idx = fixIndex(i2.v_idx, v_size); + vn_idx = fixIndex(i2.vn_idx, vn_size); + vt_idx = fixIndex(i2.vt_idx, vt_size); + faces.emplace_back(vertex_index(v_idx, vn_idx, vt_idx)); + } } } } diff --git a/experimental/viewer.cc b/experimental/viewer.cc index 0d58d0b..2691fcb 100644 --- a/experimental/viewer.cc +++ b/experimental/viewer.cc @@ -78,6 +78,7 @@ void CalcNormal(float N[3], float v0[3], float v1[3], float v2[3]) { const char *mmap_file(size_t *len, const char* filename) { + (*len) = 0; #ifdef _WIN64 HANDLE file = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); assert(file != INVALID_HANDLE_VALUE); @@ -127,9 +128,10 @@ const char *mmap_file(size_t *len, const char* filename) return NULL; } - return p; - (*len) = fileSize; + + return p; + #endif } @@ -148,7 +150,8 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename) exit(-1); return false; } - parse(vertices, normals, texcoords, faces, data, data_len, 1); + printf("filesize: %d\n", (int)data_len); + parse(vertices, normals, texcoords, faces, data, data_len, /* num_threads */-1); bmin[0] = bmin[1] = bmin[2] = std::numeric_limits::max(); bmax[0] = bmax[1] = bmax[2] = -std::numeric_limits::max(); @@ -248,8 +251,11 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename) void reshapeFunc(GLFWwindow* window, int w, int h) { (void)window; - printf("reshape\n"); - glViewport(0, 0, w, h); + // for retinal display. + int fb_w, fb_h; + glfwGetFramebufferSize(window, &fb_w, &fb_h); + + glViewport(0, 0, fb_w, fb_h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (float)w / (float)h, 0.01f, 100.0f);