Skip to content

Commit ee7d6cc

Browse files
committed
Refactor and re-design tinyobjloader.
* Separete attribs(vtx,normal,texcoords) and shape. * Support different index for vtx/normal/texcoord.
1 parent 9c81fcb commit ee7d6cc

File tree

10 files changed

+171
-146
lines changed

10 files changed

+171
-146
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Tiny but powerful single file wavefront obj loader written in C++. No dependency
2121
What's new
2222
----------
2323

24+
* XX YY, ZZZZ : New data strcutre and API!
2425
* Jan 29, 2016 : Support n-polygon(no triangulation) and OpenSubdiv crease tag! Thanks dboogert!
2526
* Nov 26, 2015 : Now single-header only!.
2627
* Nov 08, 2015 : Improved API.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test.cc

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,35 @@
88
#include <sstream>
99
#include <fstream>
1010

11-
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool triangulate = true)
11+
static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool triangulate = true)
1212
{
13+
std::cout << "# of positions : " << (attrib.positions.size() / 3) << std::endl;
14+
std::cout << "# of normals : " << (attrib.normals.size() / 3) << std::endl;
15+
std::cout << "# of texcoords : " << (attrib.texcoords.size() / 2) << std::endl;
16+
1317
std::cout << "# of shapes : " << shapes.size() << std::endl;
1418
std::cout << "# of materials : " << materials.size() << std::endl;
1519

20+
for (size_t v = 0; v < attrib.positions.size() / 3; v++) {
21+
printf(" v[%ld] = (%f, %f, %f)\n", v,
22+
static_cast<const double>(attrib.positions[3*v+0]),
23+
static_cast<const double>(attrib.positions[3*v+1]),
24+
static_cast<const double>(attrib.positions[3*v+2]));
25+
}
26+
27+
for (size_t v = 0; v < attrib.normals.size() / 3; v++) {
28+
printf(" n[%ld] = (%f, %f, %f)\n", v,
29+
static_cast<const double>(attrib.normals[3*v+0]),
30+
static_cast<const double>(attrib.normals[3*v+1]),
31+
static_cast<const double>(attrib.normals[3*v+2]));
32+
}
33+
34+
for (size_t v = 0; v < attrib.texcoords.size() / 2; v++) {
35+
printf(" uv[%ld] = (%f, %f)\n", v,
36+
static_cast<const double>(attrib.texcoords[2*v+0]),
37+
static_cast<const double>(attrib.texcoords[2*v+1]));
38+
}
39+
1640
for (size_t i = 0; i < shapes.size(); i++) {
1741
printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str());
1842
printf("Size of shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size());
@@ -22,11 +46,19 @@ static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::ve
2246
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size());
2347
assert((shapes[i].mesh.indices.size() % 3) == 0);
2448
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
25-
printf(" idx[%ld] = %d, %d, %d. mat_id = %d\n", f, shapes[i].mesh.indices[3*f+0], shapes[i].mesh.indices[3*f+1], shapes[i].mesh.indices[3*f+2], shapes[i].mesh.material_ids[f]);
49+
tinyobj::index_t i0 = shapes[i].mesh.indices[3*f+0];
50+
tinyobj::index_t i1 = shapes[i].mesh.indices[3*f+1];
51+
tinyobj::index_t i2 = shapes[i].mesh.indices[3*f+2];
52+
printf(" idx[%ld] = %d/%d/%d, %d/%d/%d, %d/%d/%d. mat_id = %d\n", f,
53+
i0.vertex_index, i0.normal_index, i0.texcoord_index,
54+
i1.vertex_index, i1.normal_index, i1.texcoord_index,
55+
i2.vertex_index, i2.normal_index, i2.texcoord_index,
56+
shapes[i].mesh.material_ids[f]);
2657
}
2758
} else {
2859
for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++) {
29-
printf(" idx[%ld] = %d\n", f, shapes[i].mesh.indices[f]);
60+
tinyobj::index_t idx = shapes[i].mesh.indices[f];
61+
printf(" idx[%ld] = %d/%d/%d\n", f, idx.vertex_index, idx.normal_index, idx.texcoord_index);
3062
}
3163

