⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 house.java

📁 本项目是用JAVA3D开发的一款图形界面的3D漫游的类似引擎.至所以这么说.是因为它的部分功能还不完全.说它是引擎是因为它可以完全脱离模型文件.本引擎实现了虚拟漫游,碰撞检测,动态添加模型,以及部分纹
💻 JAVA
字号:
package cn.bz.branchGroup;

import java.awt.BorderLayout;
import java.util.Vector;

import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
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.Material;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JPanel;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import cn.bz.util.NodeManager;

import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class House extends JPanel{
	BranchGroup House_buttom;
	BranchGroup bg_bottom;
	NodeManager nodeManager = new NodeManager();
	  public House() {
		  setLayout(new BorderLayout());
	    	// 设置一个BorderLayout
	    Canvas3D c=new Canvas3D(null);
	    	// 构造一个图形环境
	    add("Center",c);
	    	// 将图形环境放入BorderLayout的中心位置
	    SimpleUniverse u=new SimpleUniverse(c);
	    	// 生成虚拟空间
	    u.getViewingPlatform().setNominalViewingTransform();
	    	// 设置观察者的位置为缺省位置
	    //BranchGroup scene=new sphere().sphere();
	    BranchGroup scene=createSceneGraph();
			// 生成一个场景图分支,在其中定义分支的形体、材质、灯光等信息
	    
	    new NodeManager().addNote(u, scene);
	    	// 把场景放入虚拟空间中
  }
	public BranchGroup createHouse(String filename) {
	    House_buttom = nodeManager.getObj(filename);
	    return House_buttom ;
	}
	public BranchGroup createSceneGraph() {
	    // Create the root of the branch graph
	    BranchGroup sceneRoot = new BranchGroup();

	    // Create a bounds for the background and lights
	    BoundingSphere bounds =
	      new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
	    // Set up the background Color
	    Color3f bgColor = new Color3f(1.0f, 1.0f, 1.0f);
	    Background bg = new Background(bgColor);
	    bg.setApplicationBounds(bounds);
	    sceneRoot.addChild(bg);

	    // Set up the global lights
	    // First, define the color of the lights
	    Color3f light1Color = new Color3f(0.0f, 1.0f,1.0f);
	    Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);
	    Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
	    Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);
	    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);

	    // Second, define the ambient light, and insert it in the branch
	    AmbientLight ambientLight = new AmbientLight(ambientColor);
	    ambientLight.setInfluencingBounds(bounds);
	    sceneRoot.addChild(ambientLight);

	    // Lastly, define the directional lights and insert it
	    DirectionalLight light1
	      = new DirectionalLight(light1Color, light1Direction);
	    light1.setInfluencingBounds(bounds);
	    sceneRoot.addChild(light1);

	    DirectionalLight light2
	      = new DirectionalLight(light2Color, light2Direction);
	    light2.setInfluencingBounds(bounds);
	    sceneRoot.addChild(light2);

	    // Create the scene's transform group node (starts as
	    // identity).  Enable the TRANSFORM_WRITE capability so that
	    // our behavior code can modify it at runtime.  Note, we DO
	    // NOT set the TRANSFORM_READ capability because we only do
	    // writes. Add it to the root of the branch graph
	    TransformGroup sceneTG = new TransformGroup();
	    sceneTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	    sceneRoot.addChild(sceneTG);

	    // Create an Alpha object that goes from 0 to 1 in 8 seconds,
	    // repeatedly.
	    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
					    0, 0,
					    4000, 0, 0,
					    0, 0, 0);

	    // Create a new Behavior object that will rotate the scene
	    // add it into the scene graph.
	    Transform3D yAxis = new Transform3D();
	    RotationInterpolator rotator =
	      new RotationInterpolator(rotationAlpha, sceneTG, yAxis,
				       0.0f, (float) Math.PI*2.0f);
	    rotator.setSchedulingBounds(bounds);
	    //sceneTG.addChild(rotator);
	    
	    MouseRotate myMR = new MouseRotate();
	    myMR.setTransformGroup(sceneTG);
	    myMR.setSchedulingBounds(new BoundingSphere());
	    sceneRoot.addChild(myMR);

	    // Load the object
	    TransformGroup objTG = new TransformGroup();
	    Transform3D objTrans = new Transform3D();
	    objTG.getTransform(objTrans);
	    objTrans.setScale( 0.6 );
	    objTG.setTransform(objTrans);
	    sceneTG.addChild(objTG);
	    objTG.addChild(createHouse("model/bottom.obj"));

	    return sceneRoot;
	  }
	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -