• 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
AK2012

Simple .obj loader (C++)

4 posts in this topic

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
0

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

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

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
0

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
1

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