Sign in to follow this  

Simple .obj loader (C++)

This topic is 2037 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

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 [i](loading at least 3 .objs together)[/i] and got me lost.

This one, for example, ([url="https://code.google.com/p/obj-mesh-libs/source/search?q=trunk&origq=trunk&btnG=Search+Trunk"]https://code.google.com/p/obj-mesh-libs/source/search?q=trunk&origq=trunk&btnG=Search+Trunk[/url]), compiled correctly to me, but I didn't find the right place to call for the [i]Mesh * m = new WavefrontObject("MyObj.obj");[/i], 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

Share this post


Link to post
Share on other sites
Do you understand the file format for OBJ files? If not, [url="http://en.wikipedia.org/wiki/Wavefront_.obj_file"]Wikipedia has a good article[/url].

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.

Share this post


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

Share this post


Link to post
Share on other sites
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 [url="https://code.google.com/p/obj-mesh-libs/source/browse/trunk/OBJ+Loader+CG+2010/Code/Program.hpp"]Program.hpp[/url] and especially [url="https://code.google.com/p/obj-mesh-libs/source/browse/trunk/OBJ%20Loader%20CG%202010/Code/Program.cpp"]Program.cpp[/url]. 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. Edited by haegarr

Share this post


Link to post
Share on other sites
Normally, you load things at first, then in your main event loop you update, and then render. [url="http://gafferongames.com/game-physics/fix-your-timestep/"]This article[/url] 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. [i]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).[/i]

[code]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();
}
}[/code]

[edit]

Sorry, GameDev's friggin' retarded posting system mangled some of my formatting. Gosh I hate this editor. Edited by Cornstalks

Share this post


Link to post
Share on other sites

This topic is 2037 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.

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