Tutorial 02 :
Setting up your Java Environment with JOGL

Tutorial Download Section

I use the basic OpenGl container created by Kevin Duling to learn how to use Jogl.
 

You have the choice to use the old Jogl or Jogl JSR-231.

See Tutorial 01
 

First, go to Jogl page at https://jogl.dev.java.net/ and download Jogl files (Don't download JSR 231).
The archives download must contain the jar :
    jogl.jar
and these libraries (Windows):   or    for Linux :
    jogl.dll                           libjogl.so
    jogl_cg.dll                        libjogl_cg.so
(cg library is not used by any of my tutorials)

Place the jar in the folder :
    C:\Program Files\java\j2sdk1.4.2_04\jre\lib\ext
and the libraries in :
    C:\Program Files\java\j2sdk1.4.2_04\jre\bin

Note :
For people who have difficulties to find the old Jogl files, you can find unofficial files here. These files are distributed here without any guaranty.
The best way is probably to use Jogl JSR-231.

 

First, go to Jogl page at https://jogl.dev.java.net/ and download Jogl JSR 231.
The archives download must contain the jar :
    jogl.jar
    gluegen-rt.jar

and these libraries (Windows):   or    for Linux :
    jogl.dll                           libjogl.so
    jogl_awt.dll                       libjogl_awt.so
    jogl_cg.dll                        libjogl_cg.so
    gluegen-rt.dll                     gluegen-rt.so

(cg library is not used by any of my tutorials)

Place the jar in the folder :
    C:\Program Files\java\j2sdk1.4.2_04\jre\lib\ext
and the libraries in :
    C:\Program Files\java\j2sdk1.4.2_04\jre\bin
 

This tutorial contains only changes from gl4java to Jogl. To know what is in '...', take a look at the Tutorial 1 (same code for Gl4java and Jogl).

The differences between Gl4java and Jogl start with the creation of the component that holds the OpenGl scene.
In Jogl, the component class is the class :
    net.java.games.jogl.GLCanvas    //Jogl
    javax.media.opengl.GLCanvas     //Jogl JSR-231
In Jogl, GLCanvas is a final class and the constructor is hidden. So, we can't use new operator to instance a new GLCanvas. To create a GLCanvas, we use GLDrawableFactory.

The code is similar to the first tutorial, I keep here OpenGL related code :

org.jouvieje.jogl/jsr231.tutorials.******Container.java

//Jogl import
import net.java.games.jogl.*;

    ...
   
    /**
     * Creates an OpenGl scene with Jogl
     *
     * First, we creates a GLCanvas with the first line (we can't use : new GLCanvas() !!!)
     * After, we add our events in this GLCanvas (Tutorial2 implements GLEventListener)
     */

    GLCanvas glScene = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());    //Jogl
    GLCanvas glScene = new GLCanvas();                                                         //Jogl JSR-231
    glScene.addGLEventListener(new Tutorial2());
    getContentPane().add(glScene, BorderLayout.CENTER);
   
    ...
 

We have seen in Gl4java that we use differents methods like :
    reshape, init, display
In Jogl, these method are events and defined by the interface :
    net.java.games.jogl.GLEventListener    //Jogl
    javax.media.opengl.GLEventListener     //Jogl JSR-231
The events are :
    init              <=>    init           (in Gl4java)
    reshape           <=>    reshape        (in Gl4java)
    displayChanged    <=>    no equivalent in Gl4java
    display           <=>    drawGlScene    (in Gl4java)

To defines these methods, creates a class that implements the GLEventListener interface.
Underneath, we will define these events like for the tutorial 1.

The code in the events is similar. The difference is that the GL & GLU objects is replaced by a GLDrawable object, which provide access to the GL & GLU object.

Another difference is that the display method is not called in loop. The scene is only render one time.
To animate it, an Animator object is here to call display in loop :
    Animator animator = new Animator(glDrawable);
    animator.start();                              //start the animation loop
To stop the animation, we only have to call :
    animator.stop();                               //stop the animation loop

Please refer you to the first tutorial for the complete commented code (common part).

org.jouvieje.jogl/jsr231.tutorials.Tutorial2

package org.jouvieje.jogl/jsr231.tutorials;

import java.awt.event.*;

import net.java.games.jogl.*;    //Jogl
import javax.media.opengl.*;    //Jogl JSR-231

