如何在java3d(SimpleUniverse)中设置背景图像

如何在java3d(SimpleUniverse)中设置背景图像,java,image,background,java-3d,Java,Image,Background,Java 3d,大家好,我有个问题。有人能解释一下为什么这里的TextureLoader没有设置我想要的图像作为背景吗?请给我解释一下这个问题。在createSceneGraph()函数中的这部分代码中,我想将位于images文件夹中的sky.jpg图像设置为背景。我应该在代码中修复什么?谢谢你的帮助 package Aplication; import com.sun.j3d.utils.universe.SimpleUniverse; import java.applet.Applet; import

大家好,我有个问题。有人能解释一下为什么这里的TextureLoader没有设置我想要的图像作为背景吗?请给我解释一下这个问题。在createSceneGraph()函数中的这部分代码中,我想将位于images文件夹中的sky.jpg图像设置为背景。我应该在代码中修复什么?谢谢你的帮助

package Aplication;

import com.sun.j3d.utils.universe.SimpleUniverse;

import java.applet.Applet;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;

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.DirectionalLight;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class backGround extends Applet{
    private java.net.URL bgImage = null;

    public backGround(){
        init();
        setLayout(new BorderLayout());
        GraphicsConfiguration config =
                SimpleUniverse.getPreferredConfiguration();

        Canvas3D c = new Canvas3D(config);
        add("Center", c);

        BranchGroup scene = createSceneGraph();
        SimpleUniverse u = new SimpleUniverse(c);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();
        scene.compile();

        u.addBranchGraph(scene);

    }
    public void init(){
        setSize(1280,650);

    }

    public BranchGroup createSceneGraph(){
        BranchGroup firstBranch = new BranchGroup();
        //background.setColor(0.0f,0.0f,2.0f);
        BoundingSphere backgroundBounds = new BoundingSphere(new Point3d(0,0,0), 1000);

        TextureLoader myLoader = new  TextureLoader("../images/sky.jpg",this);
        ImageComponent2D myImage = myLoader.getImage( );
        Background background = new Background();
        background.setImage(myImage);
        background.setApplicationBounds(backgroundBounds);
        firstBranch.addChild(background);

        Shape3D plane = new createPlane().getGeo();
        plane.setBounds(new BoundingSphere());
        firstBranch.addChild(plane);

        AmbientLight lightA = new AmbientLight();
        lightA.setInfluencingBounds(new BoundingSphere());
        firstBranch.addChild(lightA);

        DirectionalLight lightD1 = new DirectionalLight();
        lightD1.setInfluencingBounds(new BoundingSphere());
        Vector3f direction = new Vector3f(-1.0f, -1.0f, -1.0f);
        direction.normalize();
        lightD1.setDirection(direction);
        firstBranch.addChild(lightD1);

        return firstBranch;
    }




public static void main(String argv[]) {
    Frame frame = new MainFrame(new backGround(), 750, 750);
  }
}
/=========与该类一起运行的平面类=======/


创建背景实例并将其附加到分支组,然后附加根场景图

背景可以使用普通RGB颜色、2D纹理或完整的几何体,如球体或长方体

下面是Daniel Selmann书中的一个例子,关于创建一个球体作为背景

 /*
  * Create some Background geometry to use as
  * a backdrop for the application. Here we create
  * a Sphere that will enclose the entire scene and
  * apply a texture image onto the inside of the Sphere
  * to serve as a graphical backdrop for the scene.
  */
 public BranchGroup createBackground(){
 // create a parent BranchGroup for the Background
 BranchGroup backgroundGroup = new BranchGroup();
// create a new Background node
Background back = new Background();
// set the range of influence of the background
back.setApplicationBounds( getBoundingSphere() );
// create a BranchGroup that will hold
// our Sphere geometry 
BranchGroup bgGeometry = new BranchGroup();
// create an appearance for the Sphere
Appearance app = new Appearance(); 
// load a texture image using the Java 3D texture loader   
Texture tex = new TextureLoader( "back.jpg", this).getTexture();
// apply the texture to the Appearance
app.setTexture( tex );
// create the Sphere geometry with radius 1.0.
// we tell the Sphere to generate texture coordinates
// to enable the texture image to be rendered
// and because we are *inside* the Sphere we have to generate 
// Normal coordinates inwards or the Sphere will not be visible.
Sphere sphere = new Sphere( 1.0f,
          Primitive.GENERATE_TEXTURE_COORDS |

          Primitive.GENERATE_NORMALS_INWARD, app );
 // start wiring everything together,
    // add the Sphere to its parent BranchGroup.
    bgGeometry.addChild( sphere );
  // assign the BranchGroup to the Background as geometry.
  back.setGeometry( bgGeometry );
  // add the Background node to its parent BranchGroup.
  backgroundGroup.addChild( back );
  return backgroundGroup;
 }
将背景附着到场景时,请执行以下操作:

  SimpleUniverse u = new SimpleUniverse();
  // create a BranchGroup. A BranchGroup is a node in
  // a Tree data structure that can have child nodes
  BranchGroup bgRoot = new BranchGroup();
  // create the Background node and add it to the SimpleUniverse
  u.addBranchGraph( createBackground() );

你看到窗框了吗??我觉得它能用是的,我看到了框架。这是因为我有另一个关于飞机的类,我会把它贴在这里:刚刚更新了答案,我把画飞机的类放在了那里,这是运行程序的必要条件。感谢您对@gpaschfine的帮助,它可以工作了!你看到了什么?你想看什么?我刚下载了sky.jpg,我想把它作为我的背景图片,而不是我现在的蓝色背景,我不知道为什么TextureLoader不能工作@gpasch谢谢
  SimpleUniverse u = new SimpleUniverse();
  // create a BranchGroup. A BranchGroup is a node in
  // a Tree data structure that can have child nodes
  BranchGroup bgRoot = new BranchGroup();
  // create the Background node and add it to the SimpleUniverse
  u.addBranchGraph( createBackground() );