java read 3ds files,用Java3D 3DS FileLoader读取.3DS文件的方法

importcom.microcrowd.loader.java3d.max3ds.Loader3DS;

importcom.sun.j3d.loaders.Scene;

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;

importjava.awt.Color;

importjava.io.FileNotFoundException;

importjava.util.logging.Level;

importjava.util.logging.Logger;

importjavax.media.j3d.Alpha;

importjavax.media.j3d.BoundingSphere;

importjavax.media.j3d.BranchGroup;

importjavax.media.j3d.DirectionalLight;

importjavax.media.j3d.Transform3D;

importjavax.media.j3d.TransformGroup;

importjavax.vecmath.Color3f;

importjavax.vecmath.Point3d;

importjavax.vecmath.Vector3f;

/**

*

* @author hadeslee

*/

publicclassSimple3DSLd {

publicSimple3DSLd() {

build();

}

privatevoidbuild() {

try{

BranchGroup bg = loadBranchGroup();

SimpleUniverse u =newSimpleUniverse();

u.getViewingPlatform().setNominalViewingTransform();

u.addBranchGraph(bg);

}catch(FileNotFoundException ex) {

Logger.getLogger(Simple3DSLd.class.getName()).log(Level.SEVERE,null, ex);

}

}

privateBranchGroup loadBranchGroup()throwsFileNotFoundException {

Loader3DS l3ds =newLoader3DS();

Scene scene = l3ds.load("K:/sample.3ds");

System.out.println("scene="+ scene);

if(scene !=null) {

BranchGroup objRoot =newBranchGroup();

BranchGroup bg = scene.getSceneGroup();

TransformGroup objTrans =newTransformGroup();

Transform3D trans =newTransform3D();

trans.setScale(0.3f);

objTrans.setTransform(trans);

objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

objTrans.addChild(bg);

objRoot.addChild(objTrans);

BoundingSphere bounds =newBoundingSphere(newPoint3d(0.0,0.0,

0.0),100.0);

MouseRotate behavior =newMouseRotate();

behavior.setTransformGroup(objTrans);

objTrans.addChild(behavior);

behavior.setSchedulingBounds(bounds);

MouseTranslate tr =newMouseTranslate();

tr.setTransformGroup(objTrans);

objTrans.addChild(tr);

tr.setSchedulingBounds(bounds);

MouseWheelZoom tr1 =newMouseWheelZoom();

tr1.setTransformGroup(objTrans);

objTrans.addChild(tr1);

tr1.setSchedulingBounds(bounds);

Color3f light1Color =newColor3f(Color.RED);

Vector3f light1Direction =newVector3f(0f, 0f, -10f);

DirectionalLight light1 =newDirectionalLight(light1Color,

light1Direction);

light1.setInfluencingBounds(bounds);

objRoot.addChild(light1);

returnobjRoot;

}

returnnull;

}

publicstaticvoidmain(String[] args) {

newSimple3DSLd();

}

}

