Original Post
Hi.
I'm working on migrating to a newer version of OpenGL, because of this I'm reworking quite a lot of code.
The thing that's bugging out is my OBJ-loader.
The following function throws the "String subscript out of range" error message at the "while"-loop.
[source lang="cpp"]void Models::parseAndLoad(const char* filename){
std::ifstream myfile;
myfile.open(filename);
if(!myfile){
alert(L"No file!");
return;
}
std::string c;
std::vector expline;
std::vector expline_f;
float currentVertex;
int inFace;
while(getline(myfile, c)){
if(c[0] == 'v' && c[1] == ' '){
expline = explode(" ", c);
currentVertex = convertFromString(expline.at(2)); // first value
vertices.push_back(currentVertex);
currentVertex = convertFromString(expline.at(3)); // second value
vertices.push_back(currentVertex);
currentVertex = convertFromString(expline.at(4)); // third value
vertices.push_back(currentVertex);
}else if(c[0] == 'v' && c[1] == 't'){
expline = explode(" ", c);
currentVertex = convertFromString(expline.at(2)); // first value
texcoords.push_back(currentVertex);
currentVertex = convertFromString(expline.at(3)); // second value
texcoords.push_back(currentVertex);
currentVertex = convertFromString(expline.at(4)); // third value
texcoords.push_back(currentVertex);
}else if(c[0] == 'v' && c[1] == 'n'){
expline = explode(" ", c);
currentVertex = convertFromString(expline.at(2)); // first value
normals.push_back(currentVertex);
currentVertex = convertFromString(expline.at(3)); // second value
normals.push_back(currentVertex);
currentVertex = convertFromString(expline.at(4)); // third value
normals.push_back(currentVertex);
}else if(c[0] == 'f'){
expline = explode(" ", c); // splits the row into three parts that contain the face information
expline_f = explode("/", expline.at(1)); // splits the first row into vertex, texture and normal information
inFace = convertFromString(expline_f.at(0), 2);
vFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(1), 2);
tFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(2), 2);
nFaces.push_back(inFace-1);
expline_f = explode("/", expline.at(2)); // splits the second row as above
inFace = convertFromString(expline_f.at(0), 2);
vFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(1), 2);
tFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(2), 2);
nFaces.push_back(inFace-1);
expline_f = explode("/", expline.at(3)); // splits the third row
inFace = convertFromString(expline_f.at(0), 2);
vFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(1), 2);
tFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(2), 2);
nFaces.push_back(inFace-1);
}else{
// failsafe
continue; // do nothing
}
}
myfile.close();
initVBO();
}[/source]
In the above the first "subscript" error occurs at the declaration of the while loop (while(getline(myfile, c))).
I really don't know what the error message means. As I understand it, some people get it when trying to load an empty file. The file I'm using is not empty. I've checked
Also, the code above used to work in Visual C++ 2008 and a lower OpenGL version.
Thanks in advance,
Marcus
I'm working on migrating to a newer version of OpenGL, because of this I'm reworking quite a lot of code.
The thing that's bugging out is my OBJ-loader.
The following function throws the "String subscript out of range" error message at the "while"-loop.
[source lang="cpp"]void Models::parseAndLoad(const char* filename){
std::ifstream myfile;
myfile.open(filename);
if(!myfile){
alert(L"No file!");
return;
}
std::string c;
std::vector
std::vector
float currentVertex;
int inFace;
while(getline(myfile, c)){
if(c[0] == 'v' && c[1] == ' '){
expline = explode(" ", c);
currentVertex = convertFromString(expline.at(2)); // first value
vertices.push_back(currentVertex);
currentVertex = convertFromString(expline.at(3)); // second value
vertices.push_back(currentVertex);
currentVertex = convertFromString(expline.at(4)); // third value
vertices.push_back(currentVertex);
}else if(c[0] == 'v' && c[1] == 't'){
expline = explode(" ", c);
currentVertex = convertFromString(expline.at(2)); // first value
texcoords.push_back(currentVertex);
currentVertex = convertFromString(expline.at(3)); // second value
texcoords.push_back(currentVertex);
currentVertex = convertFromString(expline.at(4)); // third value
texcoords.push_back(currentVertex);
}else if(c[0] == 'v' && c[1] == 'n'){
expline = explode(" ", c);
currentVertex = convertFromString(expline.at(2)); // first value
normals.push_back(currentVertex);
currentVertex = convertFromString(expline.at(3)); // second value
normals.push_back(currentVertex);
currentVertex = convertFromString(expline.at(4)); // third value
normals.push_back(currentVertex);
}else if(c[0] == 'f'){
expline = explode(" ", c); // splits the row into three parts that contain the face information
expline_f = explode("/", expline.at(1)); // splits the first row into vertex, texture and normal information
inFace = convertFromString(expline_f.at(0), 2);
vFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(1), 2);
tFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(2), 2);
nFaces.push_back(inFace-1);
expline_f = explode("/", expline.at(2)); // splits the second row as above
inFace = convertFromString(expline_f.at(0), 2);
vFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(1), 2);
tFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(2), 2);
nFaces.push_back(inFace-1);
expline_f = explode("/", expline.at(3)); // splits the third row
inFace = convertFromString(expline_f.at(0), 2);
vFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(1), 2);
tFaces.push_back(inFace-1);
inFace = convertFromString(expline_f.at(2), 2);
nFaces.push_back(inFace-1);
}else{
// failsafe
continue; // do nothing
}
}
myfile.close();
initVBO();
}[/source]
In the above the first "subscript" error occurs at the declaration of the while loop (while(getline(myfile, c))).
I really don't know what the error message means. As I understand it, some people get it when trying to load an empty file. The file I'm using is not empty. I've checked
Also, the code above used to work in Visual C++ 2008 and a lower OpenGL version.
Thanks in advance,
Marcus