diff options
author | Syoyo Fujita <syoyo@lighttransport.com> | 2021-03-19 14:38:58 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-19 14:38:58 +0900 |
commit | f760a8a0356a4372a27ac81f28f6afed78b5e63d (patch) | |
tree | ab576fcd636f551c7e15727fd2902db01c958736 | |
parent | 7ba4b652ee0c5175ec8abf66199e84d88adf11f1 (diff) | |
download | tinyobjloader-f760a8a0356a4372a27ac81f28f6afed78b5e63d.tar.gz |
Update example code in README. (#301)
-rw-r--r-- | README.md | 66 |
1 files changed, 42 insertions, 24 deletions
@@ -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; |