Need help with my .OBJ loader...

Started by
8 comments, last by Ahl 12 years, 3 months ago
This project has been a bit of a labor of love. I'm trying to write an .obj loader that will handle any .obj file thrown at it and so far it's proven to be a challenge. Right now it seems to work with any triangle based models but any other model with polygons bigger (read more sides) than a triangle it's messing up. Now the following is the model I'm trying to load right now. It's been my test model as it has everything: Vertexes, normals, texcoords, textures, etc. This is what it's supposed to look like:

Aren_wheel_02.jpg94e6f1e5-c929-4ac0-b97c-3456a3e3f9c3Large.jpg

This is what I'm getting:

SideView.jpg
And:
TopDown.jpg

It's important to note that there are 3 meshes that make up this model. The first being the spokes, the second being the emblem in the middle of said spokes and he third being the rim pictured above. You can see a little from the top picture and much better in the second picture the disruption at one side with all the lines running through the middle to it. The general shape is there but it seems like there is one little bit that's just not working right.

Alright, onto the code. I hope someone can see some fault in my horribly ugly logic that I'm not seeing....be gentle.


else if (Buffer.substr(0, 1) == "f")
{
if (MatName == "")
{
std::stringstream ss;
ss << Mesh_Identifier++;
MatName = ss.str();
std::vector<boost::shared_ptr<vec3>> Vertex_Vec;
Vertex_Map_It = Vertex_Map.insert(std::pair<std::string, std::vector<boost::shared_ptr<vec3>>>(MatName, Vertex_Vec));
std::vector<boost::shared_ptr<vec3>> Texcoord_Vec;
Texcoord_Map_It = Texcoord_Map.insert(std::pair<std::string, std::vector<boost::shared_ptr<vec3>>>(MatName, Texcoord_Vec));
std::vector<boost::shared_ptr<vec3>> Normal_Vec;
Normal_Map_It = Normal_Map.insert(std::pair<std::string, std::vector<boost::shared_ptr<vec3>>>(MatName, Normal_Vec));
}
if (New_Mesh == true)
{
std::cout << "Face New_Mesh" << std::endl;
New_Mesh = false;
}
int iVertex;
int iTexcoord;
int iNormal;
int NumNodes = 0;
boost::shared_ptr<Node> NewNode;
boost::shared_ptr<Node> FirstNode;
boost::shared_ptr<Node> LastNode;

line >> line_type;
while (line.good())
{
NewNode.reset(new Node);
NumNodes++;
size_t found;
int NumSlashes = 0;
std::string SubLine;
line >> SubLine;
found = SubLine.find("//");
if (found != SubLine.npos)
{
SubLine.replace(SubLine.find("//"), 2, " ");
std::istringstream Values(SubLine);
Values >> iVertex >> iNormal;
NewNode->Vertex = Vertex_Vector[abs(iVertex)-1];
NewNode->Normal = Normal_Vector[abs(iNormal)-1];
NumSlashes = 999;
break;
}
else
{
while (true)
{
found = SubLine.find('/');
if (found != SubLine.npos)
{
NumSlashes += 1;
SubLine.replace(found, 1, " ");
}
else
{
break;
}
}
std::istringstream Values(SubLine);
if (NumSlashes == 0)
{
Values >> iVertex;
NewNode->Vertex = Vertex_Vector[abs(iVertex)-1];
}
if (NumSlashes == 1)
{
Values >> iVertex >> iTexcoord;
NewNode->Vertex = Vertex_Vector[abs(iVertex)-1];
NewNode->Texcoord = Texcoord_Vector[abs(iTexcoord)-1];
}
if (NumSlashes == 2)
{
Values >> iVertex >> iTexcoord >> iNormal;
NewNode->Vertex = Vertex_Vector[abs(iVertex)-1];
NewNode->Texcoord = Texcoord_Vector[abs(iTexcoord)-1];
NewNode->Normal = Normal_Vector[abs(iNormal)-1];
}
}
if (FirstNode == NULL)
{
FirstNode = NewNode;
}
if (LastNode == NULL)
{
LastNode = NewNode;
}
else
{
NewNode->PrevNode = LastNode;
NewNode->NextNode = FirstNode;
LastNode->NextNode = NewNode;
LastNode = NewNode;
FirstNode->PrevNode = NewNode;
}
}
//Make sure our Doubly Linked List is doubly linked all the way around.
for (boost::shared_ptr<Node> Temp = FirstNode; Temp->NextNode != FirstNode; Temp = Temp->NextNode)
{
Temp->NextNode->PrevNode = Temp;
}
if (NumNodes > 3)
{
boost::shared_ptr<Node> Triangle = FirstNode;
while(true)
{
if (Triangle->NextNode->NextNode->NextNode == Triangle)
{
if (Triangle->PrevNode->Vertex != NULL) Vertex_Map_It->second.push_back(Triangle->PrevNode->Vertex);
if (Triangle->PrevNode->Normal != NULL) Normal_Map_It->second.push_back(Triangle->PrevNode->Normal);
if (Triangle->PrevNode->Texcoord != NULL) Texcoord_Map_It->second.push_back(Triangle->PrevNode->Texcoord);
if (Triangle->Vertex != NULL) Vertex_Map_It->second.push_back(Triangle->Vertex);
if (Triangle->Normal != NULL) Normal_Map_It->second.push_back(Triangle->Normal);
if (Triangle->Texcoord != NULL) Texcoord_Map_It->second.push_back(Triangle->Texcoord);
if (Triangle->NextNode->Vertex != NULL) Vertex_Map_It->second.push_back(Triangle->NextNode->Vertex);
if (Triangle->NextNode->Normal != NULL) Normal_Map_It->second.push_back(Triangle->NextNode->Normal);
if (Triangle->NextNode->Texcoord != NULL) Texcoord_Map_It->second.push_back(Triangle->NextNode->Texcoord);
break;
}
bool Pure_Tri = true;
for (boost::shared_ptr<Node> Point = FirstNode->NextNode->NextNode; Point->NextNode != Triangle->PrevNode; Point = Point->NextNode)
{
vec3 v0 = *(Triangle->PrevNode->Vertex) - *(Triangle->Vertex);
vec3 v1 = *(Triangle->NextNode->Vertex) - *(Triangle->Vertex);
vec3 v2 = *(Point->Vertex) - *(Point->Vertex);
float dot00 = v0.DotProduct(v0);
float dot01 = v0.DotProduct(v1);
float dot02 = v0.DotProduct(v2);
float dot11 = v1.DotProduct(v1);
float dot12 = v1.DotProduct(v2);
float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
if (u >= 0 && v >= 0 && u + v < 1)
{
Pure_Tri = false;
break;
}
}
if (Pure_Tri == true)
{
if (Triangle->PrevNode->Vertex != NULL) Vertex_Map_It->second.push_back(Triangle->PrevNode->Vertex);
if (Triangle->PrevNode->Normal != NULL) Normal_Map_It->second.push_back(Triangle->PrevNode->Normal);
if (Triangle->PrevNode->Texcoord != NULL) Texcoord_Map_It->second.push_back(Triangle->PrevNode->Texcoord);
if (Triangle->Vertex != NULL) Vertex_Map_It->second.push_back(Triangle->Vertex);
if (Triangle->Normal != NULL) Normal_Map_It->second.push_back(Triangle->Normal);
if (Triangle->Texcoord != NULL) Texcoord_Map_It->second.push_back(Triangle->Texcoord);
if (Triangle->NextNode->Vertex != NULL) Vertex_Map_It->second.push_back(Triangle->NextNode->Vertex);
if (Triangle->NextNode->Normal != NULL) Normal_Map_It->second.push_back(Triangle->NextNode->Normal);
if (Triangle->NextNode->Texcoord != NULL) Texcoord_Map_It->second.push_back(Triangle->NextNode->Texcoord);
boost::shared_ptr<Node> Temp = Triangle->NextNode;
Triangle->PrevNode->NextNode = Triangle->NextNode;
Triangle->NextNode->PrevNode = Triangle->PrevNode;
Triangle = Temp;
}
else
{
Triangle = Triangle->NextNode;
}
}
}
else if (NumNodes == 3)
{
boost::shared_ptr<Node> Temp = FirstNode;
while(true)
{
if (Temp->Vertex != NULL) Vertex_Map_It->second.push_back(Temp->Vertex);
if (Temp->Normal != NULL) Normal_Map_It->second.push_back(Temp->Normal);
if (Temp->Texcoord != NULL) Texcoord_Map_It->second.push_back(Temp->Texcoord);
if (Temp->NextNode == FirstNode) break;
else Temp = Temp->NextNode;
}
}
else if (NumNodes < 3)
{
std::cout << "Less than 3 nodes!" << std::endl;
system("PAUSE");
}
}
Advertisement
I've only written .obj loader for handling 3 or 4-sided polygons, nothing more, but meshes with polygons more than four sides are rare so it has worked for me.

