Java OpenGL

From Wikipedia, the free encyclopedia

JOGL (JSR-231)

A demo screenshot illustrating real-time refraction using JOGL on Mac OS X
Developer: Sun Microsystems Game Technology Group
Latest release: 1.0.0 / September 15, 2006
OS: Cross-platform
Use: 3D computer graphics software (library/API)
License: BSD license
Website: jogl.dev.java.net

Java OpenGL (JOGL) is a wrapper library that allow OpenGL to be used in the Java programming language. It is currently being developed by the Game Technology Group at Sun Microsystems, and is the reference implementation for JSR-231 (Java Bindings for OpenGL).

JOGL allows access to most features available to C programming language programmers, with the notable exception of window-system related calls in GLUT (as Java contains its own higher-level windowing systems, AWT and Swing), and some extensions.

Contents

[edit] Design

The base OpenGL C API is 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 attempting to map OpenGL functionality onto the object-oriented programming paradigm. Indeed, the majority 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.

[edit] Status and Standardization

As of 2006, JOGL provides full access to the OpenGL 2.0 specification.

The last 1.0.0 version is the reference implementation for JSR-231 (Java Bindings for OpenGL).

[edit] Java2D / OpenGL interoperability

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

  • Overlay Swing components (lightweight menus, tooltips, and other widgets) on top of OpenGL rendering.
  • Draw 3D OpenGL graphics on top of Java2D rendering (see here for a button with an OpenGL icon).
  • Use 3D graphics anywhere where ordinarily a Swing widget would be used. (Inside a JTable, JTree, ...)
  • Draw Java2D graphics on top of 3D OpenGL rendering.

[edit] 3D Triangle Base Pyramid Example

This program displays a simple 3D rendering of a pyramid using JOGL.

JavaRenderer class—This class uses GLAutoDrawable to render the 3D scene.

import javax.media.opengl.GL;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class JavaRenderer implements GLEventListener, KeyListener {
   private float rotateT = 0.0f;
   private static final GLU glu = new GLU();

   public void display(GLAutoDrawable gLDrawable) {
       final GL gl = gLDrawable.getGL();
       gl.glClear(GL.GL_COLOR_BUFFER_BIT);
       gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
       gl.glLoadIdentity();
       gl.glTranslatef(0.0f, 0.0f, -5.0f);

       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);
       gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f);

       gl.glBegin(GL.GL_TRIANGLES);

       // Front
       gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f);
       gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
       gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f);

       // Right Side Facing Front
       gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f);
       gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(1.0f, -1.0f, 1.0f);
       gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, -1.0f, -1.0f);

       // Left Side Facing Front
       gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f);
       gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(0.0f, -1.0f, -1.0f);
       gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);

       // Bottom
       gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
       gl.glColor3f(0.1f, 0.1f, 0.1f); gl.glVertex3f(1.0f, -1.0f, 1.0f);
       gl.glColor3f(0.2f, 0.2f, 0.2f); gl.glVertex3f(0.0f, -1.0f, -1.0f);

       gl.glEnd();

       rotateT += 0.2f;
   }

   public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {
   }

   public void init(GLAutoDrawable gLDrawable) {
       final GL gl = gLDrawable.getGL();
       gl.glShadeModel(GL.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(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
       gLDrawable.addKeyListener(this);
   }

   public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {
       final GL gl = gLDrawable.getGL();
       if(height <= 0) {
           height = 1;
       }
       final float h = (float)width / (float)height;
       gl.glMatrixMode(GL.GL_PROJECTION);
       gl.glLoadIdentity();
       glu.gluPerspective(50.0f, h, 1.0, 1000.0);
       gl.glMatrixMode(GL.GL_MODELVIEW);
       gl.glLoadIdentity();
   }

   public void keyPressed(KeyEvent e) {
       if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
           JavaDia.bQuit = true;
           JavaDia.displayT.stop();
           System.exit(0);
       }
   }

   public void keyReleased(KeyEvent e) {
   }

   public void keyTyped(KeyEvent e) {
   }
}
JOGL - 3D Triangle Base Pyramid Picture
JOGL - 3D Triangle Base Pyramid Picture

JavaDia class—The main class links the JavaRenderer class. The code draws the 3D scene to a GLCanvas.

import javax.media.opengl.GLCanvas;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class JavaDia implements Runnable {
    static Thread displayT = new Thread(new JavaDia());
    static boolean bQuit = false;

    public static void main(String[] args) {
        displayT.start();
    }

    public void run() {
        Frame frame = new Frame("Jogl 3d Shape/Rotation");
        GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(new JavaRenderer());
        frame.add(canvas);
        frame.setSize(640, 480);
        frame.setUndecorated(true);
        int size = frame.getExtendedState();
        size |= Frame.MAXIMIZED_BOTH;
        frame.setExtendedState(size);

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                bQuit = true;
                displayT.stop();
                System.exit(0);
            }
        });
        frame.setVisible(true);
//      frame.show();
        canvas.requestFocus();
        while( !bQuit ) {
            canvas.display();
        }
    }
}

[edit] See also

  • LWJGL : another open-source OpenGL wrapper library

[edit] External links

In other languages