I'm trying to write a reader for Wavefront .OBJ files so I can draw meshes in OpenGL. To do this, I've tried to accumulate some basic C++ file IO knowledge since I mostly know java. I understand from this questions answer that >>
can be used to read in data from the stream.
My strategy for reading my OBJ file is too first read in the char which identifies what the next pieces of data are, then keep trying to read in the numbers until I can't anymore. Then I try to read in the next char. My code is as follows:
std::ifstream objReader;
objReader.open("resources//file.obj");
char m;
int vindex = 0;
int iindex = 0;
while (objReader >> m) {
std::cout << "here" << std::endl;
if (m == 'v') {
GLfloat cv;
while (objReader >> cv) {
objData[vindex] = cv; //obj data is my verticies array to be drawn
std::cout << objData[vindex] << std::endl;
vindex++;
}
}
else if (m == 'f') {
GLuint ci;
while (objReader >> ci) {
indicies[iindex] = ci; //indicies is my EBO array
std::cout << indicies[iindex] << std::endl;
iindex++;
}
}
}
The file data I'm using can be found here.
Now when I run this code, it opens up the file fine and it sucesfully reads in the first line. It indetifies the marker as the char v
then it stores the following 3 floats into my array. The problem is, it just ends there. The loop breaks and it never even continues to the second line. Somehow, it can no longer find any other chars in the file. Why does this happen and how can I fix it?
Thanks!