JOGL Game Template

(1/1)

kingthomas:
I started learning JOGL recently so I could make Java games with accelerated graphics and make my own game engine, but had difficulty starting out. I thought I'd try to help other's in that situation.

First, download the latest release of JOGL from here for your OS. Add jogl.all.jar, nativewindow.all.jar, gluegen-rt.jar, and optionally newt.all.jar to your CLASSPATH environmental variable. Also, add the "lib" folder to your PATH environment variable (Windows), LD_LIBRARY_PATH environment variable (Solaris and Linux), or DYLD_LIBRARY_PATH environment variable (Mac OS X). Now you should be all set up to build and run JOGL programs.

Here is a template for a JOGL program.

Code:

import java.awt.*;
import java.awt.event.*;

import com.sun.opengl.util.*;

import javax.media.opengl.*;
import javax.media.opengl.awt.*;

public class Driver {
public static void main(String[] args) {
Frame frame = new Frame("JOGL Demo");
GLCanvas canvas = new GLCanvas();

final MyEventListener gears = new MyEventListener();
canvas.addGLEventListener(gears);

frame.add(canvas);
frame.setSize(300, 300);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setVisible(true);
animator.start();
}

public static class MyEventListener implements GLEventListener {

public void display(GLAutoDrawable glDraw) {
GL2 gl = glDraw.getGL().getGL2();
// TODO add drawing code here

}

public void dispose(GLAutoDrawable glDraw) {
// TODO add clean-up code here

}

public void init(GLAutoDrawable glDraw) {
GL2 gl = glDraw.getGL().getGL2();
// TODO add initialization code here

}

public void reshape(GLAutoDrawable glDraw, int x, int y, int width, int height) {
// reset the view when the window is resized
GL2 gl = glDraw.getGL().getGL2();

float h = (float)height / (float)width;

gl.glMatrixMode(GL2.GL_PROJECTION);

gl.glLoadIdentity();
gl.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();

gl.glTranslatef(0.0f, 0.0f, -40.0f);
}
}
}

RedSlash:
This is great! They should really make jogl easier to install.

kingthomas:
The easiest way is to use the NetBeans plug-in [url = http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp;jsessionid=0029d802feb1f0bdc86035afcdce?pluginid=3260]here[/url]. That is if you use NetBeans, of course.

Navigation

[0] Message Index