Schabby's Blog
OpenGL, Java, Cassandra and other stuff that totally makes the world go round

This getting started tutorial is intended for JOGL users that are mere beginners. It helps to setup a recent JOGL installation in Eclipse on Windows.

First you need to download JOGL. This is actually more difficult than it sounds because the precompiled binary packages are a bit hidden in the new project site. Fortunately, I figured out where you can download recent JOGL binaries.

For this tutorial, I am using

  • jogl-b633-2012-01-23_20-37-13, and
  • gluegen-b480-2012-01-23_16-49-04.

This is how your project setup should look like:

JOGL Project Setup

So one you got the binaries, unpack the ZIP file somewhere. You will notice the jar folder with a number of JAR files. All you need is in that jar folder, because the DLL binaries are included in the JARs.

Create a new eclipse project (name doesnt matter) and add the following JARs on the classpath:

  • gluegen-rt-natives-windows-amd64.jar
  • gluegen-rt.jar
  • jogl-all-natives-windows-amd64.jar
  • gluegen-rt.jar

These files are the native part of the OpenGL binding and are linked via Java Native Interface (JNI). Since the DLLs are precompiled as part of the JAR files, you are almost ready to go.

Once you got that set up, create a package de.schabby.jogl.helloworld and copypaste the following two classes in the newly created package.

package de.schabby.jogl.helloworld;
 
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
 
public class HelloWorld 
{
    public static void main(String[] args) 
    {
    	// setup OpenGL Version 2
    	GLProfile profile = GLProfile.get(GLProfile.GL2);
    	GLCapabilities capabilities = new GLCapabilities(profile);
 
    	// The canvas is the widget that's drawn in the JFrame
    	GLCanvas glcanvas = new GLCanvas(capabilities);
    	glcanvas.addGLEventListener(new Renderer());
    	glcanvas.setSize( 300, 300 );
 
        JFrame frame = new JFrame( "Hello World" );
        frame.getContentPane().add( glcanvas);
 
        // shutdown the program on windows close event
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                System.exit(0);
            }
        });
 
        frame.setSize( frame.getContentPane().getPreferredSize() );
        frame.setVisible( true );
    }
}

The Render.java class looks like follows. It implements GLEventListener which is the call-back implementaiton by JOGL to do all OpenGL rendering.

package de.schabby.jogl.helloworld;
 
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
 
class Renderer implements GLEventListener 
{
    private GLU glu = new GLU();
 
    public void display(GLAutoDrawable gLDrawable) 
    {
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(-1.5f, 0.0f, -6.0f);
        gl.glBegin(GL2.GL_TRIANGLES);		
        gl.glVertex3f(0.0f, 1.0f, 0.0f);	
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);	
        gl.glVertex3f(1.0f, -1.0f, 0.0f);	
        gl.glEnd();				
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        gl.glBegin(GL2.GL_QUADS);           	
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);	
        gl.glVertex3f(1.0f, 1.0f, 0.0f);	
        gl.glVertex3f(1.0f, -1.0f, 0.0f);	
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);	
        gl.glEnd();				
        gl.glFlush();
    }
 
 
    public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) 
    {
    	System.out.println("displayChanged called");
    }
 
    public void init(GLAutoDrawable gLDrawable) 
    {
    	System.out.println("init() called");
        GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL2.GL_FLAT);
    }
 
    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) 
    {
    	System.out.println("reshape() called: x = "+x+", y = "+y+", width = "+width+", height = "+height);
        final GL2 gl = gLDrawable.getGL().getGL2();
 
        if (height <= 0) // avoid a divide by zero error!
        {
            height = 1;
        }
 
        final float h = (float) width / (float) height;
 
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }
 
 
	public void dispose(GLAutoDrawable arg0) 
	{
		System.out.println("dispose() called");
	}
}

By right-clicking on the main() method and chosing "run as application", Eclipse will execute the program. You should see something like this:

Jogle Hello World

That's it so far. Please let me know if you have questions or comments.


Trackbacks/Pingbacks

  1. JOGL Download
  2. Building JOGL 2.0 lib properly

17 Antworten

  1. wdaogang says:

    thanks a lot .非常谢谢

  2. Junior says:

    Hi are the import javax.media.opengl.awt.GLCanvas; standand jogl imports or are they showing its from the internet

  3. schabby says:

    There is a JSR on OpenGL/JoGL so I guess it made its way into the giant library by now, but I usually pick the most current binaries from the Jogle project site. You find the URL on my previous post: http://schabby.de/jogl-download/

    I hope this answers your question? :)

  4. luis says:

    I am having linker errors ( I have a 64bit windows 7 os, but both JVM and eclipse are 32bit so I am using JOGL 32 bit too) for some reason it says that it can’t link the native windows 32 I placed all four dlls in the class path. here is a pic of the error http://img259.imageshack.us/img259/2051/97707781.png

  5. luis says:

    Found how to run the program after days of surfing the net.

    ok, I will explain how I managed to run it and set up JOGL at the same time. https://sites.google.com/site/justinscsstuff/jogl-tutorial-1. shows how to create your own user library; once this is done I had some trouble running the dlls which I fixed by placing them in the directory of the project I was working on.

  6. Your example is wrong, you should never have “height” set to zero. If it happens, it means that the height of your canvas is 0. Moreover, your example won’t work with the debug pipeline of JOGL 2.0, a GLException will be thrown very early.

  7. schabby says:

    Hi Julien, thanks for your post! I will correct my example as soon as i get the chance.

    Best Regards!

    Johannes

  8. Shura says:

    Thanks – I’ve tried setting up JOGL so many times, and this is the only way that has worked!

  9. schabby says:

    Hi Shura, cool, I am glad it helped :)

    Johannes

  10. testpilot says:

    Instead of adding a WindowListener for exiting the application, consider using

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    as a shortcut instead.

  11. slash13 says:

    AWESOME tutorial, for me was really hard to find a decent one.
    Thanks a lot

  12. mercutio says:

    Hi,

    By chance, do you also have an example how to run a jogl program as a java applet ?

    Thanks

  13. schabby says:

    Hi!

    I am afraid I dont :( I never really put too much though in applets to be honest. WebGL is about to win the openGL-in-Browsers race from my point of view.

  14. Yogesh says:

    Hello.,
    Thanks for the post., it helped me a lot., but i faced a problem while adding a jar file in eclipse. You have mentioned about adding the following jar files.,
    1. jogl.all.jar
    2. nativewindow.all.jar
    3. gluegen-rt.jar
    I could not find them on my computer, I downloaded the latest zip file from the link you have provided in your post.,
    http://schabby.de/jogl-download/
    I have downloaded the file named “jogl-2.0-b610-20111226-windows-i586.7z” under the link ” jogl-b610-2011-12-26_00-30-55″ dated 26-12-2011. unzipped the file., but i could not find all the jar files mentioned above. I could just find “jogl.all.jar”. Hope you’ll help me with this.

    Thanks,
    Yogesh.

  15. Johannes says:

    If you get the error

    “Can’t load IA 32-bit .dll on a AMD 64-bit platform”

    you downladed the 32-bit binaries, not the 64bit binaries.

Post Comment

Please notice: Comments are moderated by an Admin.


Blogverzeichnis Blog Verzeichnis Blogverzeichnis - Blog Verzeichnis bloggerei.de Blog Top Liste - by TopBlogs.de Bookmark and Share