Java也许是最流行的真正的面向对象的编程语言。有许多用Java去结合OpenGL的尝试,但是第一个被大家认可并注意的是Java对于OpenGl的绑定(Java Bindings for OpenGL), 或者称为JOGL.理由是它得到Sun(Java的创建者)和SGI(OpenGL的创建者)的支持。
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 outwhere 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 jarfolder with a number of JAR files. All you need is in that jarfolder, 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.helloworldand copypaste the following two classes in the newly created package.
packagede.schabby.jogl.helloworld;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjavax.media.opengl.GLCapabilities;importjavax.media.opengl.GLProfile;importjavax.media.opengl.awt.GLCanvas;importjavax.swing.JFrame;publicclass HelloWorld
{publicstaticvoid 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(newRenderer());
glcanvas.setSize(300, 300);JFrame frame =newJFrame("Hello World");
frame.getContentPane().add( glcanvas);// shutdown the program on windows close event
frame.addWindowListener(newWindowAdapter(){publicvoid windowClosing(WindowEvent ev){System.exit(0);}});
frame.setSize( frame.getContentPane().getPreferredSize());
frame.setVisible(true);}}
The Render.java class looks like follows. It implements GLEventListenerwhich is the call-back implementaiton by JOGL to do all OpenGL rendering.