Search code examples
gltf

Vertex normal, texture uv coordinate


I have one gltf issue: In gltf, does one vertex have to correspond to one vertex normal and one vertex texture uv coordinate? If I have one source format's model, in which one vertex can correspond to three vertex normals. How can I export such model to the glTF file?

Example: A cube in the source format's model: 8 vertexes, 24 vertex normals. A cube in the glTF file: Need I write 24 vertexes, and 24 vertex normals?


Solution

  • Yes, glTF exporters/writers must "split" vertices anywhere that discontinuous UVs or normals appear.

    glTF is designed to be a GPU-ready delivery format, a last-mile format, not an artist interchange format. As a result, its internal data structures are nearly a 1:1 match with the way vertex attributes are handed off to the GPU, for example with a vec3 position attribute, vec3 normal attribute, and possibly a vec2 texture coordinate in a typical case. So yes, one normal and one UV per vertex, as expected for a set of raw data being supplied to the GPU.

    Part of the advantage is that a well-curated collection of glTF files will contain binary payloads that can be set to (for example) mobile devices, where those mobile devices can then transfer whole sections of the binary data straight to GPU memory without further processing. WebGL frameworks, for example, don't have to do a lot of vertex processing after receiving the file, they just load it and render. The burden is placed explicitly on exporters and writers, not readers and loaders.

    More details on the structure are spelled out fairly well in the glTF Tutorial. In particular the section on Buffers, Buffer Views, and Accessors covers the raw storage of vertex data in binary blobs. Generally, a programmer familiar with graphics APIs could think of a glTF accessor as an individual vertex attribute, and a bufferView as a block of GPU memory containing multiple vertex attributes (accessors) at the same stride, possibly interleaved. The buffer itself is just a lump of all binary data (bufferViews) in a glTF, without any stride.