Archived

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

cowsarenotevil

Miserable file loader

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
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
You''re creating a 50,000 size vertex array regardless of the size....
That''s not something you want to do. First of all most of your models won''t have that many vertices. and ti''s possible some will have more.
So, you want to call resize() with the real amount of vertices in the file. If you can''t figure that out beforehand, try reading everything into a memory buffer - that will definitely speed it up anyway.

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

Share this post


Link to post
Share on other sites
as I said up above...find out how long the file is before you read it into the vector. The code for that is in my previous post, so check it out and make any other changes you need! Good luck getting this to work!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hi...

you must understand how iostream works before you use ist

this is a simple stdio fileloader doing exactly what you want, plus some easy (error)reporting


  
#include <windows.h>
#include <stdio.h>

#define MY_VEC_SIZE 100

struct YourVector
{
float x,y,z;
};

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
YourVector v[MY_VEC_SIZE];

// Workaround the MSVC R6002 Bug

for(int c = 0; c < MY_VEC_SIZE; c++)
{
v[c].x = 0.0f;
v[c].y = 0.0f;
v[c].z = 0.0f;
}

// Open your DataFile

FILE* YourFile;
YourFile = fopen("datafiles/MyDataFile.txt","rt");
if(YourFile == NULL)
{
MessageBox(NULL,"fopen failed","Error",MB_OK);
return 0;
}

long int n = 0;
int nResult;

while(n < MY_VEC_SIZE)
{
nResult = fscanf(YourFile,"%f%f%f",&v[n].x,&v[n].y,&v[n].z);
if((nResult == EOF) || (nResult < 3))
break;
n++;
}

char tmp[150] = "empty\0";

if(nResult == EOF)
{
if(n < MY_VEC_SIZE)
sprintf(tmp,"%d of %d vectors read - datafile shorter than vector",n,MY_VEC_SIZE);
}
else
{
if(n == MY_VEC_SIZE)
sprintf(tmp,"%d of %d vectors read - datafile ok",n,MY_VEC_SIZE);
else if(nResult < 3)
sprintf(tmp,"%d of %d vectors read - datafile ist corrupt: ",n,MY_VEC_SIZE);
}

MessageBox(NULL,tmp,"Error",MB_OK);

fclose(YourFile);

return 0;
}


in your datafile you can have your vevtor elements:

each at a single line
1.938
1.8474
3.83733

each vector at one line
1.938 1.8474 3.83733

all vectors in one big line
1.938 1.8474 3.83733 4.9383 4.88 3.48484 ...

if you want some datafileformat like
vec(1.938,1.8474,3.83733) a.s.o

simply change the argumentstring in the line:

   
nResult = fscanf(YourFile,"%f%f%f",&v[n].x,&v[n].y,&v[n].z);
// "%f%f%f"



into:

   
"vec(%f,%f,%f)"


if you absolutly want to use iostream i''ll post the iostream version of the code above, but i prefer this one..

regards














Share this post


Link to post
Share on other sites