Jump to content


3D models with C++ OpenGL


36 replies to this topic

#1 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 24 October 2009 - 03:47 AM

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.

#2 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1590 posts

Posted 24 October 2009 - 04:10 AM

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.
Currently using Blender and Unity.

#3 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 322 posts

Posted 24 October 2009 - 04:39 AM

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?

#4 v71

    Valued Member

  • Members
  • PipPipPipPip
  • 355 posts

Posted 24 October 2009 - 08:49 AM

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.

#5 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 24 October 2009 - 05:53 PM

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.

#6 Vilem Otte

    Valued Member

  • Members
  • PipPipPipPip
  • 345 posts

Posted 25 October 2009 - 02:59 PM

Looks like you forgot to include some headers ...
My blog about game development (and not just game development) - http://gameprogramme...y.blogspot.com/

If you don't know how to speed up application, go "roarrrrrr!", hit the compiler with the club and use -O3 :D

#7 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 322 posts

Posted 25 October 2009 - 03:25 PM

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

#8 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1590 posts

Posted 25 October 2009 - 05:33 PM

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.
Currently using Blender and Unity.

#9 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 27 October 2009 - 06:09 PM

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.

#10 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 03 November 2009 - 03:08 AM

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?

#11 rogerdv

    Member

  • Members
  • PipPip
  • 99 posts

Posted 03 November 2009 - 06:48 PM

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.

#12 Reedbeta

    DevMaster Staff

  • Administrators
  • 5310 posts
  • LocationSanta Clara, CA

Posted 03 November 2009 - 07:30 PM

The OBJ format doesn't support animations anyway.
reedbeta.com - developer blog, OpenGL demos, and other projects

#13 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 04 November 2009 - 02:25 AM

is it similar to how md2 animates?

#14 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 475 posts

Posted 04 November 2009 - 07:28 AM

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.

#15 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 04 November 2009 - 12:42 PM

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

#16 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 475 posts

Posted 04 November 2009 - 01:02 PM

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.

#17 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 04 November 2009 - 02:15 PM

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?

#18 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 475 posts

Posted 04 November 2009 - 03:15 PM

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.

#19 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 04 November 2009 - 03:36 PM

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.

#20 Xenomorph05

    New Member

  • Members
  • PipPip
  • 17 posts

Posted 04 November 2009 - 03:46 PM

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users