aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyoyo Fujita <syoyo@lighttransport.com>2021-03-19 14:38:58 +0900
committerGitHub <noreply@github.com>2021-03-19 14:38:58 +0900
commitf760a8a0356a4372a27ac81f28f6afed78b5e63d (patch)
treeab576fcd636f551c7e15727fd2902db01c958736
parent7ba4b652ee0c5175ec8abf66199e84d88adf11f1 (diff)
downloadtinyobjloader-f760a8a0356a4372a27ac81f28f6afed78b5e63d.tar.gz
Update example code in README. (#301)
-rw-r--r--README.md66
1 files changed, 42 insertions, 24 deletions
diff --git a/README.md b/README.md
index 1fda8ae..ce5ab1b 100644
--- a/README.md
+++ b/README.md
@@ -274,24 +274,33 @@ 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[s].mesh.num_face_vertices.size(); f++) {
- int fv = shapes[s].mesh.num_face_vertices[f];
+ size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]);
// Loop over vertices in the face.
for (size_t v = 0; v < fv; v++) {
// access to vertex
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
- tinyobj::real_t vx = attrib.vertices[3*idx.vertex_index+0];
- tinyobj::real_t vy = attrib.vertices[3*idx.vertex_index+1];
- tinyobj::real_t vz = attrib.vertices[3*idx.vertex_index+2];
- tinyobj::real_t nx = attrib.normals[3*idx.normal_index+0];
- tinyobj::real_t ny = attrib.normals[3*idx.normal_index+1];
- tinyobj::real_t nz = attrib.normals[3*idx.normal_index+2];
- tinyobj::real_t tx = attrib.texcoords[2*idx.texcoord_index+0];
- tinyobj::real_t ty = attrib.texcoords[2*idx.texcoord_index+1];
+
+ tinyobj::real_t vx = attrib.vertices[3*size_t(idx.vertex_index)+0];
+ tinyobj::real_t vy = attrib.vertices[3*size_t(idx.vertex_index)+1];
+ tinyobj::real_t vz = attrib.vertices[3*size_t(idx.vertex_index)+2];
+
+ // Check if `normal_index` is zero or positive. negative = no normal data
+ if (idx.normal_index >= 0) {
+ tinyobj::real_t nx = attrib.normals[3*size_t(idx.normal_index)+0];
+ tinyobj::real_t ny = attrib.normals[3*size_t(idx.normal_index)+1];
+ tinyobj::real_t nz = attrib.normals[3*size_t(idx.normal_index)+2];
+ }
+
+ // Check if `texcoord_index` is zero or positive. negative = no texcoord data
+ if (idx.texcoord_index >= 0) {
+ tinyobj::real_t tx = attrib.texcoords[2*size_t(idx.texcoord_index)+0];
+ tinyobj::real_t ty = attrib.texcoords[2*size_t(idx.texcoord_index)+1];
+ }
// Optional: vertex colors
- // tinyobj::real_t red = attrib.colors[3*idx.vertex_index+0];
- // tinyobj::real_t green = attrib.colors[3*idx.vertex_index+1];
- // tinyobj::real_t blue = attrib.colors[3*idx.vertex_index+2];
+ // tinyobj::real_t red = attrib.colors[3*size_t(idx.vertex_index)+0];
+ // tinyobj::real_t green = attrib.colors[3*size_t(idx.vertex_index)+1];
+ // tinyobj::real_t blue = attrib.colors[3*size_t(idx.vertex_index)+2];
}
index_offset += fv;
@@ -335,24 +344,33 @@ 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[s].mesh.num_face_vertices.size(); f++) {
- int fv = shapes[s].mesh.num_face_vertices[f];
+ size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]);
// Loop over vertices in the face.
for (size_t v = 0; v < fv; v++) {
// access to vertex
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
- tinyobj::real_t vx = attrib.vertices[3*idx.vertex_index+0];
- tinyobj::real_t vy = attrib.vertices[3*idx.vertex_index+1];
- tinyobj::real_t vz = attrib.vertices[3*idx.vertex_index+2];
- tinyobj::real_t nx = attrib.normals[3*idx.normal_index+0];
- tinyobj::real_t ny = attrib.normals[3*idx.normal_index+1];
- tinyobj::real_t nz = attrib.normals[3*idx.normal_index+2];
- tinyobj::real_t tx = attrib.texcoords[2*idx.texcoord_index+0];
- tinyobj::real_t ty = attrib.texcoords[2*idx.texcoord_index+1];
+ tinyobj::real_t vx = attrib.vertices[3*size_t(idx.vertex_index)+0];
+ tinyobj::real_t vy = attrib.vertices[3*size_t(idx.vertex_index)+1];
+ tinyobj::real_t vz = attrib.vertices[3*size_t(idx.vertex_index)+2];
+
+ // Check if `normal_index` is zero or positive. negative = no normal data
+ if (idx.normal_index >= 0) {
+ tinyobj::real_t nx = attrib.normals[3*size_t(idx.normal_index)+0];
+ tinyobj::real_t ny = attrib.normals[3*size_t(idx.normal_index)+1];
+ tinyobj::real_t nz = attrib.normals[3*size_t(idx.normal_index)+2];
+ }
+
+ // Check if `texcoord_index` is zero or positive. negative = no texcoord data
+ if (idx.texcoord_index >= 0) {
+ tinyobj::real_t tx = attrib.texcoords[2*size_t(idx.texcoord_index)+0];
+ tinyobj::real_t ty = attrib.texcoords[2*size_t(idx.texcoord_index)+1];
+ }
+
// Optional: vertex colors
- // tinyobj::real_t red = attrib.colors[3*idx.vertex_index+0];
- // tinyobj::real_t green = attrib.colors[3*idx.vertex_index+1];
- // tinyobj::real_t blue = attrib.colors[3*idx.vertex_index+2];
+ // tinyobj::real_t red = attrib.colors[3*size_t(idx.vertex_index)+0];
+ // tinyobj::real_t green = attrib.colors[3*size_t(idx.vertex_index)+1];
+ // tinyobj::real_t blue = attrib.colors[3*size_t(idx.vertex_index)+2];
}
index_offset += fv;