Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cowsarenotevil

Miserable file loader

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, I have this file loader, and in case you can''t tell... I don''t know the first thing about loading files... it loads vertices for a model... It also loads faces, but they are basically the same. The problem is that it takes about 30 seconds to load a decent model... please help. the loader:
  
	ifstream file;
	file.open(FILE1);
/*
File format should be as follows:
	1. file should have no carriage returns on a blank line (no blank lines)
	2. break files into sections of 3 lines
		a) first line is x coordinate
		b) second line is y coordinate
		c) third line is z coordinate
NOTE: There is no error checking for the file, so hope to god it works...
*/
	for (long int a=1; !file.eof(); a++)
	{
		vertex.resize(a+1);
		getline(file,vertex[a].xx);	// grab first line

		getline(file,vertex[a].yy);	// grab second line

		getline(file,vertex[a].zz);	// grab third line

		vertex[a].x = atof(vertex[a].xx.c_str());	// convert x to a double

		vertex[a].y = atof(vertex[a].yy.c_str());	// convert y to a double

		vertex[a].z = atof(vertex[a].zz.c_str());	// convert z to a double

	}
	file.close();	// be cleanly...

  
Thanks!

Share this post


Link to post
Share on other sites
Advertisement
1) store number of vertices up front and only resize() once.

2) use >> into floats instead of getline().

Share this post


Link to post
Share on other sites
I don''t really know the answer to your problem, but i''ve got a few suggestions for your code.

First of all if you didn''t already at the begining of your progmram include this:
#include <iostream>

The when you open your file, this is how the line should look:
file.open(FILE1,ios:in);

just something i learned in my AP Comp Sci class.

and like niyaw said use ">>" operator

Share this post


Link to post
Share on other sites
I can''t get it... I now have this


  	ifstream file;
file.open(FILE1);
/*
File format should be as follows:
1. file should have no carriage returns on a blank line (no blank lines)
2. break files into sections of 3 lines
a) first line is x coordinate
b) second line is y coordinate
c) third line is z coordinate
NOTE: There is no error checking for the file, so hope to god it works...
*/

for (long int a=1; !file.eof(); a++)
{
//getline(file,vertex[a].xx); // grab first line

//getline(file,vertex[a].yy); // grab second line

//getline(file,vertex[a].zz); // grab third line

file >> (float)vertex[a].x; // convert x to a double

file >> (float)vertex[a].y; // convert y to a double

file >> (float)vertex[a].z; // convert z to a double

}
vertex.resize(a+1);
file.close(); // be cleanly...


Needless to say it does not work!
Thanks again!

Share this post


Link to post
Share on other sites
try running a for loop before the loading process to check how many lines the file is:

int i=0;
while (!file.eof())
{
i++;
}
i = (i/3)+1; // since you load in 3 things, and you want to make sure there is enough space for the vector

Then resize it:

vertex.resize(i);

Then run your original for loop. That should eliminate your problem, but the question remains as to how long it takes to find the eof mark. It should run quickly I think, but could someone back me up on that or disprove my theory?

Share this post


Link to post
Share on other sites
!file.eof();

Think this is the problem!!! I think that testing the end of file every time you loop, ins''t a good idea!!! Some how, you must to know about the file size to check the end of file. Files like 3ds, organize the data in chunks with the segmentes of the data in the file. Those files says the dize of thme selves, I think, sou you must doo the same to avoid those trobles....

Filami

Techno Grooves

Share this post


Link to post
Share on other sites
You should use a standard file format (i suggust ms3d or md2), because loaders are already written for them (and if you want to write one, the documentation is avaliable), and they are tried and tested. You also wont find yourself writing an editor for your format. If you are using your format for something other than rendering a model, look at the other formats anyway. They should give you a few ideas.

btw i think its atof() that is slowing you down. There are ways to read the value directly. One more thing, you really shouldn''t store the vertex data as a string if you arn''t going to use it again, but that shouldnt matter if you are going to skip the conversion.

"Free advice is seldom cheap."
-- Rule of Acquisition #59

Share this post


Link to post
Share on other sites
something like

    	ifstream file;
file.open(FILE1);
/*
File format should be as follows:
1. file should have no carriage returns on a blank line (no blank lines)
2. first number in the file is total number of vertices
3. break files into sections of 3 lines
a) first line is x coordinate
b) second line is y coordinate
c) third line is z coordinate
NOTE: There is no error checking for the file, so hope to god it works...
*/

int num_vertices;
file >> num_vertices;
vertex.clear();
vertex.reserve(num_vertices);
for (;;)
{
// get three numbers, Vertex is your vertex type

Vertex v;
file >> v.x >> v.y >> v.z;
// check for end of file

if (file.eof()) break;
// add vertex to your vector

vertex.push_back(v);
}
file.close(); // be cleanly...

Share this post


Link to post
Share on other sites
I got it!


  	

apvector<VERTEX> vertex(50000);

...

fstream file;
file.open(FILE1, ios::binary | ios::in);
/*
File format should be as follows:
1. file should have no carriage returns on a blank line (no blank lines)
2. break files into sections of 3 lines
a) first line is x coordinate
b) second line is y coordinate
c) third line is z coordinate
NOTE: There is no error checking for the file, so hope to god it works...
*/

for (long int a=1; !file.eof(); a++)
{

file >> vertex[a].x; // convert x to a double

file >> vertex[a].y; // convert y to a double

file >> vertex[a].z; // convert z to a double

}
vertex.resize(a+1);
file.close(); // be cleanly...




Thanks Everyone!!

Share this post


Link to post
Share on other sites
I got it!


  	

apvector<VERTEX> vertex(50000);

...

fstream file;
file.open(FILE1, ios::binary | ios::in);
/*
File format should be as follows:
1. file should have no carriage returns on a blank line (no blank lines)
2. break files into sections of 3 lines
a) first line is x coordinate
b) second line is y coordinate
c) third line is z coordinate
NOTE: There is no error checking for the file, so hope to god it works...
*/

for (long int a=1; !file.eof(); a++)
{

file >> vertex[a].x; // convert x to a double

file >> vertex[a].y; // convert y to a double

file >> vertex[a].z; // convert z to a double

}
vertex.resize(a+1);
file.close(); // be cleanly...




Thanks Everyone!!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!