1 Reply Latest reply: May 10, 2010 11:09 AM by 843799 RSS

    Changing Background of BranchGroup

    843799
      Hi everybody,

      I'm currently working on a java 3d project and I'm having some problems with the BranchGroup and its background. The GUI is based on a Java3D Applet, which should view a 3D model and a webcam in background. I'm getting every 1/4 sec a new BufferedImage from webcam and I want to put it in background.

      I have a working code for a fixed background like this:
      import com.sun.j3d.loaders.Loader;
      import com.sun.j3d.loaders.Scene;
      import com.sun.j3d.loaders.objectfile.ObjectFile;
      import com.sun.j3d.utils.image.TextureLoader;
      import com.sun.j3d.utils.universe.SimpleUniverse;
      import java.applet.Applet;
      import java.awt.BorderLayout;
      import java.awt.Graphics2D;
      import java.awt.GraphicsConfiguration;
      import java.awt.GraphicsEnvironment;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import java.io.IOException;
      import javax.imageio.ImageIO;
      import javax.media.j3d.AmbientLight;
      import javax.media.j3d.Background;
      import javax.media.j3d.BoundingSphere;
      import javax.media.j3d.BranchGroup;
      import javax.media.j3d.Canvas3D;
      import javax.media.j3d.GraphicsConfigTemplate3D;
      import javax.media.j3d.PointLight;
      import javax.media.j3d.Transform3D;
      import javax.media.j3d.TransformGroup;
      import javax.vecmath.Color3f;
      import javax.vecmath.Point3d;
      import javax.vecmath.Point3f;
      import javax.vecmath.Vector3f;
      
      /**
       *
       * @author clemens
       */
      public class CrossingFrame3D extends Applet {
      
          private BranchGroup scene;
          private SimpleUniverse u;
          private Background background;
      
          public CrossingFrame3D() throws Exception {
              setLayout(new BorderLayout());
              Canvas3D canvas = createCanvas();
              add("Center", canvas);
              u = new SimpleUniverse(canvas);
      
              scene = createContent();
      
              TransformGroup vtg = u.getViewingPlatform().getViewPlatformTransform();
              Transform3D moveback = new Transform3D();
              moveback.setTranslation(new Vector3f(2.0f, 2.0f, 10.0f));
              vtg.setTransform(moveback);
      
              BufferedImage image = ImageIO.read(new File("test.jpg"));
              BufferedImage resizedImage = new BufferedImage(640, 480, image.getType());
              Graphics2D g = resizedImage.createGraphics();
              g.drawImage(image, 0, 0, 640, 480, null);
              g.dispose();
              BoundingSphere boundingSphere = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
              TextureLoader backgroundTexture = new TextureLoader(resizedImage, this);
              background = new Background(backgroundTexture.getImage());
              background.setApplicationBounds(boundingSphere);
              scene.addChild(background);
      
              scene.compile();
              u.getViewer().getView().setBackClipDistance(10000.0);
              u.addBranchGraph(scene);
          }
          private Canvas3D createCanvas() {
              GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
              GraphicsConfiguration gc1 = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(template);
              return new Canvas3D(gc1);
          }
      
          private BranchGroup createContent() throws Exception {
              Loader loader = new ObjectFile(ObjectFile.LOAD_ALL);
      
              // Use these 4 lines to load from a local file
              java.io.File file = new java.io.File("cow.obj");
              if (file.getParent() != null) {
                  if (file.getParent().length() > 0)
                  {
                      loader.setBasePath(file.getParent() + java.io.File.separator);
                  }
              }
              Scene newScene = loader.load(file.getName());
      
              BranchGroup group = newScene.getSceneGroup();
              createLights(group);
              return group;
          }
      
          private void createLights(BranchGroup graphRoot) {
              // Create a bounds for the light source influence
              BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
      
              // Set up the global, ambient light
              Color3f alColor = new Color3f(0.3f, 1.0f, 0.3f);
              AmbientLight aLgt = new AmbientLight(alColor);
              aLgt.setInfluencingBounds(bounds);
              graphRoot.addChild(aLgt);
      
              // Set up the point (infinite) light source
              Point3f location = new Point3f(0.0f, 0.0f, 100.0f);
              PointLight lgt1 = new PointLight(new Color3f(1.0f, 1.0f, 1.0f),
                      location,
                      new Point3f(1.0f, 0.0f, 0.0f));
              lgt1.setInfluencingBounds(bounds);
              graphRoot.addChild(lgt1);
          }
          
          public void updateFrame(BufferedImage img) throws IOException {
              u.cleanup();
      
              BufferedImage image = ImageIO.read(new File("test.jpg"));
              BufferedImage resizedImage = new BufferedImage(640, 480, image.getType());
              Graphics2D g = resizedImage.createGraphics();
              g.drawImage(image, 0, 0, 640, 480, null);
              g.dispose();
              TextureLoader backgroundTexture = new TextureLoader(resizedImage, this);
              background.setImage(backgroundTexture.getImage());
          }
      }
      If I now want to change the background by calling the updateFrame methode, I get this message:
      Exception in thread "main" javax.media.j3d.CapabilityNotSetException: Background: no capability to set image
              at javax.media.j3d.Background.setImage(Background.java:428)
              at CrossingFrame3D.updateFrame(CrossingFrame3D.java:123)
              at CrossingFrame3D.<init>(CrossingFrame3D.java:69)
              at Main.main(Main.java:13)
      Can anybody please help and tell me how to change the background when getting every 1/4 sec a new BufferedImage? Thanks!