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.

Thursday, November 1, 2012

Ruby Singleton Classes and Relation to Objects and Classes













The methods in the far left column are invoked on the objects defined in the code row. For example, in event sequence 1, the methods are invoked on the module A; event sequence 2, the methods are invoked on class A, and so forth. This shows where Ruby places the method a for each different case of object in the code row. The code in column 8 is a way of putting the singleton class of object o into variable  p. Column 9 shows that the  o.singleton_class method returns an object equal to the singleton class of object o.

In Ruby, methods, for the purposes of storage, are classified as either singleton or instance methods. Two methods defined on every class will tell you what methods of that class are singleton or instance; namely singleton_methods(false) and instance_methods(false); the false parameter is so that the method only looks one level deep in the class hierarchy, if it's not passed then methods from super classes are also returned. Objects - not classes - only have the singleton_methods method because objects cannot have instance methods; remember objects do not store methods only classes store methods. Class instance methods are are kept in the class that defines them, class singleton methods which are also called class methods - or static methods in java - are kept in the singleton class of the class. Where as, singleton methods defined on an object are kept in the singleton class of the object.

On the other hand, methods are also classified by protection level as either public, private or protected. Each case will have differing visibility effects on the method and will show in the return values of methods, private_methods, public_methods and protected_methods.




ruby, singleton_class, singleton classes, singleton methods, instance methods