public class Tutorial2 implements GLEventListener, KeyListener, MouseListener, MouseMotionListener
{
    /**
     * This is a bit different by comparison with Gl4java:
     * Here we have only the OpenGl events.
     * These events are added to the GLCanvas (see the main).
     *
     * In Jogl, GLCanvas class is final (we can't extends this class)
     * and has no constructors.
     *
     * But, the use is similar because we have the same events
     * (reshape, init, display ...)
     */

   
    /**
     * Used to animate the scene
     * specidy if the display must be called in loop
     */

    Animator animator;
   
    //contain the key pressed
    private boolean[] keys = new boolean[256];

    private float zOffset = -5.0f;
   
    /**********************************
     *    The displayChanged event    *
     ********************************************************
     * Called when the display mode has been changed :      *
     *
CURRENTLY UNIMPLEMENTED IN JOGL                      *
     ********************************************************/

    public void displayChanged(GLDrawable glDrawable, boolean modeChanged, boolean deviceChanged){}        //Jogl
    public void displayChanged(GLAutoDrawable glDrawable, boolean modeChanged, boolean deviceChanged){}    //Jogl JSR-231
   
    /****************************
     *    The reshape events    *
     ****************************************************************************
     * This method is called by the GLDrawable at each resize event of the      *
     * OpenGl component.                                                        *
     * It resize the Viewport of the scene and set the Viewing Volume           *
     *                                                                          *
     * Gl4java equivalent : public void reshape(int width, int height)          *
     ****************************************************************************/

    public void reshape(GLDrawable glDrawable, int x, int y, int width, int height)        //Jogl
    public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height)    //Jogl JSR-231
    {
        /*******
         * NEW *
         *******************************************************************
         * GL & GLU classes are equivalent to GLFunc & GLUFunc in GL4java  *
         * - GL provides access to OpenGl functionality and graphics       *
         * card extension                                                  *
         * - GLU provides access to OpenGl utility library routine         *
         *                                                                 *
         * To get the gl & glu objects, we call this :                     *
         * final GL gl = gLDrawable.getGL();                               *
         * final GLU glu = gLDrawable.getGLU();                            *
         *******************************************************************/

        final GL gl = glDrawable.getGL();
        final GLU glu = glDrawable.getGLU();    //Jogl
        final GLU glu = new GLU();              //Jogl JSR-231

        //The following is the same as the reshape method of the first Tutorials
        ...
    }
   
    /*************************
     *    The init method    *
     ***********************************************************
     * Call by the GLDrawable just after the Gl-Context is     *
     * initialized.                                            *
     *                                                         *
     * This method is used to :                                *
     * - activate some modes like texture, light ...           *
     * - affect value to properties                            *
     * - import testures, load your data ...                   *
     *                                                         *
     * Equivalent in Gl4java : public void init()              *
     ***********************************************************/

    public void init(GLDrawable glDrawable)        //Jogl
    public void init(GLAutoDrawable glDrawable)    //Jogl JSR-231
    {
        /*
         * Here, we register all the events listener
         */

        glDrawable.addKeyListener(this);
        glDrawable.addMouseListener(this);
        glDrawable.addMouseMotionListener(this);
       
        final GL gl = glDrawable.getGL();

        //The following is the same as the reshape method of the first Tutorials
        ...
        //End of the common part
       
        //Create the animator to call the display method in loop
        animator = new Animator(glDrawable);
        animator.start();

    }

    /***************************
     *    The display event    *
     *******************************************************************
     * Here we place all the code related to the drawing of the scene. *
     * This method is called by the drawing loop (the display method)  *
     *                                                                 *
     * Gl4java equivalent : public void drawGlScene()                  *
     *******************************************************************/

    public void display(GLDrawable glDrawable)        //Jogl
    public void display(GLAutoDrawable glDrawable)    //Jogl JSR-231
    {
        final GL gl = glDrawable.getGL();
       
        //The following is the same as the drawGlScene method of the first Tutorials
        ...
    }

    ...
}

 

See Tutorial 1


If you've got any remarks on this tutorial, please let me know to improve it.
Thanks in advance for your feedback.
 

Previous Tutorial

Back

Next turorial

Copyright © 2004-2012 Jérôme Jouvie - All rights reserved. http://jerome.jouvie.free.fr/