Fix parser.

This commit is contained in:
Syoyo Fujita
2016-05-15 18:49:06 +09:00
parent 88fe2421d9
commit d392282f02
2 changed files with 43 additions and 12 deletions

View File

@@ -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. // NOTE: Don't use powf here, it will absolutely murder precision.
// pow(10.0, -read) // pow(10.0, -read)
double frac_value = 10.0; double frac_value = 1.0;
for (int f = 0; f < read; f++) { for (int f = 0; f < read; f++) {
frac_value *= 0.1; frac_value *= 0.1;
} }
@@ -762,7 +762,7 @@ static bool parseLine(Command *command, const char *p, size_t p_len)
// group name // group name
if (token[0] == 'g' && IS_SPACE((token[1]))) { if (token[0] == 'g' && IS_SPACE((token[1]))) {
std::vector<ShortString> names; ShortString names[16];
int num_names = 0; 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<float> &vertices, std::vector<float> &normals, std::vector<float> &texcoords, std::vector<vertex_index> &faces, const char* buf, size_t len, int req_num_threads) void parse(std::vector<float> &vertices, std::vector<float> &normals, std::vector<float> &texcoords, std::vector<vertex_index> &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<char> newline_marker(len, 0); std::vector<char> newline_marker(len, 0);
auto num_threads = (req_num_threads < 0) ? std::thread::hardware_concurrency() : req_num_threads; auto num_threads = (req_num_threads < 0) ? std::thread::hardware_concurrency() : req_num_threads;
@@ -1047,11 +1054,29 @@ void parse(std::vector<float> &vertices, std::vector<float> &normals, std::vecto
int v_size = vertices.size() / 3; int v_size = vertices.size() / 3;
int vn_size = normals.size() / 3; int vn_size = normals.size() / 3;
int vt_size = texcoords.size() / 2; 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); // triangulate.
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); vertex_index i0 = commands[t][i].f[0];
faces.emplace_back(std::move(vertex_index(v_idx, vn_idx, vt_idx))); 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));
}
} }
} }
} }

View File

@@ -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) const char *mmap_file(size_t *len, const char* filename)
{ {
(*len) = 0;
#ifdef _WIN64 #ifdef _WIN64
HANDLE file = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); 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); assert(file != INVALID_HANDLE_VALUE);
@@ -127,9 +128,10 @@ const char *mmap_file(size_t *len, const char* filename)
return NULL; return NULL;
} }
return p;
(*len) = fileSize; (*len) = fileSize;
return p;
#endif #endif
} }
@@ -148,7 +150,8 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename)
exit(-1); exit(-1);
return false; 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<float>::max(); bmin[0] = bmin[1] = bmin[2] = std::numeric_limits<float>::max();
bmax[0] = bmax[1] = bmax[2] = -std::numeric_limits<float>::max(); bmax[0] = bmax[1] = bmax[2] = -std::numeric_limits<float>::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 reshapeFunc(GLFWwindow* window, int w, int h)
{ {
(void)window; (void)window;
printf("reshape\n"); // for retinal display.
glViewport(0, 0, w, h); int fb_w, fb_h;
glfwGetFramebufferSize(window, &fb_w, &fb_h);
glViewport(0, 0, fb_w, fb_h);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
gluPerspective(45.0, (float)w / (float)h, 0.01f, 100.0f); gluPerspective(45.0, (float)w / (float)h, 0.01f, 100.0f);