OpenGL:Tutorials:Java:JOGL:Introduction
From GPWiki(Redirected from Java:JOGL Beginning)
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello.
[edit] Getting JOGL
Now you should be ready to use JOGL! [edit] Setting Up Your First JOGL ApplicationThere'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. [edit] The FrontendThere 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 javax.media.opengl.*; // If thise does not work, try net.java.games.jogl.*, 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. app.setUndecorated(true); app.setVisible(true); // Goes into fullscreen-exclusive mode GraphicsDevice gd = GraphicsEnvironment. getLocalGraphicsEnvironment().getDefaultScreenDevice(); gd.setFullScreenWindow(app); } } ); // Starts the Animator in a new thread. SwingUtilities.invokeLater ( new Runnable() { public void run() { anim.start(); } } ); } public JOGLApp() { // Set the JFrame title super("JOGL Application"); // Kill the process when the JFrame is closed setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 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); } } [edit] The Event ListenerWhenever 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 javax.media.opengl.*; // If thise does not work, try net.java.games.jogl.*, 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); gl.glViewport(0,0,500,300); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, 800.0f / 600.0f, 1.0f, 500.0f); gl.glMatrixMode(GL.GL_MODELVIEW); } // 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.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f,0.0f,-5f); gl.glRotatef(rotate,0.0f,0.0f,1.0f); gl.glBegin(GL.GL_TRIANGLES); gl.glVertex3f( 0.0f, 1.5f,0.0f); gl.glVertex3f(-1.0f,-1.0f,0.0f); gl.glVertex3f( 1.0f,-1.0f,0.0f); gl.glEnd(); 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. [edit] 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. |


