I have been surfing the corners of the internet for code to load and
view md2, 3ds, obj files in a simple OpenGL program but I have run into
issues with all of them.
I have been trying to learn from the VideoTutorialRocks website but I
find it doesn’t teach everything I think I need to know. I don’t like
Nehe because they use glaux…
I want to learn from scratch how to build my own 3D model loader,
texture loader and viewers but I do not know how I should attempt to do
this. I do know that I need information about the file types I want to
use but beyond that and trying to code it is puzzling me. I do think it
is beyond my current c++ level but I need to learn somehow.
Also right now I am using VS C++ 2008 EE but I will be trying to do my
programming on the new Ubuntu 9.10 so I want to keep my code
multi-platform compatible as much as possible.
Thanks a lot for taking the time to read this and for any help you
provide! I really want to develop FOSS but I need to get the skills
Please log in or register to post a reply.
A good one to start with is an object file: .obj. It’s a text file so
you can open it up and it’s almost self explanatory showing faces and
vertices, etc. Just download Blender and save a cube in obj format. It
will be easy to tell whats going on since it’s only a cube. Then texture
it and work on applying the texture. BTW, don’t try an md2 in Blender
because it messes up the format. If you go to the bottom of the page on
Nehe’s tutorials there are examples saved in all kinds of formats, etc.
There should be one without glaux. Anyway, you can just insert the
opengl code in any of them. You could also look at the Irrlicht source
code. It runs in opengl and has a lot of model loaders in it. Someone on
the irrlicht site will probably help you locate it if you can’t find it.
For animated models there’s also Cal3d. It’s opensource and has a model
viewer that comes with it. It’s a huge amount of work doing a skeletal
animated model loader so you might want to think about using someone
else’s code at that point like Cal3d or a DirectX loader or something.
There are some md2 loader tutorials on the net for opengl, though, that
aren’t too bad.
If you’re wondering how to parse a file, do a Google search for
recursive decent parser, or packrat parsers. Those are fairly simply to
use, and fairly standard too.
Is there any particular part of the problem where you are getting
I think i can help you since i wrote such kind of application but
posting the code in a forum would be useless, contact me if you want
source code fragments, i am also improving and adding details to my
model loader / optimizer.
May i give you and advice ? first of all build a robust mesh data
structure to hold the ‘parsed’ file data , then you can specialize ( sp
?? ) when you have to load different file formats, same story for
textures, many models include bmp or jpg texturs, so you need to add
different image loaders for every different model.
Thanks for the replies and I will try to attempt the things you guys
have suggested but it will take me a day or two to check it all out.
poita: The main issue I have had while trying to use any of the sample
code for importing models and textures is - LNK2019.
error LNK2019: unresolved external symbol “int __cdecl LoadBitmap(char
*)” (?LoadBitmap@@YAHPAD@Z) referenced in function “public: void
__thiscall Game::init(void)” (?init@Game@@QAEXXZ) Game.obj
fatal error LNK1120: 1 unresolved externals C:\Documents and
These errors happen with my obj, md2 and attempt to us 3ds. If you want
to see my code I can try to send it to you in a bit.
Looks like you forgot to include some headers …
Lack of header files doesn’t cause linker errors. Linker errors are
caused by lack of linking.
Is LoadBitmap a windows function? You’ll need to link the windows PSDK
if it isn’t done automatically (which I know VC++ Express didn’t do at
Probably the examples that you are using have a dependency to some type
of image library that isn’t linked. Go to wherever you downloaded and
see if there are any dependencies listed.
Thanks for the ideas and tips! I decided to rebuild the project and for
what ever reason I didn’t get the same problems. I guess I posted before
I played with it enough. I’ll make sure to have tried everything next
time. The loading I have working is an obj and blender obj export works
just fine with it.
I was working with Visual Studio 2008 on Windows but I am now switching
to using NetBeans for my C++ openGL programming. I have tried all of the
obj loaders I found but they have to much Windows dependent includes.
edit: I didn’t realize I had to include memory.h lol so I now have my
obj loader working with netbeans on Ubuntu. Does anyone know how
animating works with c++ and obj files?
Animation does not “works” with C++. You have to study abot animation
types (keyframe, skeletal) and see what method uses the file format you
are loading. It takes some heavy maths.
The OBJ format doesn’t support animations anyway.
is it similar to how md2 animates?
Writing mesh loaders for various formats is quite a lot of work. I have
written loaders for 3ds, lwo, obj and dae (Collada), which’s around 3000
lines of code total using comprehensive infrastructure, and I don’t
claim they are complete either (: Generally the loading is split into
two phases: 1) parsing the data to data structures which closely
resemble the file format, and 2) generating the GPU friendly data from
that data to platform agnostic data structure (mainly means
triangulation, proper vertex duplication and normal/tangent/binormal
generation for lighting). Once the data is loaded to the platform
agnostic data structure, it’s quite trivial to generate OpenGL/D3D
version of it.
For loading skeletal animation data, it’s also a lot of work. I’m in the
finishing phases of writing a Collada animation loader and that’s around
1000 lines of code alone. However, plain animation sequence loader is
pretty poor system alone and you need few other components for
comprehensive skeletal animation system.
What should I do to animate my obj models? I read about cal3D in another
thread on this site.
You can’t animate obj models like Reedbeta said. The format has no
skinning or bone information.
Edit: Just for clarification, I mean obj format doesn’t support skeletal
animation for characters. You can of course animate rigid body objects
with physics for example.
I understand that the obj does not hold animation information so I was
trying to ask how do I animate the obj model?
I can rig the model in a 3D program but how can I make use of that? Do I
need to load the bones as parent/child 3D models inside the obj
character model to make it animate?
You don’t (: OBJ format doesn’t contain needed information to be
animated, so you need to use a format that does (like Collada). What you
need is the bone hierarchy (skeleton) stored into the mesh file and
information about the bone influences to the mesh vertices. OBJ format
doesn’t support this information thus it’s not suitable for character
animation. Collada can hold both skinning info/skeleton and
I’m checking out Google results for Collada. I see it is compatible with
Blender. I presume I use a Collada file rather then an obj.
What other model file formats support animations? or should I just
Google that too? I want to make sure that this is all OpenGL and Blender
I found that Blender exports to Collada 1.4 .dae files. This is great if
I can use this for animating, texture and my model.
I have a new issue. I have to get a new model loader for .dae now.
FBX is another good alternative and AFAIK supports skinning, animations,
etc. (it’s used by Autodesk as interchange format between Maya, 3DS Max,
etc.) because it’s widely supported. It’s proprietary format from
Autodesk though and you have to my knowledge use FBX SDK for loading.
You could always save some trouble and use Spin-X Engine which has
Collada loader implemented and just write OpenGL renderer for it, hehe
I am not skilled enough to write a renderer for OpenGL.
Right now I am currently looking at the Collada DOM viewer and seeing if
I can take from it what I need to load and draw a model.
I know fbx can be used with XNA..
I was hoping there would be a loader for dae like there is for obj or
There is FCollada library at least and FBX SDK can also load Collada
files. I have no experience in using either, so I don’t know how usable
those libraries are.
I have been trying to use the Collada DOM but the includes are
completely wonky. In my searching for FCollada I found this post.
It seems Collada is a “nightmare” to get working. :(
I really wanted to use it. I even downloaded the Demo from codeProject
but I ran into issues trying to even compile it.
I have spent days trying to get OBJ to work, and it does but I can’t
animate with it. Now I try a newer format and it seems to be ridiculous.
I will try to see about fbx on opengl but if anyone has other tips for
Collada I will keep trying it.
Well, you could try to use my mesh class for loading, because I know
that project compiles at least under MSVC 2008 ;) What you would do is
using namespace pfc;
int main(int argc_, const char *argv_)
The mesh class contains all the data in platform agnostic format.
However, I haven’t done large amount of testing with it and the
animation loading isn’t done yet.
Can I just Google for those header files or would you send them to me so
I can try and test it?
You can get it from Spin-X Engine SourceForge
SVN. If you
don’t want setup SVN, just download the GNU
of the project. Read docs/quick_start.txt for instructions how to get
I looked at quick_start and it says it needs the direct x sdk?
Cal3d is a good choice. You can get a Blender plugin here:
It’s open source and doesn’t have any windows files in the source code.
Pretty good documentation, I think. You just drop the py file in the
plugin directory of Blender and it will show up in the menu.
I will check more about cal3D tomorrow and I am going to try some more
with Collada. Thanks for the tips. I appreciate them all and more are
Yes, it needs DirectX SDK if you compile the entire solution with the
renderer (graphics library). You just need core and core_engine
libraries though for loading meshes, which has no DirectX
I think I am going to try a different method to use Collada files. I
will try the DOM again and there is a tutorial on the files that I need
Good luck! (: Let us know how it turns out.
Thanks! I hope I can put in enough hours to get this working. I did find
someone on the Collada forums who has started to work on this already so
I am hoping we can work on this together.
You should check out Assimp before
trying to write your own parser (especially since you’re on a time
table). It also supports alot of
Thanks! i am checking out ASSIMP and the Boost libraries. I hope that
this can make my Collada using like a dream.
Aah, just got the Collada animation to work in SXE (: Any luck with