Advertisement

files instead of...

Started by January 31, 2002 03:45 PM
2 comments, last by bdrewes 22 years, 10 months ago
Here is a question that has one big foot in the begginers land, and the other in the realm of advanced. I''m creating a program to learn the basics of 3d programming. Stuff like performing rotation an array of vertices etc... I''m using GLUT to display my results and so far everything is working great. The way it is now, an object is defined in an array ''Point *vertices'' which is grows dynamically using a temporary array and something like this: vertices = new Point[..etc..]. Vertices are added by calling add_vertex(0.0, 1.0, 0.0). So far so good, but what I realize, is that I''m creating more of a 3d modeling program than a game graphics engine, which is what I really want. A graphics engine might have the capability to manually add geometry within the code, but for the most part all the 3d stuff should come from some other program like 3d studio max. Besides all the other benifits, the most important right now is that it would eliminate the need to be dynamically allocating memory each time I add a vertex. I could instead allocate as much as I need at the beggining of my code and load all the vertices into space. So my question is this...I know I could write a program with c++ file processing (fstream etc) that would read a .txt file that looks like this: vertices: 3 0 1 0 -1 -1 0 -1 1 0 faces: 1 1 2 3 But the thing about me is that just don''t look right. To me, this: MMÇU ==·U >= €?ÿ¯Á   material_1     ™™™ ™™™0  ÿÿÿ ÿÿÿ@  0 ) A  0 ( P  0 R  0 S  0 ¡ „  0 Š  @é object_1 Aóè AxD ´øËS½ ðá>¢~0<øËS½ ðá>¢~0<ýÊS½oæ>áú˜;ýÊS½oæ>áú˜;ÖŸS½+sß>ú1?<ÖŸS½+sß>ú1?<ÉÖR½­Þ>‘;<ÉÖR½­Þ>‘;<Á÷P½rÀÝ>-©2<Á÷P½rÀÝ>-©2L½C¡Ü>ÿ_<*L½wç>{8®;¯†J½v¥á>5d1<}²H½dºÞ>ÛgDÛ looks right... (the average contents of any given file on my computer, in this case a .3ds file). So before I ask anything further, like what are chunks? And how do I load a .3ds file? Could someone tell me why my file (vertices, faces etc) looks so differant then the .3ds file (AxD´øËS½ ðá>¢~0<øËS½ ðá>.. etc)? Does anyone know of a really basic tutorial about saving, loading and reading a file type of your own creation? Thanks...
Your file is straight ASCII text. When you read it, it is assumed you are going to be grabbing an entire character at a time.

The reason most other files look like hieroglyphics to us is because the data is designed to be read in terms of bytes. The string "32" is enormously inefficient compared to how many bits you really need to represent that number.

If you want a good example of why ASCII text files aren''t used by many programs, look at the difference between a BMP and a PGM file (a PGM image is saved as an ASCII file and is very easy to read by a human). I just loaded up one of my image-processing programs and saved the same exact image as a BMP and then as a PGM. The BMP weighed 576KB, and the PGM clocked in at 913KB!
Advertisement
That clears that up. I knew there was some reason why using regular text files isn''t the way it''s done...

Ok, so knowing a bit more then I did before, I want my program to create and read files that are read in bytes.

For now I won''t be wanting to do anything as complex (in my mind) as importing a .3ds file, but instead take the program I have now, think of it as a 3d modeler, and tell to take all the data (right now just vertices xyz locations and the center point of an object) and save it in a .MCE (My Cool Extension) file. When I open it in notepad, it should like like this:

>¢~0<ýÊS½oæ>áú˜;ýÊS½oæ>áú˜;ÖŸS½+sß>ú1?
<ÖŸS½+sß>ú1?<ÉÖR½­Þ>‘;<ÉÖR½­Þ>‘;<Á÷P½rÀÝ>
-©2<Á÷P½rÀÝ>-©2L½C¡Ü>ÿ_<*L½wç>{8®;¯†

Here are some questions:

1) How do I do that
2) Are c++ fstream functions used?
3) Using what ever method to write to a file, would one simply pass pointers adresses to the file?
4) Could you or someone post an example of a really simple file write function, maybe something like this:

const char *text = "Hello";
.
.
.
// (some code putting ''text'' into a file)

and have the contents of the file, when looked at in notepad look like this:

-©2L½C¡Ü>ÿ_ (...thats the most important part to me, is how it looks )

5) What are the prerequisites or programming skills needed before getting into all of this?
You could try this code, ( I haven''t tested it )

  #include <fstream>using namespace std;void main(){	int number;	int numberArray[10];		ofstream outfile("test.dat", ios::out | ios::binary);	ifstream infile("test.dat",  ios::in  | ios::binary);	// Write the integers 1-10 to the file	for(int i = 1; i <= 10; ++i)		outfile.write((const char*)&i, sizeof(i));  	// Close the out file	outfile.close();	// Read back all numbers and store them in an array	infile.read((char *)&numberArray, sizeof(int)*10);	        // testing....        cout << numberArray[5];	// If you want to read back the values one-by-one use        // this instead	// while(infile.read((char*)&number, sizeof(number)))	// 		cout << number << endl;  		// Close the infile	infile.close();}  

This topic is closed to new replies.

Advertisement