<p>The list of pixels would typically be encoded as one of the popular image formats like ktx if it is going to be consumed by other rendering programs, or jpg if it is just for viewing. The vertices, normals and UV values would be formatted into whatever file format you desire to output.</p>
<p>This means picking a file format, like obj which is a nice simple format.</p>
<p>Basically a program would output the &quot;pixels&quot; as texture data, the other data formatted to the file type chosen and the file would &quot;refer&quot; to the texture data.</p>
<p>On the other hand some formats include the &quot;pixels&quot; directly in the format.</p>
<p>There are libraries out there that can help, or you can write your own encoder.</p>
<p>As Nicol Bolas pointed out it is difficult to give a more complete answer without knowing more details about the data and the file format.</p>
<p>Are you coding this up or is it from one of the popular rendering programs out there?</p>

CC BY-SA 4.0
2023-03-03T22:00:39.320Z
2023-03-03T22:00:39.320Z
pmw1234
<p>Here is an overview of how you would go about producing an glTF asset from your data.</p>
<p>First, write your index and vertex data arrays into files.</p>
<ul>
<li>They can be all the same file, but for the conceptually simplest case, make them separate files.</li>
<li>You write the integers or floats as raw bytes, not text or any other format, with no metadata. This is just like you would pass the same data to a GPU buffer, <strong>except</strong> that they must be <em>little endian</em> integers (that is, swap the order of the bytes in each integer if your program is compiled for a big-endian processor; this is rare but possible).</li>
</ul>
<p>Your “pixels” — a texture for the object, I assume — should be written into a image file (PNG, JPEG, or a compressed texture format specified in a glTF extension).</p>
<p>Then, you create the glTF file itself, which is a JSON structure that <strong>describes how to use the contents of the above binary files.</strong></p>
<p>The glTF file will specify that the indices are indices, and which one of the files contains vertex positions and which one contains normals, using “attributes” and “accessors”, and put them together into a “mesh”. It may also contain “node”, “scene”, “camera”, and “animation” objects, describing more about what to <em>do</em> with the mesh, but these are all optional.</p>
<p>There are a lot of details to glTF, but this is fundamentally all there is to it: write out your numerical data to binary files, then write out the glTF that describes the structure of those files. (It's also possible to pack all the data into a single <code>.glb</code> file.)</p>
<p>Now that I've given you the big picture, I hope you will be able to able to figure out all the concrete details by following the <a href="https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html" rel="nofollow noreferrer">glTF specification</a> and checking your work with a glTF scene viewer and a validator. When I was doing this, I found the <a href="https://marketplace.visualstudio.com/items?itemName=cesium.gltf-vscode" rel="nofollow noreferrer">glTF Tools Extension for Visual Studio Code</a> very useful — it does both preview and validation.</p>

CC BY-SA 4.0
2023-03-04T01:37:46.980Z
2023-03-04T15:22:16.033Z
2023-03-04T15:22:16.033Z
Kevin Reid
<p>I have searched around in a lot of places but I have yet to find some place that explains 3D file formats in a way that aligns with the model I have right now. I have a simple problem (I think) but it's hard to get through all the file format documentation and to understand what the data actually resembles.</p>
<p>Let's say one would have the following raw data in a data structure that resembles the below:</p>
<ul>
<li>A index list, of numbers
<ul>
<li><code>int[]</code></li>
</ul>
</li>
<li>A list of objects containing vertices for a mesh, with their normals and texture coordinates
<ul>
<li><code>{vertices: vec3[], normals: vec3[], UV: {u: float, v: float}}[]</code></li>
</ul>
</li>
<li>A list of pixels
<ul>
<li><code>float[]</code></li>
</ul>
</li>
</ul>
<p>How would one go about turning this data structure into a popular file format? I don't know where to begin. GlTF would have my preference if that's doable/possible.</p>

CC BY-SA 4.0
2023-03-03T21:09:01.500Z
What does any of that data *mean*? Like, what does this "list of pixels" actually *do*? It's hard to know what "aligns with the model I have right now" when we don't know what that "model" even is.
Nicol Bolas
<gltf>
How does one construct a 3D file format from pixels and vertices?
Marcel
# How does one construct a 3D file format from pixels and vertices?

(Asked by: username_0 on 2023-03-03)

I have searched around in a lot of places but I have yet to find some place that explains 3D file formats in a way that aligns with the model I have right now. I have a simple problem (I think) but it's hard to get through all the file format documentation and to understand what the data actually resembles.

Let's say one would have the following raw data in a data structure that resembles the below:

* A index list, of numbers
  + `int[]`
* A list of objects containing vertices for a mesh, with their normals and texture coordinates
  + `{vertices: vec3[], normals: vec3[], UV: {u: float, v: float}}[]`
* A list of pixels
  + `float[]`

How would one go about turning this data structure into a popular file format? I don't know where to begin. GlTF would have my preference if that's doable/possible.

**Tags:** gltf

> **Comments:**
> **username_1**: What does any of that data *mean*? Like, what does this "list of pixels" actually *do*? It's hard to know what "aligns with the model I have right now" when we don't know what that "model" even is.  

---

## 2 answers

### Answer by username_2 on 2023-03-03. Score: 1

The list of pixels would typically be encoded as one of the popular image formats like ktx if it is going to be consumed by other rendering programs, or jpg if it is just for viewing. The vertices, normals and UV values would be formatted into whatever file format you desire to output.

This means picking a file format, like obj which is a nice simple format.

Basically a program would output the "pixels" as texture data, the other data formatted to the file type chosen and the file would "refer" to the texture data.

On the other hand some formats include the "pixels" directly in the format.

There are libraries out there that can help, or you can write your own encoder.

As username_1 username_1 pointed out it is difficult to give a more complete answer without knowing more details about the data and the file format.

Are you coding this up or is it from one of the popular rendering programs out there?


---

### Answer by username_3 on 2023-03-04. Score: 1

Here is an overview of how you would go about producing an glTF asset from your data.

First, write your index and vertex data arrays into files.

* They can be all the same file, but for the conceptually simplest case, make them separate files.
* You write the integers or floats as raw bytes, not text or any other format, with no metadata. This is just like you would pass the same data to a GPU buffer, **except** that they must be *little endian* integers (that is, swap the order of the bytes in each integer if your program is compiled for a big-endian processor; this is rare but possible).

Your “pixels” — a texture for the object, I assume — should be written into a image file (PNG, JPEG, or a compressed texture format specified in a glTF extension).

Then, you create the glTF file itself, which is a JSON structure that **describes how to use the contents of the above binary files.**

The glTF file will specify that the indices are indices, and which one of the files contains vertex positions and which one contains normals, using “attributes” and “accessors”, and put them together into a “mesh”. It may also contain “node”, “scene”, “camera”, and “animation” objects, describing more about what to *do* with the mesh, but these are all optional.

There are a lot of details to glTF, but this is fundamentally all there is to it: write out your numerical data to binary files, then write out the glTF that describes the structure of those files. (It's also possible to pack all the data into a single `.glb` file.)

Now that I've given you the big picture, I hope you will be able to able to figure out all the concrete details by following the [glTF specification](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html) and checking your work with a glTF scene viewer and a validator. When I was doing this, I found the [glTF Tools Extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=cesium.gltf-vscode) very useful — it does both preview and validation.


---

