Currently I am trying to render a .obj model that I loaded into vectors. I am trying to pull the Vector3D object out of the array but it gives me an out of range error. It only goes to five before the array crashes, when the array has 12 objects for example. Here is the code for the rendering.
glBegin(GL_TRIANGLE_STRIP);
for (int indx = 0; indx < mv3_faces.size(); ++indx)
{
if (mb_print_once)
{
std::cout << "Rendering Loop....Faces at 8: " << mv3_faces.at(5).x << " Current Index: " << indx << std::endl;
std::cout << "Rendering Loop X: " << mv3_faces.at(indx).x << " Y: " << mv3_faces.at(indx).y << " Z: " << mv3_faces.at(indx).z << std::endl;
}
glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
glVertex3f(mv3_vertices.at(mv3_faces.at(indx).x).x, mv3_vertices.at(mv3_faces.at(indx).y).y, mv3_vertices.at(mv3_faces.at(indx).z).z);
}
mb_print_once = false;
glEnd();
Ignore the mb_print_once… that is for debugging purposes.
Did you remember that vertex indices in .OBJ files start at 1 rather than 0? So you have to decrement the vertex indices by 1 after reading them from an “f” tag.
And also as a little comment, I would strongly suggest using
[]instead of.at, although in this example it was a good idea, so you got the exception. But in general the range-checking overhead is not worth it, especially in such a highly to optimize loop.