<p>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?</p>

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

[{'answer_id': 66535, 'author': 'Rob Dickerson', 'author_id': 7530, 'author_profile': 'https://Stackoverflow.com/users/7530', 'pm_score': 2, 'selected': False, 'text': '<p>Personally, I\'m not even aware of Java bindings for OpenGL other than <a href="https://jogl.dev.java.net/" rel="nofollow noreferrer">JOGL</a> -- I think JOGL is pretty much the standard for Java OpenGL.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 66577, 'author': 'eferm', 'author_id': 9872, 'author_profile': 'https://Stackoverflow.com/users/9872', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://www.opengl.org/resources/bindings/" rel="noreferrer" title="opengl.org">JOGL</a></p>\n\n<p>My reasons can be quoted off the previously linked site:</p>\n\n<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>Also if you want to have some fun learning and poking around, <a href="http://processing.org/" rel="noreferrer" title="Processing">Processing</a> is an excellent way to start (Processing also uses JOGL btw...)</p>\n'}, {'answer_id': 66647, 'author': 'ReaperUnreal', 'author_id': 4218, 'author_profile': 'https://Stackoverflow.com/users/4218', 'pm_score': 3, 'selected': False, 'text': '<p>I\'d suggest checking out <a href="http://lwjgl.org/" rel="nofollow noreferrer">LWJGL</a>, the LightWeight Java Game Library. It\'s got OpenGL bindings, but it also has OpenAL bindings and some great tutorials to get you started.</p>\n\n<p>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.</p>\n'}, {'answer_id': 68392, 'author': 'shoosh', 'author_id': 9611, 'author_profile': 'https://Stackoverflow.com/users/9611', 'pm_score': 3, 'selected': False, 'text': '<p>JOGL is probably the only option worth considering.\nNotice 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.\nThe 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.\nSome code from a recent project:</p>\n\n<pre><code>import org.eclipse.swt.*;\nimport org.eclipse.swt.layout.*;\nimport org.eclipse.swt.widgets.*;\n\nimport javax.media.opengl.*;\nimport javax.media.opengl.glu.*;\n\nimport org.eclipse.swt.awt.SWT_AWT;\nimport org.eclipse.swt.events.*;\n\npublic class Main implements GLEventListener\n{\n    public static void main(String[] args) \n    {\n        Display display = new Display();\n    Main main = new Main();\n    main.runMain(display);\n    display.dispose();\n}\n\nvoid runMain(Display display)\n{\n    final Shell shell = new Shell(display);\n    shell.setText("Q*bert 3D - OpenGL Exercise");\n    GridLayout gridLayout = new GridLayout();\n    gridLayout.marginHeight = 0;\n    gridLayout.marginWidth = 0;\n\n    shell.setLayout(gridLayout);\n\n    // this allows us to set particular properties for the GLCanvas\n    GLCapabilities glCapabilities = new GLCapabilities();\n\n    glCapabilities.setDoubleBuffered(true);\n    glCapabilities.setHardwareAccelerated(true);\n\n    // instantiate the canvas\n    final GLCanvas canvas = new GLCanvas(glCapabilities);\n\n    // we can\'t use the default Composite because using the AWT bridge\n    // requires that it have the property of SWT.EMBEDDED\n    Composite composite = new Composite(shell, SWT.EMBEDDED);\n    GridData ld = new GridData(GridData.FILL_BOTH);\n    composite.setLayoutData(ld);\n\n    // set the internal layout so our canvas fills the whole control\n    FillLayout clayout = new FillLayout();\n    composite.setLayout(clayout);\n\n    // create the special frame bridge to AWT\n    java.awt.Frame glFrame = SWT_AWT.new_Frame(composite);\n    // we need the listener so we get the GL events\n    canvas.addGLEventListener(this);\n\n    // finally, add our canvas as a child of the frame\n    glFrame.add(canvas);\n\n    // show it all\n    shell.open();\n    // the event loop.\n    while (!shell.isDisposed ()) {\n        if (!display.readAndDispatch ()) display.sleep ();\n    }\n}\n</code></pre>\n'}, {'answer_id': 345338, 'author': 'DJClayworth', 'author_id': 19276, 'author_profile': 'https://Stackoverflow.com/users/19276', 'pm_score': 2, 'selected': False, 'text': '<p>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. </p>\n'}, {'answer_id': 4200153, 'author': 'Wade Walker', 'author_id': 207245, 'author_profile': 'https://Stackoverflow.com/users/207245', 'pm_score': 1, 'selected': False, 'text': '<p>We\'ve had lots of luck at work using JOGL. The new 2.0 version is at <a href="http://jogamp.org/" rel="nofollow">http://jogamp.org/</a> (the last "old" version is at <a href="http://download.java.net/media/jogl/builds/archive/jsr-231-1.1.1a/" rel="nofollow">http://download.java.net/media/jogl/builds/archive/jsr-231-1.1.1a/</a>).</p>\n\n<p>For JOGL 2 with SWT specifically, I\'ve got a series of tutorials starting at <a href="http://wadeawalker.wordpress.com/2010/10/09/tutorial-a-cross-platform-workbench-program-using-java-opengl-and-eclipse/" rel="nofollow">http://wadeawalker.wordpress.com/2010/10/09/tutorial-a-cross-platform-workbench-program-using-java-opengl-and-eclipse/</a> that demonstrates exactly how to make cross-platform JOGL SWT applications, complete with installable native binaries.</p>\n\n<p>Or if you don\'t want to use Eclipse RCP, here\'s an even simpler example that just draws one triangle with JOGL 2 and SWT. To build it, put it in a project with swt.jar (from <a href="http://www.eclipse.org/swt/" rel="nofollow">http://www.eclipse.org/swt/</a>) and the latest JOGL autobuild .jar and .dll files (from <a href="http://jogamp.org/" rel="nofollow">http://jogamp.org/</a>). The only problem with this simple example is that it won\'t be cross-platform without some extra help -- you need the ability that Eclipse RCP gives you to bundle multiple sets of platform libraries together into one project.</p>\n\n<pre><code>package name.wadewalker.onetriangle;\n\nimport org.eclipse.swt.SWT;\nimport org.eclipse.swt.events.PaintEvent;\nimport org.eclipse.swt.events.PaintListener;\nimport org.eclipse.swt.graphics.Rectangle;\nimport org.eclipse.swt.layout.FillLayout;\nimport org.eclipse.swt.opengl.GLCanvas;\nimport org.eclipse.swt.opengl.GLData;\nimport org.eclipse.swt.widgets.Composite;\nimport org.eclipse.swt.widgets.Display;\nimport org.eclipse.swt.widgets.Event;\nimport org.eclipse.swt.widgets.Listener;\nimport org.eclipse.swt.widgets.Shell;\n\nimport javax.media.opengl.GL;\nimport javax.media.opengl.GLProfile;\nimport javax.media.opengl.GL2;\nimport javax.media.opengl.GLContext;\nimport javax.media.opengl.GLDrawableFactory;\nimport javax.media.opengl.glu.GLU;\n\npublic class OneTriangle {\n\n    public static void main(String [] args) {\n        GLProfile.initSingleton( true );\n\n        GLProfile glprofile = GLProfile.get( GLProfile.GL2 );\n\n        Display display = new Display();\n        Shell shell = new Shell( display );\n        shell.setLayout( new FillLayout() );\n        Composite composite = new Composite( shell, SWT.NONE );\n        composite.setLayout( new FillLayout() );\n\n        GLData gldata = new GLData();\n        gldata.doubleBuffer = true;\n        // need SWT.NO_BACKGROUND to prevent SWT from clearing the window\n        // at the wrong times (we use glClear for this instead)\n        final GLCanvas glcanvas = new GLCanvas( composite, SWT.NO_BACKGROUND, gldata );\n        glcanvas.setCurrent();\n        final GLContext glcontext = GLDrawableFactory.getFactory( glprofile ).createExternalGLContext();\n\n        // fix the viewport when the user resizes the window\n        glcanvas.addListener( SWT.Resize, new Listener() {\n            public void handleEvent(Event event) {\n                setup( glcanvas, glcontext );\n            }\n        });\n\n        // draw the triangle when the OS tells us that any part of the window needs drawing\n        glcanvas.addPaintListener( new PaintListener() {\n            public void paintControl( PaintEvent paintevent ) {\n                render( glcanvas, glcontext );\n            }\n        });\n\n        shell.setText( "OneTriangle" );\n        shell.setSize( 640, 480 );\n        shell.open();\n\n        while( !shell.isDisposed() ) {\n            if( !display.readAndDispatch() )\n                display.sleep();\n        }\n\n        glcanvas.dispose();\n        display.dispose();\n    }\n\n    private static void setup( GLCanvas glcanvas, GLContext glcontext ) {\n        Rectangle rectangle = glcanvas.getClientArea();\n\n        glcanvas.setCurrent();\n        glcontext.makeCurrent();\n\n        GL2 gl = glcontext.getGL().getGL2();\n        gl.glMatrixMode( GL2.GL_PROJECTION );\n        gl.glLoadIdentity();\n\n        // coordinate system origin at lower left with width and height same as the window\n        GLU glu = new GLU();\n        glu.gluOrtho2D( 0.0f, rectangle.width, 0.0f, rectangle.height );\n\n        gl.glMatrixMode( GL2.GL_MODELVIEW );\n        gl.glLoadIdentity();\n\n        gl.glViewport( 0, 0, rectangle.width, rectangle.height );\n        glcontext.release();        \n    }\n\n    private static void render( GLCanvas glcanvas, GLContext glcontext ) {\n        Rectangle rectangle = glcanvas.getClientArea();\n\n        glcanvas.setCurrent();\n        glcontext.makeCurrent();\n\n        GL2 gl = glcontext.getGL().getGL2();\n        gl.glClear( GL.GL_COLOR_BUFFER_BIT );\n\n        // draw a triangle filling the window\n        gl.glLoadIdentity();\n        gl.glBegin( GL.GL_TRIANGLES );\n        gl.glColor3f( 1, 0, 0 );\n        gl.glVertex2f( 0, 0 );\n        gl.glColor3f( 0, 1, 0 );\n        gl.glVertex2f( rectangle.width, 0 );\n        gl.glColor3f( 0, 0, 1 );\n        gl.glVertex2f( rectangle.width / 2, rectangle.height );\n        gl.glEnd();\n\n        glcanvas.swapBuffers();\n        glcontext.release();        \n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/66446', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9843/']