Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
erich666 opened this issue Mar 17, 2021 · 3 comments
Open

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

erich666 opened this issue Mar 17, 2021 · 3 comments

Comments

@erich666
Copy link

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
@syoyo
Copy link
Collaborator

syoyo commented Mar 18, 2021

@erich666 Good issue!

I have created PR to fix/update example code in README.md. Could you please check it? #301

@erich666
Copy link
Author

I gave it a compile and run, the added code worked fine. Is there a way to test if there are RGB colors per vertex? I never use that format, so don't know about it.

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

Anyway, other than that bonus test, if there is one, looks good.

@syoyo
Copy link
Collaborator

syoyo commented Mar 19, 2021

@erich666 thanks! > I gave it a compile and run, the added code worked fine

There is an example to display vertex color of .obj data in NanoRT example:

https://github.com/lighttransport/nanort/tree/master/examples/gui

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants