Sign in to follow this  
KodeNerd

Wavefront Obj Vertex Corruption [C++]

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 this post


Link to post
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.

[Edit]
Fixed a minor formatting problem.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this