[C++] Simple file format for a 3d model

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm trying to make a simple file format for my 3d models (right now I only want to export vertices and indices). I'm using a binary file format now to test it a bit (later I'm gonna use 3ds max to export it). now the problem is: how should I save my vertex data (well actually i don't know exactly how to ask it...)? lets say I use the D3DXVECTOR3 for mesh xyz data. now how can I fill my struct (the one I will save as a binary file) with vertex data? the most simple way would be to just predefine an array of X elements but that is stupid, since the model may have a lot more or a lot less vertices. the problem is that i must know the file size when I'm reading the file so I have no idea how I should make this thing. I hope you'll understand what i wanted to ask :P Are there any articles on this subject (simple 3d file formats)?

Share on other sites
I recommend you start with text-based solutions - Google for OBJ documentation or 3D Studio Max's ASE format (there's tons of info in the Max SDK if you have access to it).

Once you have the general gist of things, write your own! There's nothing more rewarding.

Share on other sites
Why not do the simple thing? first write I have X verts as an unsigned int. then start writing xyz triples. Then write I have X indices and start writing indices. I am confused by the requirement that you know the file size ahead of time.

Share on other sites
Quote:
 Original post by stonemetalWhy not do the simple thing? first write I have X verts as an unsigned int. then start writing xyz triples. Then write I have X indices and start writing indices. I am confused by the requirement that you know the file size ahead of time.

Filesize... maybe not during writing, but certainly CHUNK size. At the moment I calculate it before hand - Header + int (numverts) + (5 x float (X, Y, Z, U, V)) - however I'm switching to writing the chunk to a temporary stream beforehand, reading its size, then writing to file.

You need to know the filesize during READING for error checking, test for E.O.F in unnecessary places, etc, but I don't write this to the file, I test it with:

//Determine filesize (Seek to end, get current position, rewind to start)fseek(File, 0, SEEK_END);FileSize = ftell(File);rewind(File);

Hope it helps.

Share on other sites
Quote:
 Original post by deadstarI recommend you start with text-based solutions - Google for OBJ documentation or 3D Studio Max's ASE format (there's tons of info in the Max SDK if you have access to it).

Well I had a simple text based format but as soon as I had 10k+ vertices it took quite a while for it to export and the filesize was huge. Altough I will check the obj and ase files (yes, I have max sdk)

Quote:
 Original post by stonemetalWhy not do the simple thing? first write I have X verts as an unsigned int. then start writing xyz triples. Then write I have X indices and start writing indices. I am confused by the requirement that you know the file size ahead of time.

the problem is I'm not sure how to do that in an binary file, since it must now the "layout" of the file, I had a thing like that in the "text" format

thx for the help, I'll look into this stuff

Share on other sites
Quote:
Original post by n3Xus
Quote:
 Original post by deadstarI recommend you start with text-based solutions - Google for OBJ documentation or 3D Studio Max's ASE format (there's tons of info in the Max SDK if you have access to it).

Well I had a simple text based format but as soon as I had 10k+ vertices it took quite a while for it to export and the filesize was huge. Altough I will check the obj and ase files (yes, I have max sdk)

Quote:
 Original post by stonemetalWhy not do the simple thing? first write I have X verts as an unsigned int. then start writing xyz triples. Then write I have X indices and start writing indices. I am confused by the requirement that you know the file size ahead of time.

the problem is I'm not sure how to do that in an binary file, since it must now the "layout" of the file, I had a thing like that in the "text" format

thx for the help, I'll look into this stuff

It's really not difficult...I don't have my "How to program in C++"(Deitel) book with me: there's a whole chapter devoted to writing and reading sequential and random access files. But basically, you'll need to define a class( or structure ) for your data( the number of bytes per instance is a characteristic of your file structure ): then you'll write and read your binary files using an fstream object and the reinterpret_cast operator.
When I'll be home, I'll post an example.

Share on other sites
please do if you have the time :D

anyway, i was thinking of writing a simple predefined "header" section to my file, that will contain the offset and the size of various data sections so I could read them later(the section for vertex data, the section for material properties,...), the header data will always be in the very beginning of the file, at the same "position".
is this considered "good"(well, there isn't anything that is good or perfect, but do you think it would be a "solid foundation" for adding new data sections to the file format later on?)?

Share on other sites
*bump*
Would that be "good"?

Share on other sites
Quote:
 Original post by n3Xus*bump*Would that be "good"?

Sorry, I went home very late...Well, I haven't written a sophisticated file format myself, but it doesn't matter if you write random access files...Because, you know where to find the data( previous header specifications ), and therefore, adding another type of data at the end of the previous structure, is not a problem.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631773
• Total Posts
3002268
×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!