Remove redundant & buggy memcpy.

Add verbose option.
This commit is contained in:
Syoyo Fujita
2016-07-15 01:38:39 +09:00
parent d4a7eefc54
commit 7d6318c3ad
2 changed files with 26 additions and 22 deletions

View File

@@ -951,14 +951,12 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
static bool parseLine(Command *command, const char *p, size_t p_len, static bool parseLine(Command *command, const char *p, size_t p_len,
bool triangulate = true) { bool triangulate = true) {
char linebuf[4096]; //char linebuf[4096];
assert(p_len < 4095); //assert(p_len < 4095);
// StackVector<char, 256> linebuf; //memcpy(linebuf, p, p_len);
// linebuf->resize(p_len + 1); //linebuf[p_len] = '\0';
memcpy(&linebuf, p, p_len);
linebuf[p_len] = '\0';
const char *token = linebuf; const char *token = p;
command->type = COMMAND_EMPTY; command->type = COMMAND_EMPTY;
@@ -1078,9 +1076,10 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
// namebuf + strlen(namebuf)); // namebuf + strlen(namebuf));
// command->material_name->push_back('\0'); // command->material_name->push_back('\0');
skip_space(&token); skip_space(&token);
command->material_name = p + (token - linebuf); command->material_name = token; // p + (token - linebuf);
command->material_name_len = command->material_name_len =
length_until_newline(token, p_len - (token - linebuf)) + 1; length_until_newline(token, p_len - (token - p)) + 1;
//length_until_newline(token, p_len - (token - linebuf)) + 1;
command->type = COMMAND_USEMTL; command->type = COMMAND_USEMTL;
return true; return true;
@@ -1092,9 +1091,9 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
token += 7; token += 7;
skip_space(&token); skip_space(&token);
command->mtllib_name = p + (token - linebuf); command->mtllib_name = token; //p + (token - linebuf);
command->mtllib_name_len = command->mtllib_name_len =
length_until_newline(token, p_len - (token - linebuf)) + 1; length_until_newline(token, p_len - (token - p)) + 1;
command->type = COMMAND_MTLLIB; command->type = COMMAND_MTLLIB;
return true; return true;
@@ -1105,9 +1104,9 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
// @todo { multiple group name. } // @todo { multiple group name. }
token += 2; token += 2;
command->group_name = p + (token - linebuf); command->group_name = token;
command->group_name_len = command->group_name_len =
length_until_newline(token, p_len - (token - linebuf)) + 1; length_until_newline(token, p_len - (token - p)) + 1;
command->type = COMMAND_G; command->type = COMMAND_G;
return true; return true;
@@ -1118,9 +1117,9 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
// @todo { multiple object name? } // @todo { multiple object name? }
token += 2; token += 2;
command->object_name = p + (token - linebuf); command->object_name = token;
command->object_name_len = command->object_name_len =
length_until_newline(token, p_len - (token - linebuf)) + 1; length_until_newline(token, p_len - (token - p)) + 1;
command->type = COMMAND_O; command->type = COMMAND_O;
return true; return true;

View File

@@ -214,9 +214,8 @@ const char* get_file_data(size_t *len, const char* filename)
} }
bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int num_threads) bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int num_threads, bool verbose)
{ {
#if 1
tinyobj_opt::attrib_t attrib; tinyobj_opt::attrib_t attrib;
std::vector<tinyobj_opt::shape_t> shapes; std::vector<tinyobj_opt::shape_t> shapes;
@@ -229,6 +228,7 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n
printf("filesize: %d\n", (int)data_len); printf("filesize: %d\n", (int)data_len);
tinyobj_opt::LoadOption option; tinyobj_opt::LoadOption option;
option.req_num_threads = num_threads; option.req_num_threads = num_threads;
option.verbose = verbose;
bool ret = parseObj(&attrib, &shapes, data, data_len, option); bool ret = parseObj(&attrib, &shapes, data, data_len, option);
bmin[0] = bmin[1] = bmin[2] = std::numeric_limits<float>::max(); bmin[0] = bmin[1] = bmin[2] = std::numeric_limits<float>::max();
@@ -330,9 +330,6 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n
printf("bmax = %f, %f, %f\n", bmax[0], bmax[1], bmax[2]); printf("bmax = %f, %f, %f\n", bmax[0], bmax[1], bmax[2]);
return true; return true;
#else
return false;
#endif
} }
void reshapeFunc(GLFWwindow* window, int w, int h) void reshapeFunc(GLFWwindow* window, int w, int h)
@@ -499,12 +496,14 @@ static void Init() {
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc < 2) { if (argc < 2) {
std::cout << "Needs input.obj\n" << std::endl; std::cout << "view input.obj <num_threads> <benchark_only> <verbose>" << std::endl;
return 0; return 0;
} }
bool benchmark_only = false; bool benchmark_only = false;
int num_threads = -1; int num_threads = -1;
bool verbose = false;
if (argc > 2) { if (argc > 2) {
num_threads = atoi(argv[2]); num_threads = atoi(argv[2]);
} }
@@ -513,6 +512,10 @@ int main(int argc, char **argv)
benchmark_only = true; benchmark_only = true;
} }
if (argc > 4) {
verbose = true;
}
if (benchmark_only) { if (benchmark_only) {
tinyobj_opt::attrib_t attrib; tinyobj_opt::attrib_t attrib;
@@ -527,6 +530,8 @@ int main(int argc, char **argv)
printf("filesize: %d\n", (int)data_len); printf("filesize: %d\n", (int)data_len);
tinyobj_opt::LoadOption option; tinyobj_opt::LoadOption option;
option.req_num_threads = num_threads; option.req_num_threads = num_threads;
option.verbose = true;
bool ret = parseObj(&attrib, &shapes, data, data_len, option); bool ret = parseObj(&attrib, &shapes, data, data_len, option);
return ret; return ret;
@@ -569,7 +574,7 @@ int main(int argc, char **argv)
reshapeFunc(window, width, height); reshapeFunc(window, width, height);
float bmin[3], bmax[3]; float bmin[3], bmax[3];
if (false == LoadObjAndConvert(bmin, bmax, argv[1], num_threads)) { if (false == LoadObjAndConvert(bmin, bmax, argv[1], num_threads, verbose)) {
printf("failed to load & conv\n"); printf("failed to load & conv\n");
return -1; return -1;
} }