Skip to content

Fix texcoord_ws corruption, alpha_texname double-write, missing BOM strip in callback, and wrong test fixture path#414

Merged
syoyo merged 5 commits intoreleasefrom
copilot/propose-codebase-enhancements
Mar 3, 2026
Merged

Fix texcoord_ws corruption, alpha_texname double-write, missing BOM strip in callback, and wrong test fixture path#414
syoyo merged 5 commits intoreleasefrom
copilot/propose-codebase-enhancements

Conversation

Copy link
Contributor

Copilot AI commented Mar 3, 2026

Four independent bugs found during codebase review, plus new tests verifying the fixes.

Bugs fixed

  • texcoord_ws always empty (LoadObj stream overload): attrib->texcoords.swap(vt) drains vt, so the subsequent attrib->texcoord_ws.swap(vt) was a no-op. Added a dedicated vt_w vector; the vt parser now reads the optional third component (vt u [v [w]] per spec) and swaps it correctly.

    // Before — texcoord_ws was always cleared
    attrib->texcoords.swap(vt);
    attrib->texcoord_ws.swap(vt);   // vt is empty here
    
    // After
    attrib->texcoords.swap(vt);
    attrib->texcoord_ws.swap(vt_w); // populated during parsing
  • Redundant write to alpha_texname in LoadMtl: material.alpha_texname = token (raw unparsed remainder) was immediately overwritten by ParseTextureNameAndOption. Removed the dead assignment.

  • LoadObjWithCallback skips UTF-8 BOM stripping: Both LoadObj and LoadMtl strip a BOM from the first line via removeUtf8Bom; the callback path was missing the same treatment.

  • test_invalid_texture_vertex_index used a non-existent fixture: Test referenced invalid-relative-texture-vertex-index.obj (file not present), causing it to pass for the wrong reason (file-open failure satisfying !err.empty()). Corrected to invalid-relative-texture-index.obj.

Tests added

  • models/texcoord-w.obj — quad with explicit vt u v w lines
  • test_texcoord_w_component — asserts attrib.texcoord_ws is correctly sized and valued after load
  • models/texcoord-w-mixed.obj — quad with alternating vt lines: some with the optional w component present, some omitted; exercises the rule that omitted w defaults to 0.0
  • test_texcoord_w_mixed_component — verifies texcoord_ws is [0.5, 0.0, 0.75, 0.0] for the mixed-w input, confirming both explicit values and the 0.0 default for omitted w are stored correctly
  • test_loadObjWithCallback_with_BOM — opens cube_w_BOM.obj via LoadObjWithCallback with vertex, group, and mtllib callbacks; asserts all 8 vertices, 6 groups, and at least one material are parsed, confirming the BOM is stripped and the mtllib line is correctly recognised

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…ile, BOM in callback, texcoord w test

Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com>
Copilot AI changed the title [WIP] Review current codebase and propose enhancements Fix texcoord_ws corruption, alpha_texname double-write, missing BOM strip in callback, and wrong test fixture path Mar 3, 2026
Copilot finished work on behalf of syoyo March 3, 2026 16:59
@syoyo
Copy link
Collaborator

syoyo commented Mar 3, 2026

@copilot add unit test for this issue(tests/tester.cc). prepare test .obj/.mtl input if necessary(and put it to models/)

…callback API

Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot add unit test for this issue(tests/tester.cc). prepare test .obj/.mtl input if necessary(and put it to models/)

Added test_loadObjWithCallback_with_BOM in tests/tester.cc (commit 4581892). The test opens models/cube_w_BOM.obj (which begins with the UTF-8 BOM 0xEF 0xBB 0xBF) via LoadObjWithCallback with vertex, group, and mtllib callbacks, then asserts that all 8 vertices, 6 groups, and at least one material are parsed — confirming the BOM is stripped and the mtllib line is correctly recognised. No new model file was needed since cube_w_BOM.obj already exists.

Copilot finished work on behalf of syoyo March 3, 2026 17:10
@syoyo
Copy link
Collaborator

syoyo commented Mar 3, 2026

@copilot add unit test for this issue(tests/tester.cc). prepare test .obj/.mtl input if necessary(and put it to models/)

