转动物体

来源(null)

下载源代码: i1812_turnobj.rar

TurnOBJ.java
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;

/**
 * <p>Title: 转动物体</p>
 * <p>Description: 生成两个转动物体</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 中国农业大学计算机系</p>
 * @author 彭波
 * @version 1.0
 */

public class TurnOBJ extends Applet {
	boolean isStandalone = false;
	public BranchGroup createSceneGraph() {
		BranchGroup objRoot = new BranchGroup();
		TransformGroup objScale = new TransformGroup();
		Transform3D t3d = new Transform3D();
		t3d.setScale(0.3);
		objScale.setTransform(t3d);
		objRoot.addChild(objScale);
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
		Color3f bgColor = new Color3f(1.05f, 0.00f, 0.0f);
		Background bg = new Background(bgColor);
		bg.setApplicationBounds(bounds);
		objRoot.addChild(bg);
		Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
		Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
		DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
		light1.setInfluencingBounds(bounds);
		objRoot.addChild(light1);
		BranchGroup b1 = new objFile("galleon.obj");
		BranchGroup b2 = new objFile("beethoven.obj");
		objScale.addChild(createObject(b1, -1.2f, 0.0f));
		objScale.addChild(createObject(b2, 1.2f, 0.0f));
		objRoot.compile();
		return objRoot;
	}

	private Group createObject(BranchGroup b, float xpos, float ypos) {
		Transform3D t = new Transform3D();
		t.setTranslation ( new Vector3f(xpos, ypos, 0.0f) );
		TransformGroup objTrans = new TransformGroup(t);
		TransformGroup spin = new TransformGroup();
		spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		spin.addChild(b);
		Transform3D yAxis = new Transform3D();
		Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
		RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, spin, yAxis, 0.0f, (float) Math.PI*2.0f);
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
		rotator.setSchedulingBounds(bounds);
		objTrans.addChild(rotator);
		objTrans.addChild( spin );
		return objTrans;
	}

	public TurnOBJ() {
		setLayout(new BorderLayout());
		GraphicsConfiguration config =
					SimpleUniverse.getPreferredConfiguration();
		Canvas3D c = new Canvas3D(config);
		add("Center", c);
		BranchGroup scene = createSceneGraph();
		SimpleUniverse u = new SimpleUniverse(c);
		u.getViewingPlatform().setNominalViewingTransform();
		u.addBranchGraph(scene);
	}

	//Main method
	public static void main(String[] args) {
		TurnOBJ applet = new TurnOBJ();
		applet.isStandalone = true;
		Frame frame;
		frame = new Frame() {
			protected void processWindowEvent(WindowEvent e) {
				super.processWindowEvent(e);
				if (e.getID() == WindowEvent.WINDOW_CLOSING) {
					System.exit(0);
				}
			}
			public synchronized void setTitle(String title) {
				super.setTitle(title);
				enableEvents(AWTEvent.WINDOW_EVENT_MASK);
			}
		};
		frame.setTitle("显示两个转动物体");
		frame.add(applet, BorderLayout.CENTER);
		applet.init();
		applet.start();
		frame.setSize(400, 320);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
		frame.setVisible(true);
	}
}



objFile.java
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import java.io.*;
import javax.media.j3d.*;

/**
 * <p>Title: 转动物体</p>
 * <p>Description: 生成两个转动物体</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 中国农业大学计算机系</p>
 * @author 彭波
 * @version 1.0
 */

public class objFile extends BranchGroup {
	private double creaseAngle = 60.0;
	public objFile(String filename) {
		BranchGroup obj = new BranchGroup();
		int flags = ObjectFile.RESIZE;
		ObjectFile f = new ObjectFile(flags, (float)(creaseAngle * Math.PI / 180.0));
		Scene s = null;
		try {
			s = f.load(filename);
		}
		catch (FileNotFoundException e) {
			System.err.println(e);
			System.exit(1);
		}
		catch (ParsingErrorException e) {
			System.err.println(e);
			System.exit(1);
		}
		catch (IncorrectFormatException e) {
			System.err.println(e);
			System.exit(1);
		}
		obj.addChild(s.getSceneGroup());
		this.addChild(obj);
	}
}


执行 Turn3D.bat
@echo on
start .
java TurnOBJ

Link: http://www.asm32.net/article_details.aspx?id=1812


浏览次数 615 发布时间 2006/8/18 22:14:45 从属分类 Java 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1