Sign in to follow this  

Need help with my .OBJ loader...

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[img]http://files.turbosquid.com/Preview/2011/03/08__18_36_29/Aren_wheel_02.jpg94e6f1e5-c929-4ac0-b97c-3456a3e3f9c3Large.jpg[/img]

This is what I'm getting:

[img]http://i298.photobucket.com/albums/mm265/ahlywog/SideView.jpg[/img]
And:
[img]http://i298.photobucket.com/albums/mm265/ahlywog/TopDown.jpg[/img]

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.

[CODE]
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");
}
}
[/CODE]

Share this post


Link to post
Share on other sites
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:

[code]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[/code]

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

[code]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[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='rdragon1' timestamp='1325293841' post='4898321']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Ahl' timestamp='1325291243' post='4898304']
I'm trying to write an .obj loader that will handle any .obj file thrown at it...[/quote]I suppose you mean "will handle any [u]polygonal [/u].obj file thrown at it". The stuff revolving around curved surfaces is incredibly modern in my opinion.

[quote name='Ahl' timestamp='1325305565' post='4898359']
I'm beginning to think that .OBJ is BAD.[/quote]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 [url="http://assimp.sourceforge.net/"]AssImp[/url]. Edited by Krohm

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
BAH! NM I figured it out. Mixing up win32 and win64 .libs FTW!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this