# Wavefront Obj Vertex Corruption [C++]

This topic is 3358 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I recently finished a simple .obj file loader and was testing it out using OpenGLs immediate mode rendering. All of the vertices are read correctly from the file and are correct in the std::vector but when I go to render when iterating over the std::vector the last vertex is always some extremely large or small number (2e38 or slightly less). It makes part of the model either disappears or extend to infinity. Does anyone know what could be happening?

##### Share on other sites
Quote:
 Original post by KodeNerdDoes anyone know what could be happening?
No, because you didn't post any code.

##### Share on other sites
I knew I forgot something.

This is the part where I load the vertices into the std::vector
			float x;			float y;			float z;			file >> x;			file >> y;			file >> z;			std::cout << "Reading a vertex" << std::endl;			std::cout << object.vertices.size() << std::endl;			std::cout << x << "," << y << "," << z << std::endl;			std::cin.get();			object.vertices.push_back(Vector(x,y,z));

Note that "Vector" is a structure with a constructor that accepts three floats and puts them into an array of three floats.

I also use this to put it into a std::map (note that the "object" object is of class Model::Obj
obj_cache.insert(std::make_pair(filename,object));//filename is a string and the obj_cache is of declared as std::map obj_cache<std::string,Model::Obj>

This is the section I use to render the object. The inside of the "Vector" object is an array called vector declared as "float vector[3]". Note that the vertices variable is incremented but cannot be shown for some reason. Also note that polygons is an std::vector that holds the vertex indices for each face.
for(unsigned int poly = 0;object.polygons.size();vertices++) {				glVertex3fv(object.vertices[object.polygons[poly]].vector);			}

The function that the object is rendered in is a const function and the values are never changed between the two sections of code listed.

Fixed a minor formatting problem.

[Edited by - KodeNerd on December 15, 2008 7:18:37 PM]

##### Share on other sites
Just to clear something up, whats going on with "vertices" being incremented in that last code snippit, is that a formatting error?

##### Share on other sites
Try this just to see what happens

for(unsigned int poly = 0; object.polygons.size(); ++poly){  int index = object.polygons[poly];  const Vector& vr = object.vertices[index];  glVertex3fv(vr.vector);}

##### Share on other sites
That was a copy/paste error. Sorry about that. Fixing it now.

##### Share on other sites
Alright, but why are you incrementing vertices along polygons valid range with the poly variable?
What that does is loop the length of polygons, while incrementing vertices, which im not sure where vertices is coming from, and poly is always gonna be zero, because you never change it.

##### Share on other sites
I actually found the problem.

I took the advice from NumberXareo and used his snippet. When that still didn't work I used the debugger with his snippet and found out that the faces have indices for the vertices that start from 1. I just subtracted one from the index and it worked perfectly.

##### Share on other sites
Yeah thats actually part of the obj spec, indices start at 1 not 0.