From GPWiki
Jump to: navigation, search

Getting JOGL

  • Before you can use JOGL, you must first download the bindings. You can do that from the JOGL project page.
    • Be sure to get jogl.jar and the natives relating to your operating system.
    • The javadocs are nice to have too, if you're into that sort of thing.
  • Once it's downloaded, put jogl.jar into the /lib/ext directory of your JRE.
  • Next, you have to extract the files from the natives JAR (Why'd they use a JAR anyway?) and then put them into the /bin directory of your JRE.

Now you should be ready to use JOGL!

Setting Up Your First JOGL Application

There's one small hoop that you must jump through before you can start using OpenGL in Java. Java is strictly an object-oriented language. OpenGL was developed in a non-object-oriented language. That means that to be able to use OpenGL in Java, it must be grouped into objects. Fortunately, this has already been done for us.

Two classes are needed to setup a JOGL Application: a frontend of some sort that provides a window, and an OpenGL event listener, since OpenGL communicates with the program via events.

The Frontend

There isn't much to this part. You need to provide a window to use OpenGL, and JFrames work quite nicely for that. This is also a good place to put your Animator.

The only other things that you might want in this class are input event listeners, but that won't be covered here.

import java.awt.*;
import javax.swing.*;
import*; // If thise does not work, try*, or get the newest version of JOGL.
public class JOGLApp extends JFrame {
	// Causes our JOGL app to be animated- that is, it keeps calling display()
	static Animator anim = null;
	public static void main(String[] args) {
		final JOGLApp app = new JOGLApp();
		// Creates the window in another thread
		SwingUtilities.invokeLater (
				new Runnable() {
					public void run() {
						// Removes the normal title bar. Most games do this.
						// Goes into fullscreen-exclusive mode
						GraphicsDevice gd = GraphicsEnvironment.
		// Starts the Animator in a new thread.
		SwingUtilities.invokeLater (
				new Runnable() {
					public void run() {
	public JOGLApp() {
		// Set the JFrame title
		super("JOGL Application");
		// Kill the process when the JFrame is closed
		GLCapabilities glCaps = new GLCapabilities();
		GLCanvas glCanvas = new GLCanvas(glCaps);
		glCanvas.addGLEventListener(new GameGLEventListener());
		// Add the GLCanvas just like we would any Component
		getContentPane().add(glCanvas, BorderLayout.CENTER);
		anim = new Animator(glCanvas);

The Event Listener

Whenever your program is ready to draw, an event will be triggered, and the display() function in this class will be called. Another possible event is when the program starts. The init() function will be called then. This only happens at the beginning. Other events include reshape(), when the shape of the window changes, and displayChanged(), when the display settings have changed, either because of this program, another program, or the user decided to change them.

import*; // If thise does not work, try*, or get the newest version of JOGL.
public class GameGLEventListener implements GLEventListener {
	float rotate = 0;
	// Only put stuff here that should happen once, at the beginning of the program
	public void init(GLDrawable gld) {
		GL gl = gld.getGL();
		GLU glu = gld.getGLU();
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		glu.gluPerspective(45.0f, 800.0f / 600.0f, 1.0f, 500.0f);
	// This function will get called repeatedly by the Animator. Think of it as your game loop.
	public void display(GLDrawable gld) {
		GL gl = gld.getGL();
			gl.glVertex3f( 0.0f, 1.5f,0.0f);
			gl.glVertex3f( 1.0f,-1.0f,0.0f);
		rotate = rotate + 0.5f;
	// This function is only called when the window is reshaped. You probably won't need this. 
	public void reshape(GLDrawable drawable, int x, int y, int width, int height) {}
	// This function is only called when the display settings are changed. Again, not often used.
	public void displayChanged(GLDrawable drawable, boolean a, boolean b) {}

If the code in the functions looks familiar, it's because I've borrowed, and adapted it from another page in the wiki. It just goes to show that whatever can be done with a C OpenGL game can (probably) be done with a Java OpenGL game too. And since OpenGL is OpenGL, the speeds should be very close.

Note: It is a good idea to obtain a new GL everytime display() is called.

Where to go From Here?

Now that you can setup a JOGL application, you should be able to figure out how to use the OpenGL code written for C/C++. Just remember that if a function starts with gl, it will probably be in the GL object. If it starts with glu, it will probably be in the GLU object. Consult the javadocs, or post on the message board if you need more help.


  • JOGL tutorials A collection of tutorials for beginners to JOGL and OpenGL.