vrml java_Java3D实例应用-载入VRML模型

这个Java程序演示了如何使用Java3D API加载并显示一个VRML模型文件。通过创建BranchGroup,设置光照、背景颜色,以及添加MouseRotate、MouseTranslate和MouseWheelZoom行为,用户可以交互式地查看模型。
摘要由CSDN通过智能技术生成

packagecom.java3d.dennist.loader;importjava.applet.Applet;importjava.awt.BorderLayout;importjavax.media.j3d.Background;importjavax.media.j3d.BoundingSphere;importjavax.media.j3d.BranchGroup;importjavax.media.j3d.Canvas3D;importjavax.media.j3d.DirectionalLight;importjavax.media.j3d.Transform3D;importjavax.media.j3d.TransformGroup;importjavax.vecmath.Color3f;importjavax.vecmath.Point3d;importjavax.vecmath.Vector3d;importjavax.vecmath.Vector3f;importcom.sun.j3d.utils.applet.MainFrame;importcom.sun.j3d.utils.behaviors.mouse.MouseRotate;importcom.sun.j3d.utils.behaviors.mouse.MouseTranslate;importcom.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;importcom.sun.j3d.utils.universe.SimpleUniverse;importcom.sun.j3d.utils.universe.ViewingPlatform;/***

*

*@version: 1.1

*

*@author: 苏若年 发送邮件

*

*@since: 1.0 创建时间: 2013-5-8 下午03:54:07

*

* TODO : 加载VML模型文件到场景

**/

public class JavaModelWrlLoaderApp extendsApplet{/****/

private static final long serialVersionUID = 5841679659336190804L;publicBranchGroup createSceneGraph(){//创建场景图分支

BranchGroup group = newBranchGroup();//几何变换组节点

TransformGroup transGroup = newTransformGroup();//几何变换

Transform3D trans3d = newTransform3D();//缩放变换

trans3d.setScale(0.3);//将几何变换节点对象添加到节点组

transGroup.setTransform(trans3d);//将几何变化组添加到场景

group.addChild(transGroup);//球体作用范围边界对象

BoundingSphere bound= new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

Color3f bgColor= new Color3f(0.05f,0.05f,0.2f);

Background bg= newBackground(bgColor);

bg.setApplicationBounds(bound);

group.addChild(bg);//设置光源

Color3f lightColor = new Color3f(1.0f,1.0f,0.9f);

Vector3f lightDirection= new Vector3f(4.0f,-7.0f,-12.0f);//设置定向光的颜色和影响范围

DirectionalLight light = newDirectionalLight(lightColor, lightDirection);

light.setInfluencingBounds(bound);//将光源添加到场景

group.addChild(light);//几何变换组节点 - 加载外部模型

TransformGroup objTrans = newTransformGroup();

objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);//加载Obj格式的模型文件

objTrans.addChild(new WrlFileReader("F:/MANY.WRL"));//将模型添加到变换组节点

transGroup.addChild(objTrans);//设置几何变化,绕Y轴中心旋转//添加通过鼠标左键控制3D物体旋转的对象

MouseRotate behavior = newMouseRotate();

behavior.setTransformGroup(objTrans);

objTrans.addChild(behavior);

behavior.setSchedulingBounds(bound);//添加鼠标右键的拖拉运动控制3D物体(X,Y)平移

MouseTranslate tr = newMouseTranslate();

tr.setTransformGroup(objTrans);

objTrans.addChild(tr);

tr.setSchedulingBounds(bound);//添加鼠标滚轮控制3D物体沿Z轴

MouseWheelZoom tr1 = newMouseWheelZoom();

tr1.setTransformGroup(objTrans);

objTrans.addChild(tr1);

tr1.setSchedulingBounds(bound);

group.compile();returngroup;

}//初始化用户视角

private voidinitUserPosition(SimpleUniverse universe,Point3d userPosition){

ViewingPlatform vp=universe.getViewingPlatform();

TransformGroup steerTG=vp.getViewPlatformTransform();

Transform3D t3d= newTransform3D();

steerTG.getTransform(t3d);

t3d.lookAt(userPosition,new Point3d(0,0,0), new Vector3d(0,1,0));

t3d.invert();

steerTG.setTransform(t3d);

}publicJavaModelWrlLoaderApp(){

setLayout(newBorderLayout());//创建3D场景绘制画布Canvas3D对象

Canvas3D canvas = new Canvas3D(null);

add("Center",canvas);

BranchGroup scene=createSceneGraph();

SimpleUniverse universe= newSimpleUniverse(canvas);//设定用户视角

Point3d userPosition = new Point3d(0,-3,60);

initUserPosition(universe,userPosition);//universe.getViewingPlatform().setNominalViewingTransform();

universe.addBranchGraph(scene);

}public static voidmain(String[] args) {new MainFrame(new JavaModelWrlLoaderApp(), 360,360);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值