2013-01-17 90 views
0

我們一直在創建一個非常基本的模型加載程序。代碼本身在下面;主要問題是當stringstream檢測到'f'作爲第一個字符時。爲了調試起見,代碼已經過於簡化(起初稍微複雜一點)。目前,cout < < ind3;給出0.它應該讀取2或5,取決於讀者所在的行。這兩個矢量參數用於寫入繪圖,但是在那一刻我刪除了這個操作。簡單模型加載程序問題

兩個 '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; 
} 

有沒有人有任何想法爲什麼三個inds被讀爲平0?這並不是說我已經將它們初始化爲0,而是在它們都讀取一個大的負數之前,這取決於所使用的類型,因此並不表示太多。

回答

0

字符串流可能包含錯誤標誌集(EOF),這會阻止格式化輸入。在流上調用str()將不會重置標誌。

調用str()後,調用clear()來清除標誌。

+0

啊完美,謝謝! – DerryHolt