Here's what you have to do to handle 4-sided polygons as triangle lists. In your face-reading routine (that is, where you start adding nodes) count ahead to see if there are exactly 4 integer groups separated by spaces, that's when you know you have a 4 sided poly. Then read ahead and store these values into separate 4-sized arrays and add SIX vertices for the face, as two triangles resulting from splitting up the quad.

Basically what you have now is this:

for each line
- split line by spaces
- if line has 3 number groups
--- for each number group
----- add vertex index
----- add UV index
----- add normal index
--- form a triangle with the three groups of indices
- go to next line


You would want to change your method to something like this:

for each line
- if line has 3 number groups
--- use a polygon index array { 0, 1, 2 }
- if line has 4 number groups
--- use a polygon index array { 0, 2, 3, 1, 2, 3 } // this set of numbers will vary
- for each index in array
--- add vertex index for this number
--- add UV index for this number
--- add normal index for this number
--- if we are in the third or sixth index in the array
----- form a triangle with the three groups of indices
- go to next line


The reason why the set of numbers may vary for 4 sided polygons is because you have to know where the winding order is, and how the triangles should be faced.

If for instance we had a square with vertices 0, 1, 2, 3 starting from the top left and going clockwise, we can divide it in the diagonal from 0 to 2 to get 0, 2, 3, 1, 2, 3. But if we were to divide the quad along the 1-3 diagonal, then our list may be 0, 1, 3, 1, 2, 3. I had to guess which way was the right order for the quads, but you can get it in one or two tries if you know how the triangles should face.
Electronic Meteor - My experiences with XNA and game development
For debugging, a much smaller test case would be preferred. Can you export a single 4-sided poly, and does your code correctly read it in and turn it into triangles? If not, start there. In that case it's easy enough to understand what the results should be by hand, so stepping through your algorithm in a debugger should be much easier.

