简单的模型加载器问题

时间:2013-01-17 12:57:23

标签: c++ opengl

我们一直在创建一个非常基本的模型加载器。代码本身如下;主要问题是当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 - 然后根据所使用的类型读取一个较大的负数,这样就没有多少表示。

1 个答案:

答案 0 :(得分:0)

字符串流可能设置了错误标志(EOF),这会阻止格式化输入。在流上调用str()不会重置标志。

调用str()后,调用clear()清除标志。