非常に基本的なモデル ローダーを作成してきました。コード自体は以下のとおりです。主な問題は、stringstream が最初の文字として「f」を検出した場合です。コードは、デバッグのために過度に単純化されています (最初はもう少し複雑でした)。現時点では、 cout << ind3; です。リーダーがどちらの行にあるかに応じて、2 または 5 のいずれかを読み取る必要があります。2 つのベクトル パラメータは、描画用の書き込みに使用されますが、現時点ではこの操作を削除しました。
2 つの「f」行は次のとおりです。 f 0 1 2 f 3 4 5
プログラムは v (頂点) 行を問題なく読み取ります。f 行では読み取れません。
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;
}
3 つの ind がフラット 0 として読み込まれる理由を知っている人はいますか? 私がそれらを 0 に初期化したわけではありません - 使用される型に応じてすべてが大きな負の数を読み取る前に、それはあまり意味がありません。