Aller au contenu

Java OpenGL

Un article de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 26 juillet 2011 à 13:42 et modifiée en dernier par 80.13.77.107 (discuter) (Traduction de la page Wikipedia de JOGL en français (en cours...) [Julien Gouesse]). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

Modèle:Examplefarm Modèle:Infobox software

Java OpenGL (JOGL) est une bibliothèque qui permet d'utiliser OpenGL avec le langage de programmation Java[1][2]. Elle était développée à l'origine par Kenneth Bradley Russell et Christopher John Kline, et fut développée par la suite par le Sun Microsystems Game Technology Group. Depuis 2010, c'est un projet open source indépendant sous BSD license. C'est l'implémentation de référence de Java Bindings for OpenGL (JSR-231).

JOGL permet d'accéder à la plupart des fonctionnalités disponibles en C, à l'exception notable des appels OpenGL Utility Toolkit (GLUT) relatifs au système de fenêtrage, comme Java a les siens, Abstract Window Toolkit (AWT), Swing, et quelques extensions.

Design

L'API de base en OpenGL C API, ainsi que son API de fenêtrage associée[3], sont accédées en JOGL via des appels Java Native Interface (JNI). Pour cette raison, le système sousjacent doit supporter OpenGL pour que JOGL fonctionne.

JOGL diffère des quelques autres bibliothèques en Java enveloppant OpenGL en cela qu'il expose plus ou moins l'API procédurale d'OpenGL via des méthodes dans peu de classes, plutôt que d'essayer d'appliquer le paradigme de la programmation orientée objet à OpenGL. Ainsi, une grande partie du code de JOGL est auto-générée à partir des fichiers d'entête d'OpenGL en C via un outil de conversion appelé GlueGen, qui avait été programmé spécialement pour faciliter la création de 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 :

  • 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.

Quad example

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

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;

/**
 * ported to JOGL 2.0 by Julien Gouesse (http://tuer.sourceforge.net)
 */
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

  • Java Bindings for OpenGL, The Java Community Specification Request for which JOGL provides an implementation
  • LWJGL, an alternative open-source OpenGL wrapper library
  • Ardor3D, a high performance, professionally oriented scene graph using LWJGL and JOGL
  • Xith3D, a scene graph based graphics API using JOGL and LWJGL
  • Elflight Engine, a high performance 3D game engine optimised for the web
  • JMonkey Engine, a high performance scene graph based graphics API using LWJGL and JOGL
  • Jake2, a Java port of Quake II using JOGL or LWJGL for its low-level graphic API
  • Scilab, a numerical computing program using JOGL for 2D, 3D rendering
  • Jreality, a mathematical visualization package

References

  1. « Open source Java projects: Java Binding for OpenGL (JOGL) », JavaWorld, (consulté le ) : « 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, (consulté le )
  3. « 3D & Multimedia Across Platforms and Devices Using JOGL », SIGGRAPH, (consulté le )
  4. « JSR-000231 Java Bindings for the OpenGL API », Java Community Process (consulté le ) : « 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. »