Thursday, March 21, 2013

JRuby, JOGL and OpenGL

The following is an example of using JOGL -- the Java OpenGL binding -- with JRuby. The code was originally in java and taken from JOGL tutorial. Many of the JRuby use cases are described on the JRuby-Reference

The prerequisites are
  (1) having JRuby up and running which is easiest to do with rvm
  (2) having recent version of the java sdk intalled, open-jdk or the oracle version
  (3) JOGL packages need to be installed or setup separately. The tutorials describe this too.

Once all of that is in place...







(1) and (2) mandatory for for any JRuby application intending on calling Java

(3) and (4) are the paths to the JOGL class files after having compiled it. You may be able to get JOGL working from the operating system package management system; however I could not. I had a lot of difficulty working with the packages and eventually opted for compiling from source. It failed some tests but most were passed and for this simple script seems okay.


the java_import method is the JRuby version of Java's import, java_package will import an entire package, however the classes will only be addressable by their fully qualified names. You can redefine the namespace by returning the namespace from a method call. This is precisely what def ogl and def jogl do in lines (16) and (17). An alternative to using java_package is to use include_package within a module and then include the module at a later stage.

these lines create a Java awt class. Not sure what it does and can't remember what it's for. Simply a thingy that needs to be plugged in to make a window. The method on the other hand obviously is responsible for closing the window when the user clicks close.

here we define the class that is going to run the window loop. First note line (33) which is used for Java interface implementation. Next we initialize all the instance variables and objects of our class. All of this stuff is described very clearly in the tutorials mentioned above.

These methods all have to be defined for the interface implementation. The init method is called when this class is registered as a GLEventListener back in line (48). The update method, is used to to recalculate coordinates. The last and most important method is described next where drawing happens.
That's it it's done. The last line simply generates a new object of the recently defined class.