Exploring OpenGL Java Binding by Scripting

These days I learn Java APIs by exploratory programming using the new scripting feature in JDK 6. I try interactively out using jrunscript . I wanted to try out the JSR-231 (Java Binding for the OpenGL) API.


Steps

  • I downloaded the binary from http://jogl.dev.java.net
  • In particular, I downloaded jogl-1_0_0-windows-i586.zip and unziped it under some directory, say %JOGL_HOME%
  • I ran jrunscript with the following command line:
    
    jrunscript -J-Djava.library.path=%JOGL_HOME%/lib -cp %JOGL_HOME%/lib/jogl.jar
    
    
    -J-Djava.library.path option is used so that JVM can load JOGL native DLLs and -cp jogl.jar is used to put jogl classes in the CLASSPATH. With this command line, jrunscript waits for interective user input with "js>" prompt.
  • As a simple check, I ran the following commands:
    
    
    js> importPackage(javax.media.opengl)
    js> println(new GLCapabilities())
    GLCapabilities [DoubleBuffered: true, Stereo: false, HardwareAccelerated: true,
    DepthBits: 24, StencilBits: 0, Red: 8, Green: 8, Blue: 8, Alpha: 0, Red Accum: 0
    , Green Accum: 0, Blue Accum: 0, Alpha Accum: 0, Multisample: false ]
    js>
    
    
    
    So, looks like we can access JSR-231 classes successfully. Now, to some "hello worlds" with OpenGL binding...
  • I "ported" two simple examples from the red book to JavaScript.

    File: rect.js
    
    // import JSR-231 stuff
    importPackage(javax.media.opengl);
    importClass(javax.swing.JFrame);
    
    var frame = new JFrame("hello");
    frame.setSize(400, 400);
    
    //create GLCanvas and add that to frame
    var glcanvas = new GLCanvas();
    frame.add(glcanvas);
    
    glcanvas.addGLEventListener(new GLEventListener() {
       init: function(drawable) {
       },
    
       display: function(drawable) {
         var gl = drawable.getGL();
    
         // with this "with" statement, we don't need to prefix
         // "gl." before GL method calls -- so that it feels like
         // writing in C :-)
    
         with(gl) {
             glClearColor(0.0, 0.0, 0.0, 0.0);
             glClear(GL_COLOR_BUFFER_BIT);
             glColor3f(1.0, 0.0, 0.0);
             glBegin(GL_POLYGON);
             glVertex3f(0.25, 0.25, 0.0);
             glVertex3f(0.75, 0.25, 0.0);
             glVertex3f(0.75, 0.75, 0.0);
             glVertex3f(0.25, 0.75, 0.0);
             glEnd();
             glFlush();
         }     
       },
    
       reshape: function() {},
       displayChanged: function() {}
    });
    
    frame.setVisible(true);
    
    
    File: circle.js
    
    importPackage(javax.media.opengl);
    importClass(javax.swing.JFrame);
    
    var frame = new JFrame("hello");
    frame.setSize(400, 400);
    var glcanvas = new GLCanvas();
    frame.add(glcanvas);
    
    glcanvas.addGLEventListener(new GLEventListener() {
       init: function(drawable) {
       },
    
       display: function(drawable) {
         var gl = drawable.getGL();
    
         with(gl) {
             glClearColor(0.0, 0.0, 0.0, 0.0);
             glClear(GL_COLOR_BUFFER_BIT);
             glColor3f(0.0, 1.0, 0.0);
             glBegin(GL_LINE_LOOP);
             for (var i = 0; i < 100; i++) {
               var angle = 2\*Math.PI\*i / 100;
               glVertex2f(Math.cos(angle), Math.sin(angle));
             }
             glEnd();
             glFlush();
         }     
       },
    
       reshape: function() {},
       displayChanged: function() {}
    });
    
    frame.setVisible(true);
    
    
  • I loaded the above samples in jrunscript prompt using the load function.
    
    js>load("rect.js");
    js>load("circle.js");
    
    
Yes, It works!!
Comments:

[Trackback] That's a little bit of fun for you today: You can write a 3D apps using javascript while using javascript agent! Don't think you can do that with c# :-)

Posted by Sunny Chan blogs on January 10, 2007 at 11:39 AM IST #

Very cool.

Posted by Daniel Johnson on January 10, 2007 at 01:07 PM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder