Sorry

Create your own JOGL demos

JOGL provides an API for creating programs that interact with OpenGL. Although this API consists of nearly 70 classes and interfaces (organized into seven packages), you don't need to master the entire API before you can start working with JOGL. For proof, this section focuses on only a small part of the API while demonstrating how to create a pair of JOGL programs via event-based rendering.

Event-based rendering

One approach to creating a JOGL program involves event-based rendering. With this approach, a program creates a GUI component that presents rendered content, creates a listener that renders this content, and associates the listener with the component. From time to time, the component sends an event to the listener, which invokes various OpenGL rendering operations via equivalent JOGL methods.

Active rendering

In his book, Pro Java 6 3D Game Development, Dr. Andrew Davison examines two approaches to creating a JOGL program: callbacks (which I refer to as event-based rendering) and active rendering (which is more appropriate for game development). For space reasons, I only focus on event-based rendering in this article. If you'd like to learn about active rendering, you can download a PDF draft of this book's fifteenth chapter.

The component's class implements the javax.media.opengl.GLAutoDrawable interface. This interface, which inherits from the javax.media.opengl.GLDrawable interface, essentially turns the component into a rendering surface (a drawable) on which graphics are rendered. Behind the scenes, the drawable creates a rendering context that connects the rendering pipeline to the drawable's window.

What's a viewport and a view volume?

A viewport is the region of the drawable's surface in which graphics are rendered. A view volume specifies the 3D volume in space that (after being projected) fits within the viewport.

The listener's class implements the javax.media.opengl.GLEventListener interface. Once the listener has been registered with the drawable via GLAutoDrawable's public void addGLEventListener(GLEventListener listener) method, the drawable sends events to the listener by invoking GLEventListener's four methods:

public void init(GLAutoDrawable drawable) is called after the rendering context initializes. The listener responds by creating display lists, setting the background color, and performing other initialization tasks. This method may be called more than once if the GLAutoDrawable's context is destroyed and recreated -- removing the drawable from and later adding it to the GUI, for example.

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) is called immediately after init() and each time the drawable is subsequently resized. The listener typically responds by updating the viewport and view volume. Before this method is called, the drawable sets the viewport to the same values as the last four arguments passed to this method.

public void display(GLAutoDrawable drawable) is called after reshape(), to render a scene to OpenGL's back buffer. After all listeners have been notified of a display event, the drawable automatically swaps its front and back buffers, unless you disable this behavior via drawable.setAutoSwapBufferMode (false);.

public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) is intended to be called when a mode change (such as reducing the number of screen device colors) or a device change (such as dragging a window containing the drawable to another monitor) occurs. JOGL's reference implementation doesn't implement this method.