views:

1826

answers:

5

I am trying to achieve better performance for my Java SWT application, and I just found out it is possible to use OpenGL in SWT. It seems there are more than one Java binding for OpenGL. Which one do you prefer?

Note that I have never used OpenGL before, and that the application needs to work on Windows, Linux and Mac OS X.

+1  A: 

Personally, I'm not even aware of Java bindings for OpenGL other than JOGL -- I think JOGL is pretty much the standard for Java OpenGL.

It works in Windows, Linux, and OS X, but you might want to read over the official documentation for some notes about specific issues in each platform.

Keep in mind that the OpenGL paradigm is quite different from Swing/AWT or the Java 2D API; OpenGL is not a drop-in replacement for Swing.

Rob Dickerson
+6  A: 

JOGL

My reasons can be quoted off the previously linked site:

JOGL provides full access to the APIs in the OpenGL 2.0 specification as well as nearly all vendor extensions, and integrates with the AWT and Swing widget sets.

Also if you want to have some fun learning and poking around, Processing is an excellent way to start (Processing also uses JOGL btw...)

Emore
+3  A: 

I'd suggest checking out LWJGL, the LightWeight Java Game Library. It's got OpenGL bindings, but it also has OpenAL bindings and some great tutorials to get you started.

Just keep in mind that Swing/SWT and OpenGL are generally used for entirely different things. You may end up wanting to use a combination of both. Just try LWJGL out and see how well it fits with what you're doing.

ReaperUnreal
+2  A: 

JOGL is probably the only option worth considering. Notice that there are at least two options for integrating it into an SWT application. There's a GLCanvas that belongs to SWT and a GLCanvas that belongs to AWT. The one in SWT is not feature complete and is not really maintained. It's much better to use the AWT GLCanvas inside a SWT_AWT container. Some code from a recent project:

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;

import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.*;

public class Main implements GLEventListener
{
    public static void main(String[] args) 
    {
     Display display = new Display();
 Main main = new Main();
 main.runMain(display);
 display.dispose();
}

void runMain(Display display)
{
 final Shell shell = new Shell(display);
 shell.setText("Q*bert 3D - OpenGL Exercise");
 GridLayout gridLayout = new GridLayout();
 gridLayout.marginHeight = 0;
 gridLayout.marginWidth = 0;

 shell.setLayout(gridLayout);

 // this allows us to set particular properties for the GLCanvas
 GLCapabilities glCapabilities = new GLCapabilities();

 glCapabilities.setDoubleBuffered(true);
 glCapabilities.setHardwareAccelerated(true);

 // instantiate the canvas
 final GLCanvas canvas = new GLCanvas(glCapabilities);

 // we can't use the default Composite because using the AWT bridge
 // requires that it have the property of SWT.EMBEDDED
 Composite composite = new Composite(shell, SWT.EMBEDDED);
 GridData ld = new GridData(GridData.FILL_BOTH);
 composite.setLayoutData(ld);

 // set the internal layout so our canvas fills the whole control
 FillLayout clayout = new FillLayout();
 composite.setLayout(clayout);

 // create the special frame bridge to AWT
 java.awt.Frame glFrame = SWT_AWT.new_Frame(composite);
 // we need the listener so we get the GL events
 canvas.addGLEventListener(this);

 // finally, add our canvas as a child of the frame
 glFrame.add(canvas);

 // show it all
 shell.open();
 // the event loop.
 while (!shell.isDisposed ()) {
  if (!display.readAndDispatch ()) display.sleep ();
 }
}
shoosh
+1  A: 

JOGL will give you best performance and portability. But be aware that learning JOGL, which is essentially the same as learning OpenGL, is not easy.

DJClayworth