import com.microcrowd.loader.java3d.max3ds.Loader3DS;import com.sun.j3d.loaders.Scene;import com.sun.j3d.utils.behaviors.mouse.MouseRotate;import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;import com.sun.j3d.utils.universe.SimpleUniverse;import java.awt.Color;import java.io.FileNotFoundException;import java.util.logging.Level;import java.util.logging.Logger;import javax.media.j3d.Alpha;import javax.media.j3d.BoundingSphere;import javax.media.j3d.BranchGroup;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.Vector3f;/*** * @author hadeslee*/public class Simple3DSLd { public Simple3DSLd() { build(); } private void build() { try { BranchGroup bg = loadBranchGroup(); SimpleUniverse u = new SimpleUniverse(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(bg); } catch (FileNotFoundException ex) { Logger.getLogger(Simple3DSLd.class.getName()).log(Level.SEVERE, null, ex); } } private BranchGroup loadBranchGroup() throws FileNotFoundException { Loader3DS l3ds = new Loader3DS(); Scene scene = l3ds.load("K:/sample.3ds"); System.out.println("scene=" + scene); if (scene != null) { BranchGroup objRoot = new BranchGroup(); BranchGroup bg = scene.getSceneGroup(); TransformGroup objTrans = new TransformGroup(); Transform3D trans = new Transform3D(); trans.setScale(0.3f); objTrans.setTransform(trans); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.addChild(bg); objRoot.addChild(objTrans); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); MouseRotate behavior = new MouseRotate(); behavior.setTransformGroup(objTrans); objTrans.addChild(behavior); behavior.setSchedulingBounds(bounds); MouseTranslate tr = new MouseTranslate(); tr.setTransformGroup(objTrans); objTrans.addChild(tr); tr.setSchedulingBounds(bounds); MouseWheelZoom tr1 = new MouseWheelZoom(); tr1.setTransformGroup(objTrans); objTrans.addChild(tr1); tr1.setSchedulingBounds(bounds); Color3f light1Color = new Color3f(Color.RED); Vector3f light1Direction = new Vector3f(0f, 0f, -10f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } return null; } public static void main(String[] args) { new Simple3DSLd(); }}

或者Jframe中

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.Frame;

importjava.awt.GraphicsConfiguration;

importjava.awt.GraphicsDevice;

importjava.awt.GraphicsEnvironment;

importjava.awt.Rectangle;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

importjavax.media.j3d.BoundingSphere;

importjavax.media.j3d.BranchGroup;

importjavax.media.j3d.Canvas3D;

importjavax.media.j3d.DirectionalLight;

importjavax.media.j3d.GraphicsConfigTemplate3D;

importjavax.media.j3d.Locale;

importjavax.media.j3d.PhysicalBody;

importjavax.media.j3d.PhysicalEnvironment;

importjavax.media.j3d.Transform3D;

importjavax.media.j3d.TransformGroup;

importjavax.media.j3d.View;

importjavax.media.j3d.ViewPlatform;

importjavax.media.j3d.VirtualUniverse;

importjavax.vecmath.Color3f;

importjavax.vecmath.Point3d;

importjavax.vecmath.Vector3d;

importjavax.vecmath.Vector3f;

importcom.microcrowd.loader.java3d.max3ds.Loader3DS;

importcom.sun.j3d.loaders.Scene;

importcom.sun.j3d.utils.behaviors.mouse.MouseRotate;

importcom.sun.j3d.utils.behaviors.mouse.MouseTranslate;

importcom.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;

// import com.sun.j3d.utils.universe.SimpleUniverse;

/**

* @author

*

*/

publicclassAWTFrameDSM {

privatestaticfinallongserialVersionUID = 1L;

/**

* 测试方法

*/

publicstaticvoidmain(String[] args) {

AWTFrameDSM appwin =newAWTFrameDSM();

appwin.initialize();

}

/**

* This is the default constructor

*/

publicAWTFrameDSM() {

}

/**

* 初始化Frame主窗口

*

* @return void

*/

privatevoidinitialize() {

// 实例化GraphicsConfiguration的方法1

GraphicsConfigTemplate3D template =newGraphicsConfigTemplate3D();

GraphicsEnvironment env = GraphicsEnvironment

.getLocalGraphicsEnvironment();

GraphicsDevice device = env.getDefaultScreenDevice();

GraphicsConfiguration config = device.getBestConfiguration(template);

// 实例化GraphicsConfiguration的方法2

// GraphicsConfiguration config =

// SimpleUniverse.getPreferredConfiguration();

// 新建Frame窗口对象

Frame dsframe =newFrame(config);

// 若Frame对象的布局方式为绝对布局null,就无法显示3D虚拟场景

dsframe.setLayout(newBorderLayout());

dsframe.setTitle("3DS模型显示区");

dsframe.setBounds(newRectangle(0,0,433,458));

// 添加窗口监听器实现关闭窗口(Frame),关闭窗口时退出程序

dsframe.addWindowListener(newWindowAdapter() {

publicvoidwindowClosing(WindowEvent ev) {

System.out.print("退出程序!");

System.exit(0);

}

});

// 新建3D场景绘制画布Canvas3D对象

Canvas3D cv =newCanvas3D(config);

// add the canvas to this frame. Since this is the only thing added to

// the main frame we don't care about layout managers etc.

dsframe.add(cv, BorderLayout.CENTER);

// 使Frame对象可视化

dsframe.setVisible(true);

// 在Canvas3D画布上绘制3D场景

creat3DObject(cv);

}

/**

* 创建3D虚拟场景

*/

privatevoidcreat3DObject(Canvas3D cv) {

// 创建超结构(create superstructure)

VirtualUniverse vu =newVirtualUniverse();

Locale loc =newLocale(vu);

// 创建视图分支(create view branch)

BranchGroup bgView = createViewBranch(cv);

bgView.compile();

loc.addBranchGraph(bgView);

BranchGroup bg = loadBranchGroup();

bg.compile();

loc.addBranchGraph(bg);

}

/**

* 生成BranchGroup的私有方法,创建视图分支

*

* @param cv

*            Canvas3D画布对象

*/

privateBranchGroup createViewBranch(Canvas3D cv) {

// 创建View组件对象

View view =newView();

// 设置投影方式

view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION);

// 创建ViewPlatform叶节点

ViewPlatform vp =newViewPlatform();

view.addCanvas3D(cv);

view.attachViewPlatform(vp);

view.setPhysicalBody(newPhysicalBody());

// 设置View对象属性

view.setPhysicalEnvironment(newPhysicalEnvironment());

// 几何变换

Transform3D trans =newTransform3D();

// 观察者眼睛的位置

Point3d eye =newPoint3d(0,0,1/ Math.tan(Math.PI /8));

// 观察者方向指向的点

Point3d center =newPoint3d(0,0,0);

// 垂直于观察者方向向上的方向

Vector3d vup =newVector3d(0,1,0);

// 生成几何变换矩阵

trans.lookAt(eye, center, vup);

// 求矩阵的逆

trans.invert();

// 几何变换组点

TransformGroup tg =newTransformGroup(trans);

tg.addChild(vp);

// 创建视图分支

BranchGroup bgView =newBranchGroup();

bgView.addChild(tg);

returnbgView;

}

/**

* 加载3DS文件的场景内容分支

*/

privateBranchGroup loadBranchGroup() {

Loader3DS l3ds =newLoader3DS();

// The Scene interface is a set of methods used to extract Java 3D scene

// graph information from a file loader utility.

// The interface is used to give loaders of various file formats a

// common public interface.

Scene scene =null;

try{

scene = l3ds.load("K:/opengheadok.3DS");

System.out.println("文件读取成功");

}catch(Exception fileio) {

System.out.println("文件读取失败,请检查文件是否存在");

}

System.out.println("scene="+ scene);

if(scene !=null) {

BranchGroup bg = scene.getSceneGroup();

// 创建场景图分支

BranchGroup objRoot =newBranchGroup();

// 几何变换

Transform3D trans =newTransform3D();

trans.setScale(0.3f);// 缩放变换

// 几何变换组节点

TransformGroup objTrans =newTransformGroup();

objTrans.setTransform(trans);

objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

objTrans.addChild(bg);

objRoot.addChild(objTrans);

// 球体作用范围边界对象

BoundingSphere bounds =newBoundingSphere(newPoint3d(0.0,0.0,

0.0),100.0);

// 添加通过鼠标左键控制3D物体旋转的对象

MouseRotate behavior =newMouseRotate();

behavior.setTransformGroup(objTrans);

objTrans.addChild(behavior);

behavior.setSchedulingBounds(bounds);

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

MouseTranslate tr =newMouseTranslate();

tr.setTransformGroup(objTrans);

objTrans.addChild(tr);

tr.setSchedulingBounds(bounds);

// 添加鼠标滚轮控制3D物体沿Z轴

MouseWheelZoom tr1 =newMouseWheelZoom();

tr1.setTransformGroup(objTrans);

objTrans.addChild(tr1);

tr1.setSchedulingBounds(bounds);

// 设置光源

Color3f light1Color =newColor3f(Color.gray);

Vector3f light1Direction =newVector3f(0f, 0f, -10f);

DirectionalLight light1 =newDirectionalLight(light1Color,

light1Direction);

light1.setInfluencingBounds(bounds);

objRoot.addChild(light1);

returnobjRoot;

}

returnnull;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值