我的下标超出了范围错误。 这是它发生的功能:
//Load Object Function
int loadObejct(const char *fileName)
{
cout<<"soos";
vector<string*> coord;
vector<coordinate*> vertex;
vector<face*> faces;
vector<coordinate*> normals;
ifstream in(fileName);
if(!in.is_open())
{
cout<<"no file";
return 5;
}
char buf[400];
while(!in.eof())
{
in.getline(buf, 400);
coord.push_back(new string(buf));
cout<<"loading";
}
////////////////////////////////////////////where error starts
for(int i = 0; i<coord.size(); i++)
{
if((*coord[i])[0] =='#' || (*coord[i])[0] == 'g')
{
continue;
}
else if((*coord[i])[0]=='v' && (*coord[i])[1]==' ' )
{
float tmpx, tmpy, tmpz;
sscanf(coord[i]->c_str(), "v %f %f %f", &tmpx, &tmpy, &tmpz);
vertex.push_back(new coordinate(tmpx, tmpy, tmpz));
}else if((*coord[i])[0]=='v' && (*coord[i])[1]=='n' )
{
float tmpx, tmpy, tmpz;
sscanf(coord[i]->c_str(), "vn %f %f %f", &tmpx, &tmpy, &tmpz);
normals.push_back(new coordinate(tmpx, tmpy, tmpz));
}else if((*coord[i])[0]=='f')
{
int a, b, c, d, e;
if(count(coord[i]->begin(), coord[i]->end(), ' ') == 4)
{
sscanf(coord[i]->c_str(), "f %d//%d %d//%d %d//%d %d//%d", &a, &b, &c, &b, &d, &b, &e, &b);
faces.push_back(new face(b, a, c, d, e));
}else
{
sscanf(coord[i]->c_str(), "f %d//%d %d//%d %d//%d", &a, &b, &c, &b, &d, &b);
faces.push_back(new face(b, a, c, d));
}
}
}///////////////////////////////where error ends
//draw
int num;
num = GL::GenLists(1);
GL::NewList(num, ListMode::Compile);
for(int i = 0; i<faces.size(); i++)
{
if(faces[i]->four == true)
{
GL::Begin(BeginMode::Quads);
GL::Normal3(normals[faces[i]->faceNum-1]->x, normals[faces[i]->faceNum-1]->y, normals[faces[i]->faceNum-1]->z);
GL::Vertex3(vertex[faces[i]->faces[0]-1]->x, vertex[faces[i]->faces[0]-1]->y, vertex[faces[i]->faces[0]-1]->z);
GL::Vertex3(vertex[faces[i]->faces[1]-1]->x, vertex[faces[i]->faces[1]-1]->y, vertex[faces[i]->faces[1]-1]->z);
GL::Vertex3(vertex[faces[i]->faces[2]-1]->x, vertex[faces[i]->faces[2]-1]->y, vertex[faces[i]->faces[2]-1]->z);
GL::Vertex3(vertex[faces[i]->faces[3]-1]->x, vertex[faces[i]->faces[3]-1]->y, vertex[faces[i]->faces[3]-1]->z);
GL::End();
}else
{
GL::Begin(BeginMode::Triangles);
GL::Normal3(normals[faces[i]->faceNum-1]->x, normals[faces[i]->faceNum-1]->y, normals[faces[i]->faceNum-1]->z);
GL::Vertex3(vertex[faces[i]->faces[0]-1]->x, vertex[faces[i]->faces[0]-1]->y, vertex[faces[i]->faces[0]-1]->z);
GL::Vertex3(vertex[faces[i]->faces[1]-1]->x, vertex[faces[i]->faces[1]-1]->y, vertex[faces[i]->faces[1]-1]->z);
GL::Vertex3(vertex[faces[i]->faces[2]-1]->x, vertex[faces[i]->faces[2]-1]->y, vertex[faces[i]->faces[2]-1]->z);
GL::End();
}
}
GL::EndList();
//end draw
for(int i = 0; i<coord.size(); i++)
{
delete coord[i];
}
for(int i = 0; i<faces.size(); i++)
{
delete normals[i];
}
for(int i = 0; i<normals.size(); i++)
{
delete faces[i];
}
for(int i = 0; i<vertex.size(); i++)
{
delete vertex[i];
}
return 6;
}
我标记了将其缩小到问题发生位置的区域。 我的编码是VS2012和c ++ / cli。 如您所见,我正在尝试从.obj文件加载数据以呈现3D模型。 这不是完整的计划。 如果你看到我遗漏的东西,请告诉我。 : - )