package useful;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.io.FileNotFoundException;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
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.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.Viewer;
import com.sun.j3d.utils.universe.ViewingPlatform;
public class ObjLoader extends Applet {
private static final long serialVersionUID = 1L;
public BranchGroup createSceneGraph(String filename) {
BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);// 设置涉及范围
{
Color3f bgColor = new Color3f(0.2f, 0.3f, 0.8f);// 背景颜色
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
}
{
// 设置变换组
TransformGroup objTrans1 = new TransformGroup();
Transform3D x = new Transform3D();
Transform3D y = new Transform3D();
TransformGroup xRot = new TransformGroup();
xRot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
xRot.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
x.rotX(Math.PI / 180); // x轴旋转
xRot.setTransform(x);
objRoot.addChild(xRot);
y.rotY(Math.PI / 180);
objTrans1.setTransform(y); // Y轴旋转
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
xRot.addChild(objTrans1);
// 定义鼠标行为
// MouseRotate behavior = new MouseRotate();
// behavior.setTransformGroup(objTrans1);
// objRoot.addChild(behavior);
// behavior.setSchedulingBounds(bounds);
// MouseZoom behavior2 = new MouseZoom();
// behavior2.setTransformGroup(objTrans1);
// objRoot.addChild(behavior2);
// behavior2.setSchedulingBounds(bounds);
// MouseTranslate behavior3 = new MouseTranslate();
// behavior3.setTransformGroup(objTrans1);
// objRoot.addChild(behavior3);
// behavior3.setSchedulingBounds(bounds);
}
{
// 整体光设置
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);
// 渐变光
AmbientLight ambientLight = new AmbientLight(ambientColor);
ambientLight.setInfluencingBounds(bounds);
objRoot.addChild(ambientLight);
// 定义方向光
DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
DirectionalLight light2 = new DirectionalLight(light2Color,
light2Direction);
light2.setInfluencingBounds(bounds);
objRoot.addChild(light2);
TransformGroup objTrans2 = new TransformGroup();
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans2);
// Create an Alpha object that goes from 0 to 1 in 8 seconds,
// repeatedly.
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
40000, 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, objTrans2, yAxis,0.0f,
// (float) Math.PI*2.0f);
// rotator.setSchedulingBounds(bounds);
// objTrans2.addChild(rotator);
// 准备导入
Transform3D objTrans3d = new Transform3D();
objTrans2.getTransform(objTrans3d);
objTrans2.setTransform(objTrans3d);
// objTrans2.addChild(objTrans2);
int flags = ObjectFile.RESIZE;
ObjectFile f = new ObjectFile(flags,
(float) (49.0 * Math.PI / 30.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);
}
// sceneTG.addChild(f);
MouseRotate behavior = new MouseRotate();
behavior.setTransformGroup(objTrans2);
objRoot.addChild(behavior);
behavior.setSchedulingBounds(bounds);
objTrans2.addChild(s.getSceneGroup());
return objRoot;
}
}
public ObjLoader(String filename) {
filename = "d:/111111.obj"; // 定义viewplatform
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
Viewer viewer = new Viewer(c);
Vector3d viewpoint = new Vector3d(.0, .0, 3.0);
Transform3D t = new Transform3D();
t.set(viewpoint);
ViewingPlatform v = new ViewingPlatform();
v.getViewPlatformTransform().setTransform(t);
BranchGroup scene = createSceneGraph(filename);
SimpleUniverse u = new SimpleUniverse(v, viewer);
u.getViewingPlatform();
u.addBranchGraph(scene);
}
public static void main(String[] args) {
new MainFrame(new ObjLoader(null), 800, 600);
}
}