3264
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size());
@@ -44,14 +76,14 @@ static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::ve
4476
static_cast<long>(shapes[i].mesh.num_vertices[v]));
4577
}
4678

47-
printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size());
48-
assert((shapes[i].mesh.positions.size() % 3) == 0);
49-
for (size_t v = 0; v < shapes[i].mesh.positions.size() / 3; v++) {
50-
printf(" v[%ld] = (%f, %f, %f)\n", v,
51-
shapes[i].mesh.positions[3*v+0],
52-
shapes[i].mesh.positions[3*v+1],
53-
shapes[i].mesh.positions[3*v+2]);
54-
}
79+
//printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size());
80+
//assert((shapes[i].mesh.positions.size() % 3) == 0);
81+
//for (size_t v = 0; v < shapes[i].mesh.positions.size() / 3; v++) {
82+
// printf(" v[%ld] = (%f, %f, %f)\n", v,
83+
// static_cast<const double>(shapes[i].mesh.positions[3*v+0]),
84+
// static_cast<const double>(shapes[i].mesh.positions[3*v+1]),
85+
// static_cast<const double>(shapes[i].mesh.positions[3*v+2]));
86+
//}
5587

5688
printf("shape[%ld].num_tags: %ld\n", i, shapes[i].mesh.tags.size());
5789
for (size_t t = 0; t < shapes[i].mesh.tags.size(); t++) {
@@ -70,7 +102,7 @@ static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::ve
70102
printf(" floats: [");
71103
for (size_t j = 0; j < shapes[i].mesh.tags[t].floatValues.size(); ++j)
72104
{
73-
printf("%f", shapes[i].mesh.tags[t].floatValues[j]);
105+
printf("%f", static_cast<const double>(shapes[i].mesh.tags[t].floatValues[j]));
74106
if (j < (shapes[i].mesh.tags[t].floatValues.size()-1))
75107
{
76108
printf(", ");
@@ -128,11 +160,12 @@ TestLoadObj(
128160
{
129161
std::cout << "Loading " << filename << std::endl;
130162

163+
tinyobj::attrib_t attrib;
131164
std::vector<tinyobj::shape_t> shapes;
132165
std::vector<tinyobj::material_t> materials;
133166

134167
std::string err;
135-
bool ret = tinyobj::LoadObj(shapes, materials, err, filename, basepath, triangulate);
168+
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, basepath, triangulate);
136169

137170
if (!err.empty()) {
138171
std::cerr << err << std::endl;
@@ -143,7 +176,7 @@ TestLoadObj(
143176
return false;
144177
}
145178

146-
PrintInfo(shapes, materials, triangulate);
179+
PrintInfo(attrib, shapes, materials, triangulate);
147180

148181
return true;
149182
}
@@ -223,13 +256,13 @@ std::string matStream(
223256
virtual ~MaterialStringStreamReader() {}
224257
virtual bool operator() (
225258
const std::string& matId,
226-
std::vector<material_t>& materials,
227-
std::map<std::string, int>& matMap,
228-
std::string& err)
259+
std::vector<material_t>* materials,
260+
std::map<std::string, int>* matMap,
261+
std::string* err)
229262
{
230263
(void)matId;
231264
(void)err;
232-
LoadMtl(matMap, materials, m_matSStream);
265+
LoadMtl(matMap, materials, &m_matSStream);
233266
return true;
234267
}
235268

@@ -238,10 +271,11 @@ std::string matStream(
238271
};
239272

240273
MaterialStringStreamReader matSSReader(matStream);
274+
tinyobj::attrib_t attrib;
241275
std::vector<tinyobj::shape_t> shapes;
242276
std::vector<tinyobj::material_t> materials;
243277
std::string err;
244-
bool ret = tinyobj::LoadObj(shapes, materials, err, objStream, matSSReader);
278+
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, &objStream, &matSSReader);
245279

246280
if (!err.empty()) {
247281
std::cerr << err << std::endl;
@@ -251,7 +285,7 @@ std::string matStream(
251285
return false;
252286
}
253287

254-
PrintInfo(shapes, materials);
288+
PrintInfo(attrib, shapes, materials);
255289

256290
return true;
257291
}

0 commit comments

Comments
 (0)