Skip to content

The example code for the new OO API isn't quite right #300

@erich666

Description

@erich666

Describe the issue
For this main readme file: https://github.com/tinyobjloader/tinyobjloader/blob/master/README.md#example-code-new-object-oriented-api

This "Example code (New Object Oriented API)" code works well to read in cornell_box.obj. There's just one minor issue flagged by Visual Studio, that "v" and "fv" are of different types (size_t vs. int).

But, the access code:

            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];

fails and asserts when it tries to read the normals, because there are no normals computed.

To Reproduce
Steps to reproduce the behavior:

  1. Compile in some new, basic console app the example code https://github.com/tinyobjloader/tinyobjloader/blob/master/README.md#example-code-new-object-oriented-api
  2. Run
  3. See assert

Expected behavior
A clear and concise description of what you expected to happen.

Here's the fixed code I suggest for the example:

        for (size_t v = 0; v < (size_t)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];
            if (idx.normal_index >= 0) {
                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];
            }
            if (idx.texcoord_index >= 0) {
                tinyobj::real_t tx = attrib.texcoords[2 * idx.texcoord_index + 0];
                tinyobj::real_t ty = attrib.texcoords[2 * 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];
        }

Environment

  • TinyObjLoader version v2-rc1
  • OS: Windows 10
  • Compiler Visual Studio
  • Other environment - none

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions