Open source Java projects: Java Binding for OpenGL (JOGL)

A low-level Java API for 3D graphics

1 2 3 4 5 6 7 8 9 Page 6
Page 6 of 9

Working with GLJPanel

Unlike GLCanvas, GLJPanel plays nicely with Swing GUIs. For example, you can subclass GLJPanel and have the subclass's paintComponent() method render Java2D content (such as a gradient or an image) as a background to an OpenGL scene. (You can also render Java2D content over the scene, possibly to achieve a head-up display.)

Before you can render Java2D graphics behind an OpenGL scene, you need to assign a non-zero alpha depth to the drawable, by creating a javax.media.opengl.GLCapabilities object, by invoking this object's public void setAlphaBits(int alphaBits) method (typically eight alpha bits), and by passing this object to a GLJPanel superclass constructor that takes a GLCapabilities argument.

In addition to assigning a non-zero alpha depth to the drawable, you need to make the drawable non-opaque so that Java2D-rendered content behind the scene will show through. Accomplish this task by invoking GLJPanel's public void setOpaque(boolean opaque) method with a false argument. The following code fragment demonstrates both tasks:

GLCapabilities caps = new GLCapabilities ();
caps.setAlphaBits (8);
SceneRenderer sr = new SceneRenderer (caps);

// ...

class SceneRenderer extends GLJPanel ...
{
   SceneRenderer (GLCapabilities caps)
   {
      super (caps);
      setOpaque (false);
   }
}

Listing 2 presents the source code to a JOGLDemo2 application that shows you how to architect a JOGL program based on GLJPanel and GLEventListener.

Listing 2. JOGLDemo2.java

// JOGLDemo2.java

// This demo renders a torus over a Java2D-rendered image of the sky.

import java.awt.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;

import javax.swing.*;

import com.sun.opengl.util.*;

public class JOGLDemo2 extends JFrame

...

Download the complete Listing 2.

JOGLDemo2.java presents a few items that need to be clarified. First, the display() method employs GLJPanel's public boolean shouldPreserveColorBufferIfTranslucent() method to determine if it should clear the color buffer. If this method returns true, as when the OpenGL rendering pipeline is enabled via -Dsun.java2d.opengl=true, the color buffer shouldn't be cleared.

JOGLDemo2 takes advantage of JOGL's com.sun.opengl.util.GLUT class to access the OpenGL Utility Toolkit (GLUT). Before accessing the GLUT, this class is instantiated by invoking its public GLUT() constructor -- as with GLU, GLUT's constructor can be called from outside GLEventListener methods.

Although GLUT lets you render a variety of geometric objects, display() is only concerned with rendering a torus (a doughnut shape). It accomplishes this task by invoking GLUT's public void glutSolidTorus(double innerRadius, double outerRadius, int nsides, int nrings) method:

  • innerRadius specifies the radius of the cylindrical tube that surrounds the "doughnut hole"
  • outerRadius specifies the radius from the center of the "doughnut hole" to the tube's outer wall
  • nsides specifies the shape of the tube wall; the greater the value, the rounder the tube wall
  • nrings specifies the shape of the tube; the greater the value, the rounder the tube

This method renders a solid torus that's centered at the origin of the modeling coordinate system, and whose Z axis is aligned with the camera's Z axis. (Alternatively, you can use GLUT's public void glutWireTorus(double innerRadius, double outerRadius, int nsides, int nrings) method to render a wireframe torus with the same origin and alignment.)

Finally, the reshape() method works with the GLU class's public void gluPerspective(double fovy, double aspect, double zNear, double zFar) method, which offers an easier-to-use alternative to glFrustum() that's based on a field-of-view angle and an aspect ratio (which should be set to the viewport's aspect ratio to prevent distortion):

  • fovy specifies the field-of-view angle (in degrees) in the Y direction
  • aspect specifies the aspect ratio (the ratio of width to height) of the field of view in the X direction
  • zNear specifies the positive distance from the viewpoint to the near clipping plane
  • zFar specifies the positive distance from the viewpoint to the far clipping plane

Assuming Windows and the same setup as before, compile JOGLDemo2.java via the following command line:

javac -cp "c:\jogl\jogl.jar" JOGLDemo2.java

Run this application via the command line below:

java -cp "c:\jogl\jogl.jar;c:\jogl\gluegen-rt.jar;." -Djava.library.path="c:\jogl" JOGLDemo2

Figure 5 reveals the resulting scene.

Homer Simpson would enjoy this UFO!
Figure 5. Homer Simpson would enjoy this UFO!
Related:
1 2 3 4 5 6 7 8 9 Page 6
Page 6 of 9