For debugging, a much smaller test case would be preferred. Can you export a single 4-sided poly, and does your code correctly read it in and turn it into triangles? If not, start there. In that case it's easy enough to understand what the results should be by hand, so stepping through your algorithm in a debugger should be much easier.


Yeah. I try the "ice skating uphill" approach to coding by default. One of many habits I need to break. I just generated a quad based cube in Blender and loaded that and something is definitely up.
I'm beginning to think that .OBJ is one giant whore of a modeling format. An example: The various meshes making up a given model are defined sequentially in the .obj file. In some models the vertex/texcoord/normal count is reset with each new mesh while in others that new data is added to the previous list with no indication of when either of these cases might be occurring. Bollux I say.

I'm trying to write an .obj loader that will handle any .obj file thrown at it...
I suppose you mean "will handle any polygonal .obj file thrown at it". The stuff revolving around curved surfaces is incredibly modern in my opinion.


I'm beginning to think that .OBJ is BAD.
It sure has several limitations and some quirks. Like most file formats. Managing this is relatively easy compared to other issues going around file formats. It is worth noticing that if polygon triangulation is a problem, it will likely be a problem with other formats as well. In the meanwhile, just hit the "export as triangles" option in the DCC tool and live happily with them.

Consider doing something else and save your time using AssImp.

Previously "Krohm"

Yeah, Assimp doesn't seem to work with MSVS 2010. Having to rebuild the .lib and it's not making the conversion.

Are there any other recommendable model loading libs out there?
BAH! NM I figured it out. Mixing up win32 and win64 .libs FTW!
If you are looking for a working example, the dx samples library has a very good .obj loader function. With a little effort you can make it working in your own project.
Already got it working. But thank you.

This topic is closed to new replies.

Advertisement