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