From 6556411006b25a2a8193d5eeaf61b533bd32b285 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Thu, 25 Apr 2013 17:01:52 +0900 Subject: [PATCH] Add support of group tag('g') --- cube.mtl | 24 ++++++++++++++++++++++++ cube.obj | 31 +++++++++++++++++++++++++++++++ test.cc | 33 ++++++++++++++++++++++----------- tiny_obj_loader.cc | 26 +++++++++++++++++++++++++- 4 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 cube.mtl create mode 100644 cube.obj diff --git a/cube.mtl b/cube.mtl new file mode 100644 index 0000000..d3a1c7a --- /dev/null +++ b/cube.mtl @@ -0,0 +1,24 @@ +newmtl white +Ka 0 0 0 +Kd 1 1 1 +Ks 0 0 0 + +newmtl red +Ka 0 0 0 +Kd 1 0 0 +Ks 0 0 0 + +newmtl green +Ka 0 0 0 +Kd 0 1 0 +Ks 0 0 0 + +newmtl blue +Ka 0 0 0 +Kd 0 0 1 +Ks 0 0 0 + +newmtl light +Ka 20 20 20 +Kd 1 1 1 +Ks 0 0 0 diff --git a/cube.obj b/cube.obj new file mode 100644 index 0000000..9213e17 --- /dev/null +++ b/cube.obj @@ -0,0 +1,31 @@ +mtllib cube.mtl + +v 0.000000 2.000000 2.000000 +v 0.000000 0.000000 2.000000 +v 2.000000 0.000000 2.000000 +v 2.000000 2.000000 2.000000 +v 0.000000 2.000000 0.000000 +v 0.000000 0.000000 0.000000 +v 2.000000 0.000000 0.000000 +v 2.000000 2.000000 0.000000 +# 8 vertices + +g front cube +usemtl white +f 1 2 3 4 +g back cube +# expects white material +f 8 7 6 5 +g right cube +usemtl red +f 4 3 7 8 +g top cube +usemtl white +f 5 1 4 8 +g left cube +usemtl green +f 5 6 2 1 +g bottom cube +usemtl white +f 2 6 7 3 +# 6 elements diff --git a/test.cc b/test.cc index 8916696..4de4cc2 100644 --- a/test.cc +++ b/test.cc @@ -5,22 +5,17 @@ #include #include -int -main( - int argc, - char **argv) +static bool +TestLoadObj(const char* filename) { - std::string inputfile = "cornell_box.obj"; - std::vector shapes; + std::cout << "Loading " << filename << std::endl; - if (argc > 1) { - inputfile = std::string(argv[1]); - } - - std::string err = tinyobj::LoadObj(shapes, inputfile.c_str()); + std::vector shapes; + std::string err = tinyobj::LoadObj(shapes, filename); if (!err.empty()) { std::cerr << err << std::endl; + return false; } std::cout << "# of shapes : " << shapes.size() << std::endl; @@ -61,5 +56,21 @@ main( printf("\n"); } + return true; +} + +int +main( + int argc, + char **argv) +{ + + if (argc > 1) { + assert(true == TestLoadObj(argv[1])); + } else { + assert(true == TestLoadObj("cornell_box.obj")); + assert(true == TestLoadObj("cube.obj")); + } + return 0; } diff --git a/tiny_obj_loader.cc b/tiny_obj_loader.cc index 8dccecf..7988fab 100644 --- a/tiny_obj_loader.cc +++ b/tiny_obj_loader.cc @@ -1,10 +1,11 @@ // -// Copyright 2012, Syoyo Fujita. +// Copyright 2012-2013, Syoyo Fujita. // // Licensed under 2-clause BSD liecense. // // +// version 0.9.4: Initial suupport of group tag(g) // version 0.9.3: Fix parsing triple 'x/y/z' // version 0.9.2: Add more .mtl load support // version 0.9.1: Add initial .mtl load support @@ -560,6 +561,29 @@ LoadObj( continue; } + // group name + if (token[0] == 'g' && isSpace((token[1]))) { + + printf("group\n"); + + // flush previous face group. + shape_t shape; + bool ret = exportFaceGroupToShape(shape, v, vn, vt, faceGroup, material, name); + if (ret) { + shapes.push_back(shape); + } + + faceGroup.clear(); + + // @todo { multiple group name. } + char namebuf[4096]; + token += 2; + sscanf(token, "%s", namebuf); + name = std::string(namebuf); + + continue; + } + // object name if (token[0] == 'o' && isSpace((token[1]))) {