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. There is a video-walk-through further down the post, in case you dont feel like reading.
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:
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
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:
For the lazy, I provide a video-walkthough.
Common problems and solutions:
- If you get Can't load AMD 64-bit .dll on a IA 32-bit platform, then you are using an 64bit DLL (eg. gluegen-rt.dee) with an JRE that is 32bit. In most cases you can just switch to a 64bit JRE if you are running a 64bit system. Make sure that Eclipse is using the right JRE when you execute the code.
- If you get no gluegen-rt in java.library.path, then you may need to add gluegen-rt-natives-windows-amd64.jar to you classpath.
- If you get no nativewindow_awt in java.library.path, then you may need to add jogl-all-natives-windows-amd64.jar to you classpath.
That's it so far. Please let me know if you have questions or comments.



thanks a lot .非常谢谢
Hi are the import javax.media.opengl.awt.GLCanvas; standand jogl imports or are they showing its from the internet
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?
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
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.
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.
Hi Julien, thanks for your post! I will correct my example as soon as i get the chance.
Best Regards!
Johannes
Thanks – I’ve tried setting up JOGL so many times, and this is the only way that has worked!
Hi Shura, cool, I am glad it helped
Johannes
Instead of adding a WindowListener for exiting the application, consider using
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
as a shortcut instead.
AWESOME tutorial, for me was really hard to find a decent one.
Thanks a lot
Hi,
By chance, do you also have an example how to run a jogl program as a java applet ?
Thanks
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.
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.
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.
I tried the hello world tutorial and it will work when I run it from eclipse, however when i close the window (click the x) it still leaves and instance of javaw.exe running in the task manager. I have to manually end the process all the time. Any ideas whats causing this?
Hi, the window is independent from the OS process. So closing the window does not terminate the VM. I will enhance the example to shut down the VM once the user closes the window.
Thanks for pointing me at this!!
Johannes
Hi, thank you very much for the video. Was working on similar project and couldn’t execute it for a long time, found your blog on the right time. Truly life saving, keep up the good work.
Verdammt gute Arbeit!
Ich danke vielmals!
Hi, thank you for this post. it helped me. But i tried to change frame’s background color as frame.setBackground(Color.blue).it doesn’t work.
thank you..
Hi Ezra, you are trying to set the background of the Swing Component, which is not part of OpenGL. The command you are looking for is gl.glClearColor(...). I hope this helps, Johannes
ok, thank you ..
Hi,
I have download and do all the stuff that you have said in the video of JOGL in Eclipse ,And after that when i run your first program ‘HelloWorld’ following error is coming.
Can you please help me out? THX
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The type javax.media.nativewindow.Capabilities cannot be resolved. It is indirectly referenced from required .class files
at JOGLHW.HelloWorld.main(HelloWorld.java:21)
Hi Hardik, this is an interesting problem. Does your code completely compile? Are you sure, that you got all necessary libraries added to your classpath? If yes, do you have additional depenencies on the system, such as additional libraries that are part of your project? What JDK are you using? I myself used JDK 6 64bit.
The problem appears to me, as if your current classpath does not contain the necessary javax.media.* stuff. What OS are you using?
Cheers,
Johannes
Hi schabby, i use 64-bit wnidows 7 OS. and also my code is not compile. There is error coming in line ‘GLCanvas glcanvas = new GLCanvas(capabilities);’ saying that ‘the type javax.media.nativwindow.Capabilites cannot be resolved . it is indirectly refrenced from required .class files’