3D models with C++ OpenGL

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Oct 24, 2009 at 03:47

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

36 Replies

Please log in or register to post a reply.

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Oct 24, 2009 at 04:10

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.

36b416ed76cbaff49c8f6b7511458883
0
poita 101 Oct 24, 2009 at 04:39

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

Ceee4d1295c32a0c1c08a9eae8c9459d
0
v71 105 Oct 24, 2009 at 08:49

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 details or
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.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Oct 24, 2009 at 17:53

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 Settings\Luke\Desktop\Class_openGL_3dsbmp\Debug\Class_openGL.exe 1

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.

B20d81438814b6ba7da7ff8eb502d039
0
Vilem_Otte 117 Oct 25, 2009 at 14:59

Looks like you forgot to include some headers …

36b416ed76cbaff49c8f6b7511458883
0
poita 101 Oct 25, 2009 at 15:25

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 some point).

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Oct 25, 2009 at 17:33

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.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Oct 27, 2009 at 18:09

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.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 03, 2009 at 03:08

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?

9275cef0ad2f15ec1813d63b0c5b0fad
0
rogerdv 101 Nov 03, 2009 at 18:48

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.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Nov 03, 2009 at 19:30

The OBJ format doesn’t support animations anyway.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 04, 2009 at 02:25

is it similar to how md2 animates?

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 04, 2009 at 07:28

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.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 04, 2009 at 12:42

What should I do to animate my obj models? I read about cal3D in another thread on this site.

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 04, 2009 at 13:02

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.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 04, 2009 at 14:15

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?

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 04, 2009 at 15:15

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

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 04, 2009 at 15:36

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 compatible too.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 04, 2009 at 15:46

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.

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 04, 2009 at 20:22

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 ;)

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 04, 2009 at 23:11

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

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 04, 2009 at 23:23

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.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 05, 2009 at 00:50

I have been trying to use the Collada DOM but the includes are completely wonky. In my searching for FCollada I found this post.

http://dmaggot.wordpress.com/2009/09/09/collada-nightmare/

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.

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 05, 2009 at 01:23

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 basically:

#include "core_engine/mesh.h"
#include "core/w32_fsys.h"
using namespace pfc;

int main(int argc_, const char *argv_[])
{
  w32_file_system fs(true);
  owner_ptr<mesh> m=load_mesh("test.dae");
  return 0;
}

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.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 05, 2009 at 01:37

Can I just Google for those header files or would you send them to me so I can try and test it?

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 05, 2009 at 01:45

You can get it from Spin-X Engine SourceForge SVN. If you don’t want setup SVN, just download the GNU tarball of the project. Read docs/quick_start.txt for instructions how to get started.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 05, 2009 at 01:56

I looked at quick_start and it says it needs the direct x sdk?

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Nov 05, 2009 at 04:46

Cal3d is a good choice. You can get a Blender plugin here:

http://www-users.cs.umn.edu/\~mein/blender/plugins/python/import_export/blend2cal3d/index.html

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.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 05, 2009 at 05:17

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 welcome! :)

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 05, 2009 at 11:50

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

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 05, 2009 at 13:37

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 to read.

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 05, 2009 at 13:51

Good luck! (: Let us know how it turns out.

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 05, 2009 at 13:58

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.

46407cc1bdfbd2db4f6e8876d74f990a
0
Kenneth_Gorking 101 Nov 05, 2009 at 14:43

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

3f2682fa420fe7a245f9d9cd679472da
0
Xenomorph05 101 Nov 05, 2009 at 18:16

Thanks! i am checking out ASSIMP and the Boost libraries. I hope that this can make my Collada using like a dream.

Fe8a5d0ee91f9db7f5b82b8fd4a4e1e6
0
JarkkoL 102 Nov 09, 2009 at 21:10

Aah, just got the Collada animation to work in SXE (: Any luck with Assimp?