How to create a Code::Blocks project which can load Collada 3D models from Blender into OpenGL using C++ and the Collada DOM on Debian Lenny



After learning some C++ and a bit of OpenGL, I realized that without some sort of 3D model loader, I'd have to map my polygons point-by-point using floating point numbers. That seemed next to impossible as I'm not a math genius. I wanted to be able to create a model in Blender, and display it in an OpenGL C++ program. I had two options, either I could try to reinvent the wheel by building my own loader (which would be hard since I'm new to C++ and would probably turn out terrible), or I could use an existing API which has been created by experts. I decided the latter would be a better idea, and I chose to use the Collada format because it seemed modern and popular enough that it should be around for a while. The API I chose was the Collada DOM because I couldn't find anything else. Unfortunately, getting the Collada DOM running was challenging, but I figured out a way that works (mostly). I documented the process so that anyone else who wants to create 3D models in a program like Blender, and use them in a C++ program, won't have to figure everything out themselves the way I had to. Please let me know if I've missed any dependencies, and if you think anything could be done in a better way, please tell me how. I'm not an expert on these topics, just someone who figured out how to make something work that really should have been a lot easier than it was.

When you finish the tutorial, you will have a Code::Blocks project which can build the example viewer program that comes with the Collada DOM. With that, you can see how to use the Collada DOM to load Collada models into OpenGL. Once you have seen the Collada DOM API in action, you will be able to use that knowledge along with the Code::Blocks IDE to make your own OpenGL program that uses Collada models.

Here's how to do it:
  1. Install dependencies and get the Collada DOM:

  2. Download bullet version 2.73:

  3. Unpack and compile bullet:

  4. Backup Collada DOM default bullet libs (which don't work):

  5. Copy new bullet libs to Collada DOM include folder, and fix lib names so Collada DOM will compile:

  6. Back up old bullet headers and copy new ones to Collada DOM include folder:

  7. Compile Collada DOM:

  8. Install Code::Blocks IDE if you don't already have it:

  9. Make a Code::Blocks project for the viewer application:

    Make a new empty project in Code::Blocks, then create a new C++ source file.
    Copy the contents of ~/svn/collada-dom/viewer/projects/linux/main.cpp into your new file.
    Click "Project --> Build options...".
    In the new window, on the upper left corner, click the name of your project (above the word Debug).
    Click the "Linker settings" tab. Under "Other linker options:" enter the following (substituting your_username for your actual username):

    -lxml2
    -L/home/your_username/svn/collada-dom/dom/external-libs/tinyxml/lib/linux/libtinyxml.a
    -lpcre
    -lpcrecpp
    -lboost_filesystem
    -L/home/your_username/svn/collada-dom/dom/build/linux-1.4/
    -lcollada14dom
    -lminizip
    -lz
    -L/home/your_username/svn/collada-dom/rt/build/linux-1.4/
    -lcollada14rt
    -L/home/your_username/svn/collada-dom/fx/build/linux-1.4/
    -lcollada14fx
    -L/home/your_username/svn/collada-dom/rt/external-libs/bullet/lib/linux/
    -lbulletopenglsupport
    -lbulletdynamics
    -lbulletcollision
    -lbulletmath
    -lGL
    -lGLU
    -lglut
    -lCg
    -lCgGL
    -lpthread

  10. Click the "Search directories" tab. Add the following entries under the "Compiler" tab:

    /usr/include/libxml2
    /home/your_username/svn/collada-dom/dom/external-libs/tinyxml
    /home/your_username/svn/collada-dom/dom/external-libs/minizip/include
    /home/your_username/svn/collada-dom/dom/include
    /home/your_username/svn/collada-dom/dom/include/dae
    /home/your_username/svn/collada-dom/dom/include/1.4
    /home/your_username/svn/collada-dom/fx/include
    /home/your_username/svn/collada-dom/rt/include
    /home/your_username/svn/collada-dom/rt/external-libs/bullet/include
    /home/your_username/svn/collada-dom/rt/external-libs/bullet
    /usr/include/GL
    /usr/include/Cg

    Make sure to substitute "your_username" for your actual username, like you did before.


  11. Click the "Linker" tab (not "Linker settings"). Add the following entries:

    /home/your_username/svn/collada-dom/dom/build/linux-1.4
    /home/your_username/svn/collada-dom/rt/build/linux-1.4
    /home/your_username/svn/collada-dom/fx/build/linux-1.4

    Click OK. Click "File --> Save project".
    Hit F9 to build and run the project and it will take you to an error in the file cfxPlatform.h.
    You need to change #include CFX_PLATFORM_INCLUDE to read:

    #include "/home/your_username/svn/collada-dom/fx/include/cfxLinux.h"

    Now click "File --> Save Everything".


  12. Before building, copy some files to your project directory so your new program runs properly:

  13. Now build and run your Code::Blocks project. If all went according to plan, the program should build successfully, and you should see it in action displaying the cage demo file.


  14. For some strange reason, the program can't find libcollada14fx.so even when it's in the same directory as the binary, so you need to write a simple wrapper script (a.k.a. launcher) which sets the global variable LD_LIBRARY_PATH to the directory where the shared library lives. Seeing as we're already setting LD_LIBRARY_PATH for the wrong reasons, until there is a fix for this, we may as well set it for all the Collada DOM libs. That way we don't have to copy them anywhere special. Create a new file in the Code::Blocks build directory:

  15. Enter the following into the new file:

    #!/bin/bash export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/your_username/svn/collada-dom/dom/build/linux-1.4/:/home/your_username/svn/collada-dom/fx/build/linux-1.4/:/home/your_username/svn/collada-dom/rt/build/linux-1.4/ ./your_program_bin $@

    Make sure you substitute "your_username" for your system username, and "your_program_bin" for the filename of the output bin from your Code::Blocks project.


  16. Save the script and make it executable:

  17. Now you can run the Collada DOM viewer program from the command line by running your wrapper script:

  18. You can load your Collada 3D models by dropping your Collada .dae and other associated files (exported from your 3D modeling application) into the same directory as the binary and wrapper, and run your wrapper with the .dae file as its argument:

    For some reason, when exporting Collada models in Blender, you have to "Disable Physics", which is an option in the export menu. Some sample Collada files that come with the DOM don't seem to work in the viewer program. Whether these caveats are due to an incompatible build of bullet or something else I did wrong, I do not know, but it seems this setup offers a lot of potential. My goal is to use the idea behind the viewer program, but rewrite it using SDL instead of GLUT. C++ is still very new to me, so it might be a while before I figure it out, but when I do, I'll make a new tutorial.

    Congratulations, now you can make 3D models in a modeling program, and load them into OpenGL using Collada files, the Collada DOM, and C++! Now you have an IDE project which you can modify and expand upon to make a full-scale 3D program or game! If anyone can figure out how to avoid having to set LD_LIBRARY_PATH, please let me know.


Was this tutorial helpful? Toss me a few bucks if you're feeling generous.


Comments

Display Name: hal Posted On:
2011-06-28 02:41:19 EST
Subject: Great report
Comment:
Thanks for taking the time to write this up.

The little things can
really snag someone trying something outside of their comfort zone, and an
article like this can really help a lot.

Display Name: Defcronyke Webmaster Posted On:
2011-06-30 07:57:56 EST
Subject: Re: Great report
Comment:
Hi hal,

I'm glad someone finally found this useful. Collada seems
to be the industry standard for 3D model data, so I have no idea why it's
so difficult and counter-intuitive to set up the Collada DOM. I hope
someone fixes it in the near future.

Display Name: Iman Posted On:
2011-08-12 03:01:00 EST
Subject: Very useful Comment
Comment:
That's useful comment because of the simplicity and straightforward to
start.

Thanks.


Post a Comment



Display Name:
Email Address:
Subject:
Comment:
 NOTE: Your email address will only be seen by the webmaster, and will not be used to send you anything. Your IP address will be logged, but not displayed publicly.


Reload Image
Code:




Go back to Tutorials


This tutorial is copyright © 2010 Defcronyke Webmaster, The Eternal Void Network. It may only be linked to with permission from the original author. If you provide reference to it anywhere, please give credit to the author.



Number of Unique Hits: 00002871
Privacy Policy