Lego Models (LDraw) Loaded from Java

  • strict warning: Non-static method view::load() should not be called statically in /home/codecomp/public_html/ on line 906.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/codecomp/public_html/ on line 607.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/codecomp/public_html/ on line 607.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/codecomp/public_html/ on line 159.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/codecomp/public_html/ on line 24.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/codecomp/public_html/ on line 134.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/codecomp/public_html/ on line 134.


Back in 2007 I step on LDraw and MLCAD WEB sites. For someone like me, who played a lot with Lego during his childhood, LDraw and MLCAD are/were certainly an incommensurable source of joy.

I found those WEB sites just before Christmas vacation so I raged to build in MLCAD many of the Lego models I had when I was a child.

By then I was also doing some small work on 3D graphics programming, using mostly Java3D and JOGL, so I developed a small piece of software intended to load LDraw models from Java. The following is a video of that software, showing some of the Lego models I build that Christmas:

Recently, I got a request to write a video explaining how to run the software (I have to admit that there isn't a tutorial, and it's not exactly easy to run it). So here goes the tutorial.

First, import the code from the SVN repository (I will not detail this, you'll have to figure it out). The source code can be found here:

You will need to get the Lego project. I use Eclipse, so the project is already configured to run in Eclipse.

After importing the project, you should try to run the class view.Main. It will probably not run at first, so you will find in front of this exception:

  at view.Main.display(
  at com.sun.opengl.impl.GLDrawableHelper.display(
  at com.sun.opengl.impl.GLDrawableHelper.invokeGL(
  at java.awt.event.InvocationEvent.dispatch(
  at java.awt.EventQueue.dispatchEvent(
  at java.awt.EventDispatchThread.pumpOneEventForFilters(
  at java.awt.EventDispatchThread.pumpEventsForFilter(
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(
  at java.awt.EventDispatchThread.pumpEvents(
  at java.awt.EventDispatchThread.pumpEvents(

This is because you don't have the ldraw parts and the models in the right place. To solve it you have to uncompress the files ldraw.tar.gz and model.tar.gz located in the root of the project.

That way you will end with two additional directories in project root: ldraw and model.

Go back to eclipse and refresh the project: select the project Lego and hit F5, or select the project and ”right click->refresh”.

Then go again to view.Main and change the part:

// ***************************************
// XXX: Change according the real location
private static final String ldraw_base = "/home/dmi/workspace/Lego/ldraw"; // LDraw home
private static final String model_base = "/home/dmi/workspace/Lego/model"; // Where to the models to be displayed
// ***************************************

Set the path "/home/dmi/workspace/Lego/XXX” in both entries according to the location of the project root. If you just uncompressed the ldraw and model gz files in the project root left the last part unchanged, if you put the files anywhere else change the route accordingly.

Then, in the same file, go to the part with the models (I only show a few, but you should see a lot of commented similar lines:

part = partLoader.loadPart("1499-1.mpd");  // Medium twin seat spaceship
//part = partLoader.loadPart("885-1.mpd");   // Very small spaceship (5)
//part = partLoader.loadPart("487-1.mpd");   // Medium Ship with deployable car
//part = partLoader.loadPart("6804-1.mpd");  // Very small car (2)
//part = partLoader.loadPart("6807-1.mpd");  // Very (very) small spaceship with robot

And uncomment one line (you can uncomment as many as you want, but it should display the last one). If every thing goes well you should see something like:

You can add your own models in the model directory and then add the appropriate line there to load it.

If you run it and find this exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jogl_drihack in java.library.path
  at java.lang.ClassLoader.loadLibrary(
  at java.lang.Runtime.loadLibrary0(
  at java.lang.System.loadLibrary(
  at com.sun.opengl.impl.NativeLibLoader$DefaultAction.loadLibrary(
  at com.sun.opengl.impl.NativeLibLoader.loadLibrary(
  at com.sun.opengl.impl.NativeLibLoader.access$100(
  at com.sun.opengl.impl.NativeLibLoader$
  at Method)
  at com.sun.opengl.impl.NativeLibLoader.loadDRIHack(
  at com.sun.opengl.impl.x11.DRIHack.begin(
  at com.sun.opengl.impl.x11.X11GLDrawableFactory.<clinit>(
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(
  at view.Main.main(

It's because java is not finding JOGL binary libraries. Here you have two options:

  • Add the libraries in a place where they can be found by default. For example, in Linux add them in /usr/lib (or /usr/local/lib) and then run ldconfig. In windows add them in “C:\windows\system32” (I've not been windows user for a long time, so find yourself the right place if it does not work). Note that this option will require root access in Linux.

  • Another option is to go in eclipse to the “Run Configurations” and manually set up the library path. To go to “Run Configurations”, pull the run button (don't click the button, just the tiny arrow besides the button) -> Run Configurations” -> Main”. Main is the name of the “view.Main” executable class. Then go to Arguments -> VM Arguments, and add:


Change “/home/dmi/workspace/Lego” according to your set up.

The JOGL binary libraries are included in the root of the project, the files are:

-rw-r--r-- 1 dmi dmi   20480 2012-05-20 16:51 jogl_awt.dll
-rw-r--r-- 1 dmi dmi  114688 2012-05-20 16:52 jogl_cg.dll
-rw-r--r-- 1 dmi dmi  290816 2012-05-20 16:51 jogl.dll
-rw-r--r-- 1 dmi dmi   10107 2012-05-20 16:51
-rw-r--r-- 1 dmi dmi  185584 2012-05-20 16:51
-rw-r--r-- 1 dmi dmi    6421 2012-05-20 16:51
-rw-r--r-- 1 dmi dmi  962937 2012-05-20 16:51

The *.so files are for Linux, the *.dll files are for Windows.

If you are using any other IDE instead of Eclipse, or you want to run from the console, or make a jar, etc, you will have to figure it our by yourself, but given this tutorial it should not be difficult. For example, I can run it directly from the console with the commands:

$ > export LD_LIBRARY_PATH=/home/dmi/workspace/Lego/
$ > java -cp bin/:lib/jogl.jar:lib/commons-logging-1.1.jar:lib/commons-lang-2.1.jar view.Main

I wrote the code in 2007 and even then I was using an old version of JOGL (Don't ask why). A good contribution would be to migrate it to use a newer version of JOGL. Also, considering what I've learn since then about software and 3D graphics, I think the code could be probably improved a lot.

Also there is a known bug with transparency. If you use transparent Lego pieces, it's possible that something behind them would not display correctly. This is because transparency (using depth buffer) requires to draw all transparent geometry after drawing non transparent geometry (on top of non transparent geometry). Currently the code is not doing this, so if any object behind a transparent window is drawn after the window, it will just not been drawn, because it will be behind the window and depth buffer will prevent it from being drawn. One solution would be to detect the transparent objects and add them to a list instead of drawing them, and at the end, draw them after all the non transparent objects.

Relating to the license of the code, basically you can do whatever you want with it. I only ask two things: 1) If you use it, change it, blog about it, or do anything else with it, please mention me as the original author and link back to this post. 2) If you make any changes, improvements, convert it to a library, etc, I would like to know it and have the possibility to add those changes to the original project.


Kevin Loddewykx has contributed a great update to the project. I'll let his changelog speak for itself:

Update for project "Lego"
1) Uses the most recent version of jogl + gleugen and log4j 1.2
2) Uses the most recent version of ldraw parts library and ldconfig.ldr

3) Camera rotates now instead of the object
4) Model can be given through the command line
5) Pressing R will now change the model when it is changed in the code, when you
are real-time debugging

6) Transparency has been updated, so transparent objects always will be rendered
7) Previous when more then one model was uncommented, it's was possible that
models would get mixed up.

8) Extra model added train.ldr :)

The transparency fix is especially important, the following picture shows the bug:


And the following picture shows the same model after Kevin's fix:


Thanks a lot Kevin for the update, it works like a charm :)

Great Article

Thanks for article, it helps a lot. Regards.


I figure that if God actually does exist, He's big enough to understand an honest difference of opinion.

Isaac Asimov
via linux fortune