Simple .obj loader (C++)

Started by
3 comments, last by Cornstalks 11 years, 10 months ago
Hi everyone!

I've been searching on the web and forums for the last few days for a SIMPLE .obj loader for OpenGL in C++, and though there are several available out there (and even here), I'm not being lucky when it comes to finding a simple one (that allows textures as well).

As you can tell, I'm away from being an experienced programmer, and most of the codes I've found so far seem to have problems I am not being able to solve. I did got in touch with great examples (e.g. Assimp), but they are way too over the top for the simple solution I need (loading at least 3 .objs together) and got me lost.

This one, for example, (https://code.google.com/p/obj-mesh-libs/source/search?q=trunk&origq=trunk&btnG=Search+Trunk), compiled correctly to me, but I didn't find the right place to call for the Mesh * m = new WavefrontObject("MyObj.obj");, and therefore I couldn't get it to work too.

Do any of you know where I can find a suitable code for my case?

Appreciate any help,
AK
Advertisement
Do you understand the file format for OBJ files? If not, Wikipedia has a good article.

You haven't described the actual problem you're having (there's parsing the file, and then there's properly storing the parsed file in some data structure in your program, and then there's rendering this data structure), so I'm not sure what else to say. I don't think just giving you the code for my (simple) OBJ loader would do you any favors, though, if there's a conceptual hole in any of these aspects.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Hi,

I do understand the file format of OBJ files, and understand a little of how the parsing/storing/rendering works too. What I'm getting confused, actually, is in which part of a given code I should call my .obj models for them to appear on the screen. This, obviously, changes in each code, and that's where I'm getting stuck. None of the codes I've found have a documentation that I could understand.

Sorry if I'm confusing more the situation...

Just to clarify, this obviously is a college task, but my task is not to write my own obj loader. The task is to load several .objs from an existing loader, and then set camera views and mouse + keyboard interactions to each one of them. But since I'm stuck in the first part... =(

Btw, thank you for your answer!
The project "obj-mesh-libs" is not only a loader but comes with an example of use. Usually one integrates a loader into the own project, using the loader functionality and perhaps a wrapper to adapt the internal structure to the own needs. However, the example of use shows one way of ow to do so. Look into the lib's "Code" directory and there into the files Program.hpp and especially Program.cpp. The method "Program::LoadObject" does the actual loading. In the sample it is called from "Program::MenuCallback" where a file dialog is run to get the name and path of the OBJ to load.
Normally, you load things at first, then in your main event loop you update, and then render. This article is great for talking about how to separate updating and rendering (I didn't follow this article in the code below, but that's because I wanted to focus on other parts; in actual code I would follow that article). I wrote some small sample (pseudo)code that goes over the common flow of loading and displaying objects in a scene. Realize it's only pseudocode, and I wanted to focus on the flow of things rather than actual details (and hence, I simplified it a lot).

class Mesh
{
public:
// any needed public functions....

private:
vector<Vector3f> vertices;
vector<Vector3f> normals;
// other mesh data used in the actual OpenGL rendering call
};

class MeshLoader
{
public:
typedef Mesh type;

Mesh load(string path)
{
Mesh mesh;

// ...load the OBJ file into a Mesh object

return mesh;
}
};

template <typename T>
class ResourceCache
{
public:
const Resource& getResource(string id)
{
map<string, T::type>::const_iterator i = cache.find(id);
if (i == cache.end())
{
return cache[id] = loader.load(id);
}
else
{
return i->second;
}
}

private:
map<string, T::type> cache;
T loader;
};

class SceneObject
{
public:
SceneObject(string meshPath, ResourceCache<MeshLoader>& cache) :
mesh(cache.getResource(meshPath))
{
}

void render()
{
// Render the mesh... the vertex data is stored in the mesh,
// the object's position and rotation are stored in the
// transform. Now just call the OpenGL functions needed to
// draw this mesh with the given transform.
}

private:
const Mesh& mesh;
Matrix44f transform; // matrix transform
};

int main()
{
ResourceCache<MeshLoader> meshCache;
SceneObject tank1("tank.obj", meshCache);
SceneObject tank2("tank.obj", meshCache);
SceneObject person("person.obj", meshCache);

Window window; // some window for you to use
while (window.isOpen())
{
update(); // update the program (handle input, handle physics, etc.)

// and now render everything
tank1.render();
tank2.render();
person.render();
}
}


[edit]

Sorry, GameDev's friggin' retarded posting system mangled some of my formatting. Gosh I hate this editor.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement