Skip to content

WaveFrontReader: read per-vertex colors from "v x y z r g b" - #329

Open
Joel Kiptoo (Kiptoo-Deus) wants to merge 1 commit into
microsoft:mainfrom
Kiptoo-Deus:wavefront-vertex-colors
Open

Joel Kiptoo (Kiptoo-Deus) wants to merge 1 commit into
microsoft:mainfrom
Kiptoo-Deus:wavefront-vertex-colors

Conversation

@Kiptoo-Deus

Copy link
Copy Markdown

Fixes #75.

Tools such as MeshLab write a per-vertex color after the position on v lines (v x y z r g b). The reader read x y z and dropped the rest of the line, so these colors were lost.

Changes in WaveFrontReader.h:

  • On a v line, the rest of the line is parsed after x y z. If there are exactly three extra values they are read as an RGB color. A single extra value is still the optional w coordinate from the OBJ spec and is ignored, and any other count is ignored as well. Positions without a color default to white.
  • The colors go into a new public vertexColors array (XMFLOAT3, one entry per entry in vertices) with a hasVertexColors flag, filled in as vertices are created from faces. When the file has no colors the array stays empty.
  • Vertex itself is unchanged, so the .vbo layout used by LoadVBO and existing callers are not affected.
  • The rest of the line is read into a std::wstring rather than a fixed buffer, so an overlong v line can't put the stream into a failed state.

Vertex deduplication is keyed on the position index, so vertices that share a position also share its color and the existing dedup behaviour doesn't change.

Testing: on macOS with clang, using the same small shim as in #324 (std::wifstream from a wchar_t* path is an MSVC extension libc++ doesn't have), I loaded:

  • the MeshLab snippet from the issue: colors 0.984314 0.764706 1.0 etc. come through with the right positions and normals
  • a quad with a different color on each corner, shared between two faces: 4 vertices, 4 colors
  • a file without colors and a file using v x y z w: hasVertexColors is false and vertexColors is empty
  • a file where only some vertices have a color: the others are white
  • CRLF line endings, an end-of-line comment and relative (negative) indices
  • a v line with 1,700 extra values: the rest of the file still loads

I didn't touch meshconvert. If you'd like the colors passed through to its output formats I can follow up on that separately.

Some tools, such as MeshLab, write a per-vertex color after the position
on 'v' lines. The reader ignored everything after x y z, so these colors
were dropped.

When a 'v' line has exactly three extra values they are read as an RGB
color. A single extra value is still treated as the optional 'w'
coordinate and ignored. Positions without a color default to white.

The colors go into a new vertexColors array, one entry per vertex,
alongside a hasVertexColors flag. Vertex itself is unchanged, so the
.vbo format and existing callers are unaffected.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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.

WaveFrontReader update to support per-vertex color extension

1 participant