• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ahl

Need help with my .OBJ loader...

9 posts in this topic

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]
0

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.
2

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.
1

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.
0

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.
0

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
0

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?
0

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.
0

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  
Followers 0