Java Opengl

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.

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

The 1.1.1 release gives limited access to GLU NURBS, providing rendering of curved lines and surfaces via the traditional GLU APIs.

The 1.1.1a release fixes a critical buffer overrun issue, and is otherwise completely compatible with 1.1.1.

A 1.1.2 pre-release was temporarily available but appears to have been abandoned.

Version 2.0 is currently in beta 10, which includes a minor API re-factoring and support for OpenGL profiles GL 1.3 – 3.0, GL 3.1, ES 1.x and ES 2.x.

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.

3D Tetrahedron Example

JOGL – 3D Tetrahedron Picture

This program displays a simple 3D rendering of a tetrahedron using JOGL (version 1.x).

JOGLTetrahedron classhis class uses the JOGL API (version 1.x) to render a 3D tetrahedron.

//depends on jogl.jar and gluegen-rt.jar

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;

import javax.media.opengl.GLCanvas;

import java.awt.Frame;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import com.sun.opengl.util.Animator;

public class JOGLTetrahedron implements GLEventListener, KeyListener {

float rotateT = 0.0f;

static GLU glu = new GLU();

static GLCanvas canvas = new GLCanvas();

static Frame frame = new Frame(“Jogl 3D Shape/Rotation”);

static Animator animator = new Animator(canvas);

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) {

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) {

GL gl = gLDrawable.getGL();

if(height <= 0) {

height = 1;

}

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) {

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 JOGLTetrahedron());

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();

}

}

See also

Java Bindings for OpenGL – The Java Community Specification Request which JOGL provides an implementation for.

LWJGL : An alternative open-source OpenGL wrapper library.

Ardor3D : a high performance, professionally oriented scene graph that uses LWJGL and JOGL

Xith3D : a scene graph based graphics API that uses 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 that uses LWJGL and JOGL

Poxnora, an online multiplayer game that uses JOGL

Wurm Online, a fantasy MMORPG that uses JOGL

RuneScape, another MMORPG using JOGL

Jake2, a Java port of Quake II using JOGL or LWJGL for its low-level graphic API

Scilab, a numerical computing software using JOGL for 2D & 3D rendering

External links

New JOGL Home at Kenai.com

JOGL 2.x Specification

JOGL at Java.net

JSR-231 “Java Bindings for OpenGL” website

tool kiet – The OpenGL Programming Guide examples using JOGL.

NeHe’s tutorials and sample code

JOGL port of the Nehe tutorials

JSR 231 Specification (draft)

Setting up JOGL in Eclipse

NetBeans OpenGL Pack – OpenGL Development Environment plugin for the NetBeans IDE

Viewer3D – An applet to display interactive 3D content with JOGL.

Eclipse OpenGL Pack OpenGL plugin for the Eclipse_(IDE) IDE

Categories: Java platform | 3D computer graphics | Java APIs | Java libraries

I am China Crafts Suppliers writer, reports some information about buy anthurium , wholesale lobster tails.

Processing your request, Please wait....

Leave a Reply