How do you make your own file format?

Started by
10 comments, last by Kate18 20 years, 3 months ago
Hi, I have made a simple raytracer in c# (non realtime) that renders spheres. Now I would like to make a simple modeller that makes spheres and be able to save the file and have it rendered by my raytracer. I am totally confused about how to do this. Is there a way to make dx display files that aren''t in .x format? Any tips on this would be great, Thanks Kate
Advertisement
Where the data comes from is irrelevant, it''s just data. You can load the data from anywhere. The .x format is good because there are helper functions to load the data from the file. You''ll have to do this yourself if you are not going to use the .x format. If your just creating a sphere you could just create the data `on the fly`,put it in a vertex buffer, then draw it.
If all you''re rendering/modelling is spheres then you don''t need anything as complex as .x file support. Correct me if I''m wrong, but all you really need is a few D3DXCreateSphere() calls to generate the meshes. Then you can just serialize the data as the centers of the spheres combined with their radii and material information.
Thanks guys, but that just went way over my head. I did a bit of research on serializiation but its not making much sense.
Maybe if I show an example of a scene my raytracer renders.
This is a sphere with a blue background

eye 0 2 10
lookat 0 0 0
up 0 1 0
fov 40
background 0.2 0.8 0.9
light 1 1 1 ambient
light 1 1 1 directional -1 -2 -1
light 0.5 0.5 0.5 point -2 4 -2

surface 0.6 0.2 0.5 0.4 0.4 0.6 100.0 0.0 0.0 1.0
sphere 0 1 0 2

Now, in my modeller app, I have a window that is rendering a blank screen and a button that when clicked creates a sphere in the center of the screen. Now, when i click on the render button, i need the scene to be saved with camera position, background colour and the sphere in the format as above, then my raytracer opens and renders the file that was just saved.
Even if my modeler opens up .x files, i need to find a way to out put files that my raytracer can render.
A few more hints would be nice
Thanks again
Do you have a requirement to render more complex objects and scenes later? If so, the work to save your scene as .X might be worth it. If not, then a simple text parser will do what you want for spheres (or other simple primitives) quite nicely.

There's many ways to do this (including the use of scripting language libraries), but here's a simple approach that will work for the simple case, in C#.

To save: Use a StreamWriter class to create/open a file and write out the scene, one line at a time, literally in the format you give in your example above. Writing an ASCII text file for that scene should be trivial if you already wrote a ray tracer.

To load: Use a StreamReader class to open a saved scene file and read it in one line at a time. As you have each line in memory as a string variable, use the string.Split method, specifying a 'space' character as the delimiter. This will give you an array of strings containing the items on that line, in order. Then perform a switch expression on the first element of the array, where your 'case's will be like "eye", "lookat", "up", etc. Within each case you will now know how to handle the expected number of elements that make up the rest of the array (as they are the specific parameters to your keyword - "eye" would expect 3 strings containing integers, "fov" would expect 1 string containing an integer, etc).

Hope this helps.

Atlay

[edited by - Atlay on January 17, 2004 7:16:37 AM]
You guys are being bloody complicated...do it simple.

Write them all to file in a certain order, so start with a struct or something like so:
struct MeshFileData{    float Eye[3];    float LookAt[3];    float Up[3];    float FoV;    float Background[3];    //etc.}; 


And use fwrite to write it to file. fread it again later to load it.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
better still just whack it out into xml
(Visual Basic .NET) www.elpuerco.co.uk
Promit: This is C#, not C or C++.

Back on topic, personally I think it would be easier to use .NET''s serialization facility as you seem to want to use C# for both the modeller and the raytracer.

In many cases, using .NET serialization is as simply as smacking a [Serialized()] at the top of the class to be saved and then using a BinaryFormatter (or SoapFormatter) on an open stream. And the data you seem to want to load/save seems simple enough to use this approach.

The MSDN entry for BinaryFormatter should have sample code that can get you started.
wow, c# does not have fwrite or fread? I''m guessing that it does support any other of the standard functions, either.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
?

Why would C# have C standard library functions? C# is a different language without source code compatibility with either C or C++.

This topic is closed to new replies.

Advertisement