簡體   English   中英

如何在 java3d (SimpleUniverse) 中設置背景圖像

[英]how to set backGround image in java3d (SimpleUniverse)

大家好,我有一個問題。 有人可以向我解釋為什么這里的 TextureLoader 沒有將我想要的圖像設置為我的背景嗎? 請向我解釋問題。 在createSceneGraph() 函數中放置的這部分代碼中,我想將位於images 文件夾中的sky.jpg 圖像設置為backGround。 我應該在我的代碼中修復什么? 謝謝你的幫助。

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);
  }
}

/ ==========plane 與該類一起運行的類======== /

package Aplication;

import javax.media.j3d.Appearance;
import javax.media.j3d.Geometry;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Material;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;


public class createPlane extends Shape3D {
    Shape3D plane;
      public class LitQuad extends Shape3D {
            LitQuad(Point3f A, Point3f B, Point3f C, Point3f D) {
              this.setGeometry(createGeometry(A, B, C, D));
              this.setAppearance(createAppearance());
            }


      Geometry createGeometry(Point3f A, Point3f B, Point3f C, Point3f D) {

          QuadArray plane = new QuadArray(4, GeometryArray.COORDINATES
              | GeometryArray.NORMALS);

          plane.setCoordinate(0, A);
          plane.setCoordinate(1, B);
          plane.setCoordinate(2, C);
          plane.setCoordinate(3, D);

          Vector3f a = new Vector3f(A.x - B.x, A.y - B.y, A.z - B.z);
          Vector3f b = new Vector3f(C.x - B.x, C.y - B.y, C.z - B.z);
          Vector3f n = new Vector3f();
          n.cross(b, a);

          n.normalize();

          plane.setNormal(0, n);
          plane.setNormal(1, n);
          plane.setNormal(2, n);
          plane.setNormal(3, n);

          return plane;
        }

       Appearance createAppearance() {
          Appearance appear = new Appearance();
          Color3f color = new Color3f(0.0f,1.5f,0.0f);
          Material material = new Material();
          material.setDiffuseColor(color);
          appear.setMaterial(material);

          return appear;
        }
      }

      public createPlane(){
            plane = new LitQuad(new Point3f(-1.5f, -0.5f, -5.0f),
                    new Point3f(-1.5f, -0.5f, 1.0f),
                    new Point3f(1.5f, -0.5f, 1.0f), new Point3f(1.5f, -0.5f, -5.0f));
      }
      public Shape3D getGeo(){
          return plane;
      }
}

創建一個背景實例並將其附加到 BranchGroup,然后附加您的根場景圖。

背景可以使用純 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() );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM