From 8d60b4963ae7a7cccd41d2bd22f2b2e17f1e0e9d Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sun, 1 May 2016 21:30:50 +0900 Subject: [PATCH] Rename varialble for better understanding. Write some API usage in README.md. --- README.md | 36 +++++++++++++++++++++++++++++++++--- loader_example.cc | 10 +++++----- tests/tester.cc | 8 ++++---- tiny_obj_loader.h | 6 +++--- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f24bc8d..74bc8f8 100644 --- a/README.md +++ b/README.md @@ -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(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(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(shapes[i].mesh.num_vertices[v])); + static_cast(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 indices; std::vector - 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 material_ids; // per-face material ID std::vector 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(npolys)); + shape->mesh.num_face_vertices.push_back(static_cast(npolys)); shape->mesh.material_ids.push_back(material_id); // per face } }