Java OpenGL

JOGL (JSR-231)

A demo screenshot illustrating real-time refraction using JOGL on Mac OS X
Developer(s) JogAmp Community
Stable release 1.1.1 / May 22, 2008; 3 years ago (2008-05-22)
Preview release 2.0-rc4 / December 2, 2011; 2 months ago (2011-12-02)
Operating system Cross-platform
Type 3D computer graphics software (library/API)
License BSD license
Website jogamp.org

Java OpenGL (JOGL) is a wrapper library that allows OpenGL to be used in the Java programming language[1][2]. It was originally developed by Kenneth Bradley Russell and Christopher John Kline, and was further developed by the Sun Microsystems Game Technology Group. Since 2010, it has been an independent open source project under a BSD license. It is the reference implementation for Java Bindings for OpenGL (JSR-231).

JOGL allows access to most OpenGL features available to C language programs through the use of Java Native Interface (JNI). It offers access to both the standard GL* functions along with the GLU* functions; however the OpenGL Utility Toolkit (GLUT) library is not available for window-system related calls, as Java has its own windowing systems; Abstract Window Toolkit (AWT), Swing, and some extensions.

Contents

Design

The base OpenGL C API, as well as its associated Windowing API[3], are accessed in JOGL via Java Native Interface (JNI) calls. As such, the underlying system must support OpenGL for JOGL to work.

JOGL differs from some other Java OpenGL wrapper libraries in that it merely exposes the procedural OpenGL API via methods on a few classes, rather than trying to map OpenGL functionality onto the object-oriented programming paradigm. Indeed, most of the JOGL code is autogenerated from the OpenGL C header files via a conversion tool named GlueGen, which was programmed specifically to facilitate the creation of JOGL.

This design decision has both its advantages and disadvantages. The procedural and state machine nature of OpenGL is inconsistent with the typical method of programming under Java, which is bothersome to many programmers. However, the straightforward mapping of the OpenGL C API to Java methods makes conversion of existing C applications and example code much simpler. The thin layer of abstraction provided by JOGL makes runtime execution quite efficient, but accordingly is more difficult to code compared to higher-level abstraction libraries like Java3D. Because most of the code is autogenerated, changes to OpenGL can be rapidly added to JOGL.

Status and standardization

As of 2007, JOGL provides full access to the OpenGL 2.0 specification. The last 1.1.0 version is the reference implementation for JSR-231 (Java Bindings for OpenGL)[4]. The 1.1.1 release gives limited access to GLU NURBS, providing rendering of curved lines and surfaces via the traditional GLU APIs.

Version 2.0 is currently in development, which includes a minor API refactoring and support for OpenGL profiles GL 1.3 - 3.0, GL 3.1 - 3.3, GL ≥ 4.0, ES 1.x and ES 2.x.

Java2D-OpenGL interoperability

Since the Java SE 6 version of the Java language, Java2D (the API for drawing two dimensional graphics in Java) and JOGL have become interoperable, allowing it to :

Quad example

This program displays a simple 3D rendering of a polygon using JOGL. Please note though that this code is a demonstration of the use of JOGL and as such makes use of immediate mode drawing commands, this serves to show how the conventional C style API is used through JOGL but you are strongly recommended to make use of modern OpenGL techniques.

JOGLQuad class—This class uses the JOGL API (version 2.0) to render a polygon.

import java.awt.Component;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLLightingFunc;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
 
import com.jogamp.opengl.util.Animator;
 
public class JOGLQuad implements GLEventListener, KeyListener {
    float rotateT = 0.0f;
 
    static GLU glu = new GLU();
 
    static GLCanvas canvas = new GLCanvas();
 
    static Frame frame = new Frame("Jogl Quad drawing");
 
    static Animator animator = new Animator(canvas);
 
    public void display(GLAutoDrawable gLDrawable) {
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -5.0f);
 
        // rotate on the three axis
        gl.glRotatef(rotateT, 1.0f, 0.0f, 0.0f);
        gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f);
        gl.glRotatef(rotateT, 0.0f, 0.0f, 1.0f);
 
        // Draw A Quad
	gl.glBegin(GL2.GL_QUADS);		
            gl.glColor3f(0.0f, 1.0f, 1.0f);   // set the color of the quad
	    gl.glVertex3f(-1.0f, 1.0f, 0.0f);	// Top Left
	    gl.glVertex3f( 1.0f, 1.0f, 0.0f);	// Top Right
	    gl.glVertex3f( 1.0f,-1.0f, 0.0f);	// Bottom Right
	    gl.glVertex3f(-1.0f,-1.0f, 0.0f);	// Bottom Left
        // Done Drawing The Quad
	gl.glEnd();                                                     
 
        // increasing rotation for the next iteration					
        rotateT += 0.2f; 
    }
 
    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {
    }
 
    public void init(GLAutoDrawable gLDrawable) {
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glShadeModel(GLLightingFunc.GL_SMOOTH);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClearDepth(1.0f);
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glDepthFunc(GL.GL_LEQUAL);
        gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
        ((Component) gLDrawable).addKeyListener(this);
    }
 
    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {
        GL2 gl = gLDrawable.getGL().getGL2();
        if (height <= 0) {
            height = 1;
        }
        float h = (float) width / (float) height;
        gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(50.0f, h, 1.0, 1000.0);
        gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
 
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            exit();
        }
    }
 
    public void keyReleased(KeyEvent e) {
    }
 
    public void keyTyped(KeyEvent e) {
    }
 
    public static void exit() {
        animator.stop();
        frame.dispose();
        System.exit(0);
    }
 
    public static void main(String[] args) {
        canvas.addGLEventListener(new JOGLQuad());
        frame.add(canvas);
        frame.setSize(640, 480);
        frame.setUndecorated(true);
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                exit();
            }
        });
        frame.setVisible(true);
        animator.start();
        canvas.requestFocus();
    }
 
    public void dispose(GLAutoDrawable gLDrawable) {
        // do nothing
    }
}

See also

References

  1. ^ "Open source Java projects: Java Binding for OpenGL (JOGL)". JavaWorld. 2008-09-18. http://www.javaworld.com/javaworld/jw-09-2008/jw-09-opensourcejava-jogl.html. Retrieved 2011-02-06. "JOGL originated as a project named Jungle, which was created by 3D graphics experts Ken Russell (of Sun Microsystems) and Chris Kline (of Irrational Games)." 
  2. ^ "Hello JOGL". JavaWorld. 2005-02-21. http://www.javaworld.com/javaworld/jw-02-2005/jw-0221-jogl.html. Retrieved 2011-02-06. 
  3. ^ "3D & Multimedia Across Platforms and Devices Using JOGL". SIGGRAPH. 2010-07-27. http://jogamp.org/doc/siggraph2010/jogamp-siggraph2010.pdf. Retrieved 2011-02-06. 
  4. ^ "JSR-000231 Java Bindings for the OpenGL API". Java Community Process. http://jcp.org/aboutJava/communityprocess/final/jsr231/index.html. Retrieved 2011-02-06. "In order to facilitate maximum community participation for the Java Binding for the OpenGL API, we use the JOGL project on java.net found at https://jogl.dev.java.net. The JOGL source code can be found there, licensed under a liberal source code license (mostly licensed as BSD except where we use other parties' licensed code). We take a snapshot of the code from this project every few months, run the Technology Compatibility Kit on the source code, and then officially make it the Reference Implementation for each formal Java Binding for the OpenGL API release." 

External links