Use an animated 2d texture

From JReality Wiki
Jump to: navigation, search

Source file: AnimatedTextureExample

Source file: GameOfLife

JavaDoc: Texture2D

Run as Java webstart

This example shows an animated 2d texture which maps a live game of life onto a quadrilateral.

  • The source code for the game of life comes from the class GameOfLife.
  • The important thing, is that one provides n instance of Runnable which updates the BufferedImage which is attached to the texture.
    • This is executed each frame that the texture is rendered.
    • If the texture appears in several nodes of the scene, it's only updated once each frame.
    • If the Runnable is null, then the user is expected to update the buffered image himself, not synchronized with the rendering.
  • Mipmapping is turned off since it can make a big performance hit, and this example doesn't want to interpolate texture values anyway.
  • Since the animated texture in this case is triggered by the rendering itself, there's a timer which forces the viewer to render regularly. [June 10: this timer is temporarily out of action, rotate the scene to get the texture to animate if it doesn't automatically do so.]

public class AnimatedTextureExample {
  static int count = 0;
  public static void main(String[] args)    {
    SceneGraphComponent worldSGC = new SceneGraphComponent("AnimatedTextureExample");
    Appearance ap = new Appearance();
    DefaultGeometryShader dgs = ShaderUtility.createDefaultGeometryShader(ap, true);
    DefaultPolygonShader dps = (DefaultPolygonShader) dgs.createPolygonShader("default");
    final Texture2D tex2d = (Texture2D) AttributeEntityUtility
         .createAttributeEntity(Texture2D.class, "polygonShader.texture2d", ap, true);
    final int width = 128, height = 128;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    final ImageData id = new ImageData(bi);
    // (shouldn't have to do this; but I do)
    bi = (BufferedImage) id.getImage();
    final Graphics2D g = bi.createGraphics();
    final GameOfLife gol = new GameOfLife(bi);
    tex2d.setRunnable(new Runnable() {
      public void run() {
        Image current = gol.currentValue();
        g.drawImage(current, 0, 0, null);
     JRViewer display(worldSGC);
     // to update the texture
     Timer timer = new Timer(20, new ActionListener()  {
      public void actionPerformed(ActionEvent e) {

The result should look like this:


Previous: Use a 2d texture Developer Tutorial: Contents Next: Use a skybox and reflection map