我创建了一个用于OpenGL的小型OBJ文件加载器,但是根本没有读取面部。在添加面部阅读器之前,程序读取的一切都很好,所以我在这里有点难过。这是我的代码:
void OBJLoader::LoadObjFile(std::string f,std::vector<float>& v,std::vector<float>& n,std::vector<float>& u)
{
std::ifstream file;
file.open(f);
OutputDebugStringA("OPENING FILE-\n");
OutputDebugStringA(f.c_str());
std::string vertex,normal,uv,face;
std::string data;
std::string data2;
std::string data3 = "";
uv = "vt";
normal = "vn";
vertex = "v";
face = "f";
std::size_t var1;
int i = 1;
int j = 1;
int k = 1;
bool loop = true;
int loopcount = 1;
std::vector<float> tV,tU,tN;
if(file.is_open())
{
while(std::getline(file,data) && file.good())
{
var1 = data.find(vertex);
if(var1 != std::string::npos && var1 < data.size() && data[1] == 0x20)
{
data3.append(data,2,std::string::npos);
double v1,v2,v3;
std::istringstream ss(data3);
if(ss >> v1 >> v2 >> v3){ v.push_back((float)v1); v.push_back((float)v2); v.push_back((float)v3); i++;}
else
{
OutputDebugStringA("Error reading vertex data.\n");
std::stringstream ssc;
ssc << loopcount;
std::string str = ssc.str();
OutputDebugStringA((char*)str.c_str());
OutputDebugStringA("\n");
}
data3 = "";
data2 = "";
}
var1 = data.find(normal);
if(var1 != std::string::npos && var1 < data.size())
{
data3.append(data,3,std::string::npos);
float v1,v2,v3;
std::istringstream ss(data3);
if(ss >> v1 >> v2 >> v3){ n.push_back((float)v1); n.push_back((float)v2); n.push_back((float)v3); j++;}
else
{
OutputDebugStringA("Error reading normal data.\n");
std::stringstream ssc;
ssc << loopcount;
std::string str = ssc.str();
OutputDebugStringA((char*)str.c_str());
OutputDebugStringA("\n");
}
data3 = "";
data2 = "";
}
var1 = data.find(uv);
if(var1 != std::string::npos && var1 < data.size())
{
data3.append(data,3,std::string::npos);
float v1,v2,v3;
std::istringstream ss(data3);
if(ss >> v1 >> v2){ u.push_back((float)v1); u.push_back((float)v2); u.push_back((float)v3); k++;}
else
{
OutputDebugStringA("Error reading UV data.\n");
std::stringstream ssc;
ssc << loopcount;
std::string str = ssc.str();
OutputDebugStringA((char*)str.c_str());
OutputDebugStringA("\n");
}
data3 = "";
data2 = "";
}
var1 = data.find(face);
if(var1 != std::string::npos && var1 < data.size())
{
data3.append(data,2,std::string::npos);
std::istringstream ss(data3);
for(int l = 0;l < data3.size();l++)
{
if(data3[l] == '/') data3[l] = ' ';
}
std::istringstream ss(data3);
unsigned int verts[9];
char slashes[6];
//if(ss >> verts[0] >> slashes[0] >> verts[1] >> slashes[1] >> verts[2] >> verts[3] >> slashes[2] >> verts[4] >> slashes[3] >> verts[5] >> verts[6] >> slashes[4] >> verts[7] >> slashes[5] >> verts[8])
if (ss >> verts[0] >> verts[1] >> verts[2] >> verts[3] >> verts[4] >> verts[5] >> verts[6] >> verts[7] >> verts[8])
else
{
try
{
tV.push_back(v.at(verts[0]));
tV.push_back(v.at(verts[0]+1));
tV.push_back(v.at(verts[0]+2));
tU.push_back(u.at(verts[1]));
tU.push_back(u.at(verts[1]+1));
tU.push_back(u.at(verts[1]+2));
tN.push_back(n.at(verts[2]));
tN.push_back(n.at(verts[2]+1));
tN.push_back(n.at(verts[2]+2));
//
tV.push_back(v.at(verts[3]));
tV.push_back(v.at(verts[3]+1));
tV.push_back(v.at(verts[3]+2));
tU.push_back(u.at(verts[4]));
tU.push_back(u.at(verts[4]+1));
tU.push_back(u.at(verts[4]+2));
tN.push_back(n.at(verts[5]));
tN.push_back(n.at(verts[5]+1));
tN.push_back(n.at(verts[5]+2));
//
tV.push_back(v.at(verts[6]));
tV.push_back(v.at(verts[6]+1));
tV.push_back(v.at(verts[6]+2));
tU.push_back(u.at(verts[7]));
tU.push_back(u.at(verts[7]+1));
tU.push_back(u.at(verts[7]+2));
tN.push_back(n.at(verts[8]));
tN.push_back(n.at(verts[8]+1));
tN.push_back(n.at(verts[8]+2));
}
catch(std::out_of_range e)
{
OutputDebugStringA("Out of Range!\n");
}
}
}
else
{
OutputDebugStringA("Invalid face data! Generic Error!\n");
std::stringstream ssc;
std::stringstream ssc2;
ssc << loopcount;
std::string str = ssc.str();
OutputDebugStringA((char*)str.c_str());
OutputDebugStringA("\n");
}
data3 = "";
data2 = "";
}
loopcount++;
}
}
else
{
}
}
感谢任何帮助!
此处还有我正在使用的OBJ文件:
v 0.000 0.000 0.000
v 1.000 0.000 0.000
v 1.000 1.000 0.000
v 0.000 0.000 0.000
v 0.000 1.000 0.000
v 1.000 1.000 0.000
vt 0.000 0.000
vt 1.000 0.000
vt 1.000 1.000
vn 0.000 0.000 1.000
f 1/7/10 2/8/10 3/9/10
f 4/7/10 5/8/10 6/9/10
好的,我修正了斜线问题,但现在这段代码导致了std :: out_of_range的激活:
tV.push_back(v.at(verts[0]));
tV.push_back(v.at(verts[0]+1));
tV.push_back(v.at(verts[0]+2));
tU.push_back(u.at(verts[1]));
tU.push_back(u.at(verts[1]+1));
tU.push_back(u.at(verts[1]+2));
tN.push_back(n.at(verts[2]));
tN.push_back(n.at(verts[2]+1));
tN.push_back(n.at(verts[2]+2));
//
tV.push_back(v.at(verts[3]));
tV.push_back(v.at(verts[3]+1));
tV.push_back(v.at(verts[3]+2));
tU.push_back(u.at(verts[4]));
tU.push_back(u.at(verts[4]+1));
tU.push_back(u.at(verts[4]+2));
tN.push_back(n.at(verts[5]));
tN.push_back(n.at(verts[5]+1));
tN.push_back(n.at(verts[5]+2));
//
tV.push_back(v.at(verts[6]));
tV.push_back(v.at(verts[6]+1));
tV.push_back(v.at(verts[6]+2));
tU.push_back(u.at(verts[7]));
tU.push_back(u.at(verts[7]+1));
tU.push_back(u.at(verts[7]+2));
tN.push_back(n.at(verts[8]));
tN.push_back(n.at(verts[8]+1));
tN.push_back(n.at(verts[8]+2));
答案 0 :(得分:10)
似乎使用>>
从istream进行解析并不像您期望的那样有效。您是否注意到在解析顶点,法线和uv时它会自动跳过空格?然而,对于面孔,你试图解析每个角色。您还需要非空格斜杠作为分隔符。
这种方法https://stackoverflow.com/a/1895584/642532怎么样?一个答案建议告诉流将逗号(在您的情况下为斜杠)视为空格,这样您就可以像顶点一样轻松地解析面。另一个答案建议使用字符串标记器,如StrTk http://www.partow.net/programming/strtk/index.html或Boost。