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 9
Page 9 of 9

Work with textures

Computer games achieve realism by mapping images onto polygonal shapes -- mapping a missile's image onto a polygon-based missile object can result in a realistic-looking missile, for example. The image data that's mapped onto a shape is known as a texture. Although you can work with textures via various methods in the GL class, JOGL provides several classes, starting with those below, that simplify this task:

  • com.sun.opengl.util.texture.Texture
  • com.sun.opengl.util.texture.TextureCoords
  • com.sun.opengl.util.texture.TextureIO

The Texture class represents an OpenGL texture object, the TextureCoords class specifies the texture coordinates for a rectangular area of the texture, and the TextureIO class lets you read textures from disk or other sources (and also write memory-based textures back to disk). Listing 4's JOGLDemo5 application source code demonstrates all three classes.

Listing 4. JOGLDemo5.java

// JOGLDemo5.java

// This demo renders a textured cube, with each side displaying a different
// texture.

import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

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

import javax.swing.*;

import com.sun.opengl.util.*;
import com.sun.opengl.util.texture.*;

public class JOGLDemo5 extends JFrame

...

Download the complete Listing 4.

JOGLDemo5.java describes an application that animates a cube, rotating it in randomly-chosen directions around the X, Y, and Z axes. The three aforementioned texture classes are used to load six textures based on nebula images and render these textures onto the cube's six surfaces. Figure 7 reveals one frame of the animation.

Six JPEG-based nebula images form the basis of this rotating cube's textures.
Figure 7. Six JPEG-based nebula images form the basis of this rotating cube's textures.

You could reduce the jagged edges by using polygon antialiasing. However, you would have to turn off depth buffer testing and manually sort the six polygons from front to back, which could be somewhat tricky. Alternatively, you could antialias the entire scene by taking advantage of the accumulation buffer. However, this form of antialiasing can leave the scene somewhat blurry, as Figure 8 reveals.

Full-scene antialiasing typically results in a somewhat blurry image.
Figure 8. Full-scene antialiasing typically results in a somewhat blurry image.

In conclusion

JOGL offers a powerful and easy-to-use API for accessing OpenGL to render 3D (and 2D) graphics. Lack of space prevented me from digging deeper into JOGL; there's much more for you to explore. You might want to begin by investigating JOGL's partial support for Non-uniform rational B-splines (NURBs), and its com.sun.opengl.util.JOGLAppletLauncher class for deploying JOGL-based applets.

Jeff Friesen is a freelance software developer and educator who specializes in Java technology. Check out his javajeff.mb.ca website to discover all of his published Java articles and more.

Learn more about this topic

More

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