We’ve been creating a very basic model loader. The code itself is below; the main problem is when the stringstream detects an ‘f’ as a first character. The code has been overly simplified (it was a bit more complicated at first) for the sake of debugging. At the moment, cout << ind3; is giving 0. It should read either a 2 or a 5, depending on which line the reader is on. The two vector parameters are used to write to for drawing, but at the minute I’ve removed this operation.
The two ‘f’ lines are:
f 0 1 2
f 3 4 5
The program reads the v (vertex) lines in just fine; it won’t read in f lines.
bool modelLoader(string fileName,vector<GLfloat>& vertices, vector<GLushort>& indices)
{
vector<GLfloat> localVertices;
ifstream objFile;
string line,testline;
stringstream ss;
GLfloat x, y, z;
//GLushort ind1, ind2, ind3; Excluded for testing
int ind1=0, ind2=0, ind3=0;
objFile.open(fileName);
if (!objFile.is_open())
{
cout << "FAILED TO LOAD: OBJ FILE\n";
return false;
}
while ( objFile.good() )
{
getline(objFile, line);
ss.str(line);
if (line == "")
{
continue;
}
else if(line[0] == 'v')
{
ss.ignore(2);
ss >> x >> y >> z;
localVertices.push_back(x);
localVertices.push_back(y);
localVertices.push_back(z);
}
else if (line[0] == 'f')
{
cout<<ss.str()<<endl; // for debug
ss.ignore(6); // To skip 'f 0 1 ' and get purely a 2. Was originally
// set to ss.ignore(2) when reading in all 3 values.
cout<<ss.str()<<endl; // for debug
ss >> ind3;
cout << ind3 << endl;
}
}
objFile.close();
cout << "Reader success.\n";
return true;
}
Does anyone have any idea why the three inds are being read in as flat 0’s? It’s not that I’ve initialised them to 0 either – before they all read a large negative number depending on the type used so that doesn’t indicate much.
The string stream probably has error flags set (EOF), which would prevent formatted input. Calling str() on the stream will not reset the flags.
After calling str(), call clear() to clear the flags.