Friday, September 27, 2013

OpenGL - load 3D objects from .obj files


In this article I will show you, how to load  complex 3D objects into your JAVA program and how to render them. The example uses JAVA/JOGL, so it is not directly Android-related.

If you try to create some 3D program based on the OpenGL platform, you probably have learned, that it is nearly impossible to create the 3D objects in a vertex-by-vertex way. Maybe there are geeks who are able to define, say 10 kilos of triangles by hand, but I'm not so assiduous to break my fingers on a keyboard.

Fortunately there are much more convenient ways of creating 3D objects. The most convenient of all is probably to download them from Internet ;-) But before we start to use them in the program we have to load them there.
3D objects loaded into the JAVA program

The way I'm about to teach you is that simple:
  • Download 3D object from Internet
  • Import it to the Blender program (http://www.blender.org/)
  • Export it to the .obj format
  • Load it to your program and use it there
Of course you can create the 3D object with Blender (or another 3D editor) by yourself.

The last part of the list can be done surprisingly easily cause the .obj format (http://en.wikipedia.org/wiki/Wavefront_.obj_file) is quite simple and straightforward. I have written the simple loader for you and you are welcome to download it here. Don't worry about the size of the archive - it is so big because it holds four example models for you, so you can just load the projects into Netbeans and run. There are two projects in the archive
  • JOGLObj - the loader itself with the "main" class JOGLObject
  • JOGL - just the example program which shows how to use the JOGLObject

Note you will need java JDK7, and the JOGL library installed and imported to the project (http://jogamp.org/wiki/index.php/Downloading_and_installing_JOGL)


Hope this will help you to create something nice.