Sign in to follow this  
n3Xus

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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by stonemetal
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by deadstar
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).


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 stonemetal
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by n3Xus
Quote:
Original post by deadstar
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).


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 stonemetal
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.


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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this