diff options
author | Syoyo Fujita <syoyo@lighttransport.com> | 2016-05-01 21:30:50 +0900 |
---|---|---|
committer | Syoyo Fujita <syoyo@lighttransport.com> | 2016-05-01 21:30:50 +0900 |
commit | 8d60b4963ae7a7cccd41d2bd22f2b2e17f1e0e9d (patch) | |
tree | b614e2621a041cd15a3cceeff89d762f6242377e | |
parent | 9d6b58b90e879a87e4455f79ef9ae585f9bfed10 (diff) | |
download | tinyobjloader-8d60b4963ae7a7cccd41d2bd22f2b2e17f1e0e9d.tar.gz |
Rename varialble for better understanding.
Write some API usage in README.md.
-rw-r--r-- | README.md | 36 | ||||
-rw-r--r-- | loader_example.cc | 10 | ||||
-rw-r--r-- | tests/tester.cc | 8 | ||||
-rw-r--r-- | tiny_obj_loader.h | 6 |
4 files changed, 45 insertions, 15 deletions
@@ -79,9 +79,9 @@ Features TODO ---- -* [ ] Read .obj/.mtl from memory. * [ ] Fix Python binding. -* [ ] Unit test codes. +* [ ] Fix obj_sticker example. +* [ ] More unit test codes. License ------- @@ -91,6 +91,10 @@ Licensed under MIT license. Usage ----- +`attrib_t` contains single and linear array of vertex data(position, normal and texcoord). +Each `shape_t` does not contain vertex data but contains array index to `attrib_t`. +See `loader_example.cc` for more details. + ```c++ #define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc #include "tiny_obj_loader.h" @@ -111,7 +115,33 @@ if (!ret) { exit(1); } -// See loader_example.cc for more details. +// Loop over shapes +for (size_t s = 0; s < shapes.size(); s++) { + // Loop over faces(polygon) + size_t index_offset = 0; + for (size_t f = 0; f < shapes[i].mesh.num_face_vertices; f++) { + int fv = shapes[i].mesh.num_face_vertices[f]; + + // Loop over vertices in the face. + for (size_t v = 0; v < fv; f++) { + // access to vertex + tinyobj::index_t idx = shapes[i].mesh.indices[index_offset + v]; + float vx = attrib.positions[3*idx.vertex_index+0]; + float vy = attrib.positions[3*idx.vertex_index+1]; + float vz = attrib.positions[3*idx.vertex_index+2]; + float nx = attrib.normals[3*idx.normal_index+0]; + float ny = attrib.normals[3*idx.normal_index+1]; + float nz = attrib.normals[3*idx.normal_index+2]; + float tx = attrib.texcoords[2*idx.texcoord_index+0]; + float ty = attrib.texcoords[2*idx.texcoord_index+1]; + } + index_offset += fv; + + // per-face material + shapes[i].mesh.material_ids[f]; + } +} + ``` diff --git a/loader_example.cc b/loader_example.cc index 435684e..e592e95 100644 --- a/loader_example.cc +++ b/loader_example.cc @@ -65,7 +65,7 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj } printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size()); - assert(shapes[i].mesh.material_ids.size() == shapes[i].mesh.num_vertices.size()); + assert(shapes[i].mesh.material_ids.size() == shapes[i].mesh.num_face_vertices.size()); for (size_t m = 0; m < shapes[i].mesh.material_ids.size(); m++) { printf(" material_id[%ld] = %d\n", m, shapes[i].mesh.material_ids[m]); @@ -73,10 +73,10 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj } - printf("shape[%ld].num_faces: %ld\n", i, shapes[i].mesh.num_vertices.size()); - for (size_t v = 0; v < shapes[i].mesh.num_vertices.size(); v++) { - printf(" num_vertices[%ld] = %ld\n", v, - static_cast<long>(shapes[i].mesh.num_vertices[v])); + printf("shape[%ld].num_faces: %ld\n", i, shapes[i].mesh.num_face_vertices.size()); + for (size_t v = 0; v < shapes[i].mesh.num_face_vertices.size(); v++) { + printf(" num_face_vertices[%ld] = %ld\n", v, + static_cast<long>(shapes[i].mesh.num_face_vertices[v])); } //printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size()); diff --git a/tests/tester.cc b/tests/tester.cc index f16eeb0..e8e3b42 100644 --- a/tests/tester.cc +++ b/tests/tester.cc @@ -65,7 +65,7 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj } printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size()); - assert(shapes[i].mesh.material_ids.size() == shapes[i].mesh.num_vertices.size()); + assert(shapes[i].mesh.material_ids.size() == shapes[i].mesh.num_face_vertices.size()); for (size_t m = 0; m < shapes[i].mesh.material_ids.size(); m++) { printf(" material_id[%ld] = %d\n", m, shapes[i].mesh.material_ids[m]); @@ -73,10 +73,10 @@ static void PrintInfo(const tinyobj::attrib_t &attrib, const std::vector<tinyobj } - printf("shape[%ld].num_faces: %ld\n", i, shapes[i].mesh.num_vertices.size()); - for (size_t v = 0; v < shapes[i].mesh.num_vertices.size(); v++) { + printf("shape[%ld].num_faces: %ld\n", i, shapes[i].mesh.num_face_vertices.size()); + for (size_t v = 0; v < shapes[i].mesh.num_face_vertices.size(); v++) { printf(" num_vertices[%ld] = %ld\n", v, - static_cast<long>(shapes[i].mesh.num_vertices[v])); + static_cast<long>(shapes[i].mesh.num_face_vertices[v])); } //printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size()); diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 8500fc9..01ff721 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -113,7 +113,7 @@ typedef struct { typedef struct { std::vector<index_t> indices; std::vector<unsigned char> - num_vertices; // The number of vertices per face. Up to 255. + num_face_vertices; // The number of vertices per face. 3 = polygon, 4 = quad, ... Up to 255. std::vector<int> material_ids; // per-face material ID std::vector<tag_t> tags; // SubD tag } mesh_t; @@ -604,7 +604,7 @@ static bool exportFaceGroupToShape( shape->mesh.indices.push_back(idx1); shape->mesh.indices.push_back(idx2); - shape->mesh.num_vertices.push_back(3); + shape->mesh.num_face_vertices.push_back(3); shape->mesh.material_ids.push_back(material_id); } } else { @@ -615,7 +615,7 @@ static bool exportFaceGroupToShape( idx.texcoord_index = face[k].vt_idx; } - shape->mesh.num_vertices.push_back(static_cast<unsigned char>(npolys)); + shape->mesh.num_face_vertices.push_back(static_cast<unsigned char>(npolys)); shape->mesh.material_ids.push_back(material_id); // per face } } |