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");
    worldSGC.setGeometry(Primitives.texturedQuadrilateral(null));
    Appearance ap = new Appearance();
    worldSGC.setAppearance(ap);
    DefaultGeometryShader dgs = ShaderUtility.createDefaultGeometryShader(ap, true);
    dgs.setShowLines(false);
    dgs.setShowPoints(false);
    DefaultPolygonShader dps = (DefaultPolygonShader) dgs.createPolygonShader("default");
    dps.setDiffuseColor(Color.white);
    
    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);
    tex2d.setImage(id);
    // (shouldn't have to do this; but I do)
    bi = (BufferedImage) id.getImage();
    tex2d.setRepeatS(Texture2D.GL_CLAMP_TO_EDGE);
    tex2d.setRepeatT(Texture2D.GL_CLAMP_TO_EDGE);
    tex2d.setMagFilter(Texture2D.GL_NEAREST);
    tex2d.setMinFilter(Texture2D.GL_NEAREST);
    tex2d.setAnimated(true);
    tex2d.setMipmapMode(false);
    final Graphics2D g = bi.createGraphics();
    final GameOfLife gol = new GameOfLife(bi);
 
    tex2d.setRunnable(new Runnable() {
     
      public void run() {
        gol.update();
        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) {
        va.getCurrentViewer().renderAsync();    
      }
       
     });
    timer.start();
 
  }
}
 


The result should look like this:


AnimatedTexture.png



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