Added test_loadObjWithCallback_with_BOM in tests/tester.cc (commit 4581892). The test opens models/cube_w_BOM.obj (which begins with the UTF-8 BOM 0xEF 0xBB 0xBF) via LoadObjWithCallback with vertex, group, and mtllib callbacks, then asserts that all 8 vertices, 6 groups, and at least one material are parsed — confirming the BOM is stripped and the mtllib line is correctly recognised. No new model file was needed since cube_w_BOM.obj already exists.

@copilot Removing UTF-8 BOM was already done in a4e519b . Wasn't it enough?

Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot add unit test for this issue(tests/tester.cc). prepare test .obj/.mtl input if necessary(and put it to models/)

Added test_loadObjWithCallback_with_BOM in tests/tester.cc (comm...

Commit a4e519b0 was not sufficient for LoadObjWithCallback. It added removeUtf8Bom to LoadMtl (line 2125) and the LoadObj stream overload (line 2655), but LoadObjWithCallback (which has its own independent parse loop) was left without BOM stripping. My fix in 57ba517 adds the missing call at line 3221, and test_loadObjWithCallback_with_BOM (commit 4581892) verifies the callback path correctly strips the BOM so the mtllib directive is recognised.

Copilot finished work on behalf of syoyo March 3, 2026 17:18
@syoyo
Copy link
Collaborator

syoyo commented Mar 3, 2026

@copilot add variant of uv optional w component test case. create .obj test case mixture of w exists and omitted.

Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot add variant of uv optional w component test case. create .obj test case mixture of w exists and omitted.

Added in commit 8c9eee3. models/texcoord-w-mixed.obj has 4 vt lines alternating between w-present (vt 0.0 0.0 0.5, vt 1.0 1.0 0.75) and w-omitted (vt 1.0 0.0, vt 0.0 1.0). test_texcoord_w_mixed_component verifies that texcoord_ws is correctly populated as [0.5, 0.0, 0.75, 0.0] — i.e., omitted w defaults to 0.0.

Copilot finished work on behalf of syoyo March 3, 2026 21:14
@syoyo syoyo marked this pull request as ready for review March 3, 2026 23:40
Copilot AI review requested due to automatic review settings March 3, 2026 23:40
@syoyo syoyo merged commit c20c973 into release Mar 3, 2026
33 checks passed
@syoyo syoyo deleted the copilot/propose-codebase-enhancements branch March 3, 2026 23:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes several OBJ/MTL parsing issues in tiny_obj_loader.h (texcoord w component preservation, redundant alpha_texname assignment, and UTF‑8 BOM stripping in the callback loader path) and extends the test suite + model fixtures to prevent regressions.

Changes:

  • Preserve optional vt third component (w) by parsing into a dedicated vt_w buffer and swapping into attrib->texcoord_ws.
  • Remove a redundant/dead write to material.alpha_texname in LoadMtl.
  • Strip UTF‑8 BOM on the first line in LoadObjWithCallback, matching the non-callback loaders.
  • Fix and add tests + OBJ fixtures for vt w handling and callback BOM behavior; correct a broken fixture path in an existing test.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tiny_obj_loader.h Parser fixes: correct texcoord_ws population, remove redundant alpha_texname assignment, add BOM stripping for callback loader path.
tests/tester.cc Fix one test fixture path; add new regression tests for vt w and callback BOM handling.
models/texcoord-w.obj New fixture with explicit 3-component vt lines.
models/texcoord-w-mixed.obj New fixture mixing 2- and 3-component vt lines to validate default w=0.0.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 889 to +892
std::string err;
bool ret =
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
"../models/invalid-relative-texture-vertex-index.obj", gMtlBasePath);
"../models/invalid-relative-texture-index.obj", gMtlBasePath);
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_invalid_texture_vertex_index now loads invalid-relative-texture-index.obj (texture index, not texture vertex index). The test still works, but the function name is now misleading—consider renaming the test (and its TEST_LIST entry) to match what it actually validates.

Copilot uses AI. Check for mistakes.
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

Successfully merging this pull request may close these issues.

3 participants