Transcript Class 16

Java and OpenGL
Rich Truban
Andrew Potozniak
Java + OpenGL = JOGL
Getting Started
Java 5 SDK
http://java.sun.com/j2se/1.5.0/download.jsp
Eclipse IDE
http://www.eclipse.org/
(http://download.eclipse.org/eclipse/downloads/drops/R-3.0.1-200409161125/eclipse-SDK-3.0.1-win32.zip)
JOGL’s Main site
https://jogl.dev.java.net/
Installing JOGL
• Download:
– The JAR file:
• jogl.jar
– The Natives:
• jogl-natives-linux.jar
• jogl-natives-win32.jar
Installing JOGL into the JRE
• Find the directory where your JRE is
installed.
• Windows & Linux (Global Install)
– Drop the JOGL.jar file into:
• jre1.5.0/lib/ext/
– Drop the natives into:
• jre1.5.0/bin/
Incorporating JOGL into Eclipse
• Make a new Java Project
– Create a lib/ directory
– Import the sample code
• Drop JOGL.jar into the lib directory
– You can drop the natives into this directory as
well.
• Had to do this as well as another little trick to make
JOGL work in Linux.
Simple JOGL test code
public class JOGLTest {
public static void main(String args[]) {
try {
//Load the libraries
System.loadLibrary("jogl");
System.out.println("Hello World! (The native libraries are installed.)");
//Test for the presence of the JAR
GLCapabilities glCaps = new GLCapabilities();
System.out.println("Hello JOGL! (The jar appears to be available.)");
} catch (Exception e) {
System.out.println(e);
}
}
}
Testing JOGL
• Run the sample program
• If it fails, make sure you installed the
JOGL.jar and JOGL natives in the correct
places.
• If it runs, you’re well on your way to make
OGL programs in Java
Setting up a GLCanvas
public class Main extends JFrame {
private static GLCanvas glCanvas;
private GraphicEngine ge;
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GLCapabilities glCaps = new GLCapabilities();
glCaps.setDoubleBuffered(true);
glCaps.setHardwareAccelerated(true);
glCanvas = GLDrawableFactory.getFactory().createGLCanvas(glCaps);
ge = new GraphicEngine();
glCanvas.addGLEventListener(ge);
getContentPane().add(glCanvas);
setSize(640, 480);
}
public static void main(String args[]) {
Main main = new Main();
main.setVisible(true);
while(true){glCanvas.repaint();}
}
}
Setting up the GLEventListener
public class GraphicEngine implements GLEventListener{
public void init(GLDrawable gld){
gld.getGL().glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
public void display(GLDrawable gld){
GL gl = gld.getGL();
GLU glu = gld.getGLU();
GLUT glut = new GLUT();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
}
//Have to implement these methods for the GLEventListener
public void displayChanged(GLDrawable gld, boolean arg1, boolean arg2) { }
public void reshape(GLDrawable gld, int x, int y, int width, int height) { }
}
Any questions so far?
Except for that one Anton!
Why did we do that? Because we can!
Let’s add some spice to the mix!
• Adding a GL_QUADS call
• Adding a GLUT object
Advantages of using Java
•
•
•
•
User-friendly and Robust Development Environment
Platform independent
Javadoc API (http://java.sun.com/j2se/1.5.0/docs/api/)
Let’s just face it. It’s a lot better than Python <smirk>