|
3 | 3 | #include <cstdio>
|
4 | 4 | #include <cstdlib>
|
5 | 5 | #include <cassert>
|
| 6 | +#include <cstddef> |
6 | 7 | #include <iostream>
|
| 8 | +#include <sstream> |
| 9 | +#include <fstream> |
| 10 | +#include <functional> |
| 11 | +#include <memory> |
7 | 12 |
|
8 |
| -static bool |
9 |
| -TestLoadObj( |
10 |
| - const char* filename, |
11 |
| - const char* basepath = NULL) |
| 13 | +static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes) |
12 | 14 | {
|
13 |
| - std::cout << "Loading " << filename << std::endl; |
14 |
| - |
15 |
| - std::vector<tinyobj::shape_t> shapes; |
16 |
| - std::string err = tinyobj::LoadObj(shapes, filename, basepath); |
17 |
| - |
18 |
| - if (!err.empty()) { |
19 |
| - std::cerr << err << std::endl; |
20 |
| - return false; |
21 |
| - } |
22 |
| - |
23 | 15 | std::cout << "# of shapes : " << shapes.size() << std::endl;
|
24 | 16 |
|
25 | 17 | for (size_t i = 0; i < shapes.size(); i++) {
|
@@ -53,14 +45,124 @@ TestLoadObj(
|
53 | 45 | printf(" material.map_Kd = %s\n", shapes[i].material.diffuse_texname.c_str());
|
54 | 46 | printf(" material.map_Ks = %s\n", shapes[i].material.specular_texname.c_str());
|
55 | 47 | printf(" material.map_Ns = %s\n", shapes[i].material.normal_texname.c_str());
|
56 |
| - std::map<std::string, std::string>::iterator it(shapes[i].material.unknown_parameter.begin()); |
57 |
| - std::map<std::string, std::string>::iterator itEnd(shapes[i].material.unknown_parameter.end()); |
| 48 | + auto it = shapes[i].material.unknown_parameter.begin(); |
| 49 | + auto itEnd = shapes[i].material.unknown_parameter.end(); |
58 | 50 | for (; it != itEnd; it++) {
|
59 | 51 | printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str());
|
60 | 52 | }
|
61 | 53 | printf("\n");
|
62 | 54 | }
|
| 55 | +} |
| 56 | + |
| 57 | +static bool |
| 58 | +TestLoadObj( |
| 59 | + const char* filename, |
| 60 | + const char* basepath = NULL) |
| 61 | +{ |
| 62 | + std::cout << "Loading " << filename << std::endl; |
| 63 | + |
| 64 | + std::vector<tinyobj::shape_t> shapes; |
| 65 | + std::string err = tinyobj::LoadObj(shapes, filename, basepath); |
63 | 66 |
|
| 67 | + if (!err.empty()) { |
| 68 | + std::cerr << err << std::endl; |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + PrintInfo(shapes); |
| 73 | + |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +static bool |
| 79 | +TestStreamLoadObj() |
| 80 | +{ |
| 81 | + std::cout << "Stream Loading " << std::endl; |
| 82 | + |
| 83 | + std::stringstream objStream; |
| 84 | + objStream |
| 85 | + << "mtllib cube.mtl\n" |
| 86 | + "\n" |
| 87 | + "v 0.000000 2.000000 2.000000\n" |
| 88 | + "v 0.000000 0.000000 2.000000\n" |
| 89 | + "v 2.000000 0.000000 2.000000\n" |
| 90 | + "v 2.000000 2.000000 2.000000\n" |
| 91 | + "v 0.000000 2.000000 0.000000\n" |
| 92 | + "v 0.000000 0.000000 0.000000\n" |
| 93 | + "v 2.000000 0.000000 0.000000\n" |
| 94 | + "v 2.000000 2.000000 0.000000\n" |
| 95 | + "# 8 vertices\n" |
| 96 | + "\n" |
| 97 | + "g front cube\n" |
| 98 | + "usemtl white\n" |
| 99 | + "f 1 2 3 4\n" |
| 100 | + "g back cube\n" |
| 101 | + "# expects white material\n" |
| 102 | + "f 8 7 6 5\n" |
| 103 | + "g right cube\n" |
| 104 | + "usemtl red\n" |
| 105 | + "f 4 3 7 8\n" |
| 106 | + "g top cube\n" |
| 107 | + "usemtl white\n" |
| 108 | + "f 5 1 4 8\n" |
| 109 | + "g left cube\n" |
| 110 | + "usemtl green\n" |
| 111 | + "f 5 6 2 1\n" |
| 112 | + "g bottom cube\n" |
| 113 | + "usemtl white\n" |
| 114 | + "f 2 6 7 3\n" |
| 115 | + "# 6 elements"; |
| 116 | + |
| 117 | + auto getMatFileIStreamFunc = |
| 118 | + [](const std::string& matId) |
| 119 | + { |
| 120 | + if (matId == "cube.mtl") { |
| 121 | + |
| 122 | + std::unique_ptr<std::stringstream> matStream( |
| 123 | + new std::stringstream( |
| 124 | + "newmtl white\n" |
| 125 | + "Ka 0 0 0\n" |
| 126 | + "Kd 1 1 1\n" |
| 127 | + "Ks 0 0 0\n" |
| 128 | + "\n" |
| 129 | + "newmtl red\n" |
| 130 | + "Ka 0 0 0\n" |
| 131 | + "Kd 1 0 0\n" |
| 132 | + "Ks 0 0 0\n" |
| 133 | + "\n" |
| 134 | + "newmtl green\n" |
| 135 | + "Ka 0 0 0\n" |
| 136 | + "Kd 0 1 0\n" |
| 137 | + "Ks 0 0 0\n" |
| 138 | + "\n" |
| 139 | + "newmtl blue\n" |
| 140 | + "Ka 0 0 0\n" |
| 141 | + "Kd 0 0 1\n" |
| 142 | + "Ks 0 0 0\n" |
| 143 | + "\n" |
| 144 | + "newmtl light\n" |
| 145 | + "Ka 20 20 20\n" |
| 146 | + "Kd 1 1 1\n" |
| 147 | + "Ks 0 0 0")); |
| 148 | + |
| 149 | + return matStream; |
| 150 | + } |
| 151 | + |
| 152 | + std::unique_ptr<std::stringstream> emptyUP( nullptr ); |
| 153 | + return emptyUP; |
| 154 | + }; |
| 155 | + |
| 156 | + std::vector<tinyobj::shape_t> shapes; |
| 157 | + std::string err = tinyobj::LoadObj(shapes, objStream, getMatFileIStreamFunc); |
| 158 | + |
| 159 | + if (!err.empty()) { |
| 160 | + std::cerr << err << std::endl; |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + PrintInfo(shapes); |
| 165 | + |
64 | 166 | return true;
|
65 | 167 | }
|
66 | 168 |
|
|
79 | 181 | } else {
|
80 | 182 | assert(true == TestLoadObj("cornell_box.obj"));
|
81 | 183 | assert(true == TestLoadObj("cube.obj"));
|
| 184 | + assert(true == TestStreamLoadObj()); |
82 | 185 | }
|
83 |
| - |
| 186 | + |
84 | 187 | return 0;
|
85 | 188 | }
|
0 commit comments