Skip to content

Conversation

@TeAmo-Lemon
Copy link

Description:

I encountered an issue where the model loader failed to parse OBJ files that use the v//vn format (vertex and normal, but no texture coordinates) or other variations. The original code strictly expected a v/vt/vn structure, causing read errors on models with different export settings.

What this PR does:
The current OBJ face parser assumes every face vertex always has vertex/texcoord/normal indices in the format f v/t/n v/t/n v/t/n.
It crashes or fails silently on OBJ files that use the common optional format, such as:

f 815//815 869//869 868//868

(only vertex and normal, texture coordinate is missing → double slash //)

Changes

  • Modified the face parsing loop to properly handle cases where texture coordinate or normal is omitted
  • Implemented sscanf checks to dynamically detect the format of each face vertex (v/vt/vn, v//vn, v/vt, or just v).
  • Assigned default indices (1) to prevent undefined behavior before decrementing.
  • Indices are still correctly decremented (--f, --t, --n) only when present.

Before:

  • Cannot parse f v//vn lines → parsing error or wrong indices

After:

Correctly parses lines like:

  • f 1/1/1 2/2/2 3/3/3 (full)
  • f 1//1 2//2 3//3 (no texcoord)
  • f 1/1 2/2 3/3 (no normal)
  • f 1 2 3 (only position)

Tested with:

  • Original tiny renderer models
  • Several community OBJ files containing // syntax

Thanks for reviewing!

The original parsing logic for face lines ('f') relied on a fixed 'v/vt/vn' structure, which caused failures when loading OBJ files with missing attributes (e.g., 'v//vn' or 'v/vt'). This commit replaces the strict stream extraction with a token-based approach using sscanf.
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.

1 participant