📄 example6_16.java
字号:
/* 导入3D文件 */
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 com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.io.*;
import com.sun.j3d.utils.behaviors.vp.*;
class Load3d
{
private boolean spin = false;
private boolean noTriangulate = false;
private boolean noStripify = false;
private double creaseAngle = 60.0;
private File filename = null;
private SimpleUniverse u;
private BoundingSphere bounds;
private String str_fileName;
public BranchGroup createSceneGraph() {
// 建立构建场景图的根目录对象
BranchGroup objRoot = new BranchGroup();
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.7);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objScale.addChild(objTrans);
int flags = ObjectFile.RESIZE;
//if (!noTriangulate) flags |= ObjectFile.TRIANGULATE;
//if (!noStripify) flags |= ObjectFile.STRIPIFY;
ObjectFile f = new ObjectFile(flags,(float)(creaseAngle * Math.PI / 180.0));
Scene s = null;
try { s=f.load(str_fileName);}
catch (FileNotFoundException e) {System.exit(1);}
catch (ParsingErrorException e) {System.exit(1);}
catch (IncorrectFormatException e) { System.exit(1); }
objTrans.addChild(s.getSceneGroup());
//设置场景的范围
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
//使物体旋转
if (spin)
{
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,
objTrans, yAxis, 0.0f, (float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
}
// 设置背景
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
Background bgNode = new Background(bgColor);
bgNode.setApplicationBounds(bounds);
objRoot.addChild(bgNode);
return objRoot;
}
public Load3d()
{
if (filename == null)
{
try {
filename = new File("galleon.obj");
str_fileName=filename.getName();
} catch (Exception e) { System.exit(1); }
}
BranchGroup scene = createSceneGraph();
u = new SimpleUniverse();
//在平台上添加鼠标行为
ViewingPlatform viewingPlatform = u.getViewingPlatform();
//建立场景面板对象
PlatformGeometry pg = new PlatformGeometry();
// 设置环境光源
Color3f ambientColor = new Color3f(0.9f, 0.9f, 0.9f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
pg.addChild(ambientLightNode);
// 设置光源方向
Color3f light1Color = new Color3f(2.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
Color3f light2Color = new Color3f(2.0f, 1.0f, 1.0f);
Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
pg.addChild(light1);
DirectionalLight light2
= new DirectionalLight(light2Color, light2Direction);
light2.setInfluencingBounds(bounds);
pg.addChild(light2);
viewingPlatform.setPlatformGeometry( pg );
viewingPlatform.setNominalViewingTransform();
//设置鼠标控制
if (!spin) {
OrbitBehavior orbit = new OrbitBehavior();
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
orbit.setSchedulingBounds(bounds);
viewingPlatform.setViewPlatformBehavior(orbit);
}
u.addBranchGraph(scene);
}
}
public class Example6_16
{
public static void main( String[] args )
{ new Load3d(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -