-1

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.

Marcel
  • 99
  • 1
  • 2
    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 Mar 03 '23 at 21:09

2 Answers2

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 Nicol Bolas 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?

pmw1234
  • 3,209
  • 1
  • 8
  • 16
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 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 very useful — it does both preview and validation.

Kevin Reid
  • 1,036
  • 1
  • 6
  • 8