This is the second post in a series of OpenGL “tutorials” explaining how to get a triangle on your screen using as few lines of code, in as many languages, and on as many platforms as possible.

In Java the predominant approach for getting OpenGL up and running appears to be a library called JOGL. (For instance, the popular artist programming environment Processing uses JOGL) To get setup with JOGL, you’ll have to download and install the library.  Don’t forget to set your CLASSPATH and your DYLD_LIBRARY_PATH (mac), LD_LIBRARY_PATH (linux/solaris), or PATH (windows) environment variables.

import javax.media.opengl.*;
import java.awt.Frame;
import com.sun.opengl.util.Animator;
// A rather motley collection of imports

// GLEventListeners provide a callback mechanism for the most
// common callbacks.  Here I'll use the mandatory enclosing class
// to provide a GLEventListener implementation.
public class hello_gl_comments implements GLEventListener
{

public static void main(String[] args)
{
    // This use of a class/struct solely to bundle together an
    // unwieldy number of configuration arguments is also used
    // in some Microsoft APIs.
    GLCapabilities caps = new GLCapabilities();
    caps.setHardwareAccelerated(true);
    caps.setDoubleBuffered(true);
    // We can set the depth buffer and RGBA mode here, although
    // JOGL requires a more specific understanding of what doing
    // so means.  I'd rather avoid that complication here.
    GLCanvas canvas = new GLCanvas(caps);
    // In JOGL this canvas (space to make an OpenGL drawing)
    // is distinct from the window in which it's situated.

    // For all of our purposes here Frame == Window
    Frame frame = new Frame("Hello, GL!");
    frame.setSize(500,500);
    frame.setLocation(0,0);
    frame.add(canvas);
    frame.setVisible(true);

    // register the callbacks
    hello_gl_comments listener = new hello_gl_comments();
    canvas.addGLEventListener(listener);

    // This is basically equivalent to calling glutMainLoop()
    Animator anim = new Animator(canvas);
    anim.start();
}

/*
    Note that all of the callbacks take a GLAutoDrawable as
    a first argument.  Access to the standard OpenGL functions
    is then provided through a GL object:

        GL gl = drawable.getGL();

    I believe that the purpose of issuing these calls to
    a specific GL object instead of to static members of a class
    (a la System and Math) is to support multiple,
    simultaneously operating GL contexts.
*/

    public void display(GLAutoDrawable drawable)
    {
        GL gl = drawable.getGL();

        gl.glClear(GL.GL_COLOR_BUFFER_BIT);

        gl.glColor3f(1,1,1);
        gl.glBegin(GL.GL_TRIANGLES);
            gl.glVertex3f(-0.5f,-0.5f,1.0f);
            gl.glVertex3f(0.5f,-0.5f,1.0f);
            gl.glVertex3f(-0.5f,0.5f,1.0f);
        gl.glEnd();

        // buffers are swapped by default.  No need to do anything
    }

    // unimplemented callbacks -- required to support interface
    public void init(GLAutoDrawable drawable)
    {}
    public void reshape(GLAutoDrawable drawable,
                        int x,
                        int y,
                        int width,
                        int height)
    {}
    public void displayChanged(GLAutoDrawable drawable,
                               boolean modeChanged,
                               boolean deviceChanged)
    {}
}

Since Java’s AWS (windowing system) and JOGL make things a bit more complicated and idiosyncratic than GLUT, I’ve inlined a bunch of comments into the code to try to highlight correspondences with the C program.

What I like about working in Java with JOGL:  So far, not much.  The big advantage to be won from Java is automatic garbage collection and memory safety.  Unfortunately, Java seems to make everything more verbose.  Even if I pull out all the comments, I’m still left with 55 lines instead of 36 in C.  This wouldn’t be so big a deal if those extra 19 lines were something other than just boiler-plate.  Add to this the all too likely case that vertex/vector/pixel manipulation is going to be slow like molasses and Java doesn’t really seem like a happy language to write any (even basic) 3d graphics in.

Advertisements