Java3D实例应用-载入3ds 模型

3ds3dsmax建模软件的衍生文件格式,做完max场景文件后可导出生3ds格式,可与其它建模软件兼容,也可用于渲染.

 

相关文档

Java3D 创建三维几何模型:[ http://www.cnblogs.com/dennisit/archive/2013/05/06/3063042.html ]

Java3D 加载外部Obj模型:[ http://www.cnblogs.com/dennisit/archive/2013/05/07/3065126.html ]

 

3ds模型加载时用到第三方jar包 Java3dloader

Java3dloader包下载地址:  http://sourceforge.net/projects/java3dsloader/ 

 


实例一: 加载3ds文件到场景

效果图如下:

读取3ds模型加载到场景

package com.java3d.dennist.loader;

import javax.media.j3d.BranchGroup;

import com.microcrowd.loader.java3d.max3ds.Loader3DS;
import com.sun.j3d.loaders.Scene;

/**
 *
 *
 *  @version : 1.1
 *  
 *  @author  : 苏若年         <a href="mailto:DennisIT@163.com">发送邮件</a>
 *    
 *  @since   : 1.0      创建时间:    2013-5-7 下午02:44:02
 *     
 *  TODO     : 
 *
 */
public class ThreeDSFileReader  extends BranchGroup{
        
    /**
     * 
     * 读取ObjModel文件
     * 
     * @param filePath    3ds文件路径
     * 
     */
    public ThreeDSFileReader(String filePath){
        BranchGroup branchGroup = new BranchGroup();
        Loader3DS l3ds = new Loader3DS();  
        Scene scene = null;
        try {
            scene = l3ds.load(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("3DS模型加载失败" + e.getMessage());
        }
        branchGroup.addChild(scene.getSceneGroup());
        this.addChild(branchGroup);
        
    }

}

加载模型到场景

package com.java3d.dennist.loader;

import java.applet.Applet;
import java.awt.BorderLayout;

import javax.media.j3d.Alpha;
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.RotationInterpolator;
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.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;

/**
 *
 *  com.java3d.dennist.loaderJavaModel3dsLoaderApp.java   
 *
 *  @version : 1.1
 *  
 *  @author  : 苏若年         <a href="mailto:DennisIT@163.com">发送邮件</a>
 *    
 *  @since   : 1.0      创建时间:    2013-5-7 下午02:57:32
 *     
 *  TODO     : 
 *
 */
public class JavaModel3dsLoaderApp extends Applet{

    public BranchGroup createSceneGraph(){
        // 创建场景图分支
        BranchGroup group = new BranchGroup();
        
        // 几何变换组节点
        TransformGroup transGroup = new TransformGroup();
        // 几何变换
        Transform3D trans3d = new Transform3D();
        // 缩放变换
        trans3d.setScale(0.7);
        //将几何变换节点对象添加到节点组
        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 = new Background(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 = new DirectionalLight(lightColor, lightDirection);
        light.setInfluencingBounds(bound);
        //将光源添加到场景
        group.addChild(light);
        
        //几何变换组节点 - 加载外部模型
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        //加载Obj格式的模型文件
        objTrans.addChild(new ThreeDSFileReader("F:/rexue.3ds"));
        //将模型添加到变换组节点
        transGroup.addChild(objTrans);
        
        //设置几何变化,绕Y轴中心旋转
        Transform3D yAxis = new Transform3D();
        Alpha rotationAlpha = new Alpha(-1,Alpha.INCREASING_ENABLE,
                0,0,
                6000,0,0,
                0,0,0);
        RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans,yAxis,0.0f,(float)Math.PI*2.0f);
        rotator.setSchedulingBounds(bound);
        objTrans.addChild(rotator);
        
        group.compile();
        
        return group;
    }
    
    
    public  JavaModel3dsLoaderApp(){
        
        setLayout(new BorderLayout());
        // 创建3D场景绘制画布Canvas3D对象
        Canvas3D canvas = new Canvas3D(null);
        add("Center",canvas);
        BranchGroup scene = createSceneGraph();
        
        SimpleUniverse universe = new SimpleUniverse(canvas);
        //设定用户视角
        Point3d userPosition = new Point3d(0,18,18);
        initUserPosition(universe,userPosition);
        //universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(scene);
    }
    
    
    //初始化用户视角
    private void initUserPosition(SimpleUniverse universe,Point3d userPosition){
        ViewingPlatform vp = universe.getViewingPlatform();
        TransformGroup steerTG = vp.getViewPlatformTransform();
        Transform3D t3d = new Transform3D();
        steerTG.getTransform(t3d);
        t3d.lookAt(userPosition, new Point3d(0,0,0), new Vector3d(0,1,0));
        t3d.invert();
        steerTG.setTransform(t3d);
    }  
    
    
    public static void main(String[] args) {
        new MainFrame(new JavaModel3dsLoaderApp(), 340,340); 
    }
}

上面实例实现的效果是,模型绕Y轴旋转,下面实例中我们实现鼠标触控模型三维空间任意旋转变换.

实例二 : 实现鼠标触控模型三维空间任意旋转

效果图:

实例代码:

package com.java3d.dennist.loader;

import java.applet.Applet;
import java.awt.BorderLayout;

import javax.media.j3d.Alpha;
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.RotationInterpolator;
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.utils.applet.MainFrame;
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 com.sun.j3d.utils.universe.ViewingPlatform;

/**
 *
 *  com.java3d.dennist.loaderJavaModel3dsLoaderApp.java   
 *
 *  @version : 1.1
 *  
 *  @author  : 苏若年         <a href="mailto:DennisIT@163.com">发送邮件</a>
 *    
 *  @since   : 1.0      创建时间:    2013-5-7 下午02:57:32
 *     
 *  TODO     : 
 *
 */
public class JavaModel3dsLoaderApp extends Applet{

    public BranchGroup createSceneGraph(){
        // 创建场景图分支
        BranchGroup group = new BranchGroup();
        
        // 几何变换组节点
        TransformGroup transGroup = new TransformGroup();
        // 几何变换
        Transform3D trans3d = new Transform3D();
        // 缩放变换
        trans3d.setScale(0.9);
        //将几何变换节点对象添加到节点组
        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 = new Background(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 = new DirectionalLight(lightColor, lightDirection);
        light.setInfluencingBounds(bound);
        //将光源添加到场景
        group.addChild(light);
        
        //几何变换组节点 - 加载外部模型
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        //加载Obj格式的模型文件
        objTrans.addChild(new ThreeDSFileReader("F:/rexue.3ds"));
        //将模型添加到变换组节点
        transGroup.addChild(objTrans);
        
        // 添加通过鼠标左键控制3D物体旋转的对象
        MouseRotate behavior = new MouseRotate();
        behavior.setTransformGroup(objTrans);
        objTrans.addChild(behavior);
        behavior.setSchedulingBounds(bound);
        // 添加鼠标右键的拖拉运动控制3D物体(X,Y)平移
        MouseTranslate tr = new MouseTranslate();
        tr.setTransformGroup(objTrans);
        objTrans.addChild(tr);
        tr.setSchedulingBounds(bound);
        // 添加鼠标滚轮控制3D物体沿Z轴
        MouseWheelZoom tr1 = new MouseWheelZoom();
        tr1.setTransformGroup(objTrans);
        objTrans.addChild(tr1);
        tr1.setSchedulingBounds(bound);
        
        group.compile();
        
        return group;
    }
    
    
    public  JavaModel3dsLoaderApp(){
        
        setLayout(new BorderLayout());
        // 创建3D场景绘制画布Canvas3D对象
        Canvas3D canvas = new Canvas3D(null);
        add("Center",canvas);
        BranchGroup scene = createSceneGraph();
        
        SimpleUniverse universe = new SimpleUniverse(canvas);
        //设定用户视角
        Point3d userPosition = new Point3d(0,10,18);
        initUserPosition(universe,userPosition);
        //universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(scene);
    }
    
    
    //初始化用户视角
    private void initUserPosition(SimpleUniverse universe,Point3d userPosition){
        ViewingPlatform vp = universe.getViewingPlatform();
        TransformGroup steerTG = vp.getViewPlatformTransform();
        Transform3D t3d = new Transform3D();
        steerTG.getTransform(t3d);
        t3d.lookAt(userPosition, new Point3d(0,0,0), new Vector3d(0,1,0));
        t3d.invert();
        steerTG.setTransform(t3d);
    }  
    
    
    public static void main(String[] args) {
        new MainFrame(new JavaModel3dsLoaderApp(), 340,340); 
    }
}

 

附录:

附录一不依赖Applet实现Java3D加载外部模型

View Code
package com.java3d.dennist.study;

import com.java3d.dennist.loader.ThreeDSFileReader;
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 javax.media.j3d.Background;
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;  

/**
 *
 *

 *  @version : 1.1
 *  
 *  @author  : 苏若年         <a href="mailto:DennisIT@163.com">发送邮件</a>
 *    
 *  @since   : 1.0      创建时间:    2013-5-6 下午04:54:04
 *     
 *  TODO     : Java3D技术 加载3ds模型
 *
 */
public class Java3dsLoader {

    public Java3dsLoader(){
        build();
    }
    
    private void build() {  
        try {  
            BranchGroup bg = createSceneGraph();  
            SimpleUniverse u = new SimpleUniverse();  
            u.getViewingPlatform().setNominalViewingTransform();  
            u.addBranchGraph(bg);  
        } catch (Exception e) {  
            e.printStackTrace();
        } 
    }  
    

    public BranchGroup createSceneGraph(){
        // 创建场景图分支
        BranchGroup group = new BranchGroup();
        
        // 几何变换组节点
        TransformGroup transGroup = new TransformGroup();
        // 几何变换
        Transform3D trans3d = new Transform3D();
        // 缩放变换
        trans3d.setScale(0.7);
        //将几何变换节点对象添加到节点组
        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 = new Background(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 = new DirectionalLight(lightColor, lightDirection);
        light.setInfluencingBounds(bound);
        //将光源添加到场景
        group.addChild(light);
        
        //几何变换组节点 - 加载外部模型
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        //加载Obj格式的模型文件
        objTrans.addChild(new ThreeDSFileReader("F:/rexue.3ds"));
        //将模型添加到变换组节点
        transGroup.addChild(objTrans);
        
        // 添加通过鼠标左键控制3D物体旋转的对象
        MouseRotate behavior = new MouseRotate();
        behavior.setTransformGroup(objTrans);
        objTrans.addChild(behavior);
        behavior.setSchedulingBounds(bound);
        // 添加鼠标右键的拖拉运动控制3D物体(X,Y)平移
        MouseTranslate tr = new MouseTranslate();
        tr.setTransformGroup(objTrans);
        objTrans.addChild(tr);
        tr.setSchedulingBounds(bound);
        // 添加鼠标滚轮控制3D物体沿Z轴
        MouseWheelZoom tr1 = new MouseWheelZoom();
        tr1.setTransformGroup(objTrans);
        objTrans.addChild(tr1);
        tr1.setSchedulingBounds(bound);
        
        group.compile();
        
        return group;
    }
    
    
    public static void main(String[] args) {
        new Java3dsLoader();
    }

}

附录二基于swing实现Java3D加载外部模型

View Code
package com.java3d.dennist.study;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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.GraphicsConfigTemplate3D;
import javax.media.j3d.Locale;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.java3d.dennist.loader.ThreeDSFileReader;
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;

/**
 * 
 * @version : 1.1
 * 
 * @author  : 苏若年 <a href="mailto:DennisIT@163.com">发送邮件</a>
 * 
 * @since   : 1.0 创建时间: 2013-5-6 下午05:09:47
 * 
 *  TODO    : 基于Swing实现Java3D 模型加载
 * 
 */
public class JFrame3dsLoader {

    private static final long serialVersionUID = 1L;

    /**
     * 测试方法
     */
    public static void main(String[] args) {
        JFrame3dsLoader appwin = new JFrame3dsLoader();
        appwin.initialize();
    }

    /**
     * This is the default constructor
     */
    public JFrame3dsLoader() {
        
    }

    /**
     * 初始化Frame主窗口
     * 
     * @return void
     */
    private void initialize() {
        // 实例化GraphicsConfiguration的方法1
        GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
        GraphicsEnvironment env = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsDevice device = env.getDefaultScreenDevice();
        GraphicsConfiguration config = device.getBestConfiguration(template);
        // 新建Frame窗口对象
        Frame dsframe = new Frame(config);
        // 若Frame对象的布局方式为绝对布局null,就无法显示3D虚拟场景
        dsframe.setLayout(new BorderLayout());
        dsframe.setTitle("3DS模型显示区");
        dsframe.setBounds(new Rectangle(0, 0, 433, 458));
        // 添加窗口监听器实现关闭窗口(Frame),关闭窗口时退出程序
        dsframe.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                System.out.print("退出程序!");
                System.exit(0);
            }
        });
        // 新建3D场景绘制画布Canvas3D对象
        Canvas3D cv = new Canvas3D(config);
        dsframe.add(cv, BorderLayout.CENTER);
        // 使Frame对象可视化
        dsframe.setVisible(true);
        // 在Canvas3D画布上绘制3D场景
        creat3DObject(cv);
    }

    /**
     * 创建3D虚拟场景
     */
    private void creat3DObject(Canvas3D cv) {
        // 创建超结构(create superstructure)
        VirtualUniverse vu = new VirtualUniverse();
        Locale loc = new Locale(vu);
        // 创建视图分支(create view branch)
        BranchGroup bgView = createViewBranch(cv);
        bgView.compile();
        loc.addBranchGraph(bgView);
        BranchGroup bg = createSceneGraph();
        bg.compile();
        loc.addBranchGraph(bg);
    }

    /**
     * 生成BranchGroup的私有方法,创建视图分支
     * 
     * @param cv
     *            Canvas3D画布对象
     */
    private BranchGroup createViewBranch(Canvas3D cv) {
        // 创建View组件对象
        View view = new View();
        // 设置投影方式
        view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION);
        // 创建ViewPlatform叶节点
        ViewPlatform vp = new ViewPlatform();
        view.addCanvas3D(cv);
        view.attachViewPlatform(vp);
        view.setPhysicalBody(new PhysicalBody());
        // 设置View对象属性
        view.setPhysicalEnvironment(new PhysicalEnvironment());
        // 几何变换
        Transform3D trans = new Transform3D();
        // 观察者眼睛的位置
        Point3d eye = new Point3d(0, 0, 1 / Math.tan(Math.PI / 8));
        // 观察者方向指向的点
        Point3d center = new Point3d(0, 0, 0);
        // 垂直于观察者方向向上的方向
        Vector3d vup = new Vector3d(0, 1, 0);
        // 生成几何变换矩阵
        trans.lookAt(eye, center, vup);
        // 求矩阵的逆
        trans.invert();
        // 几何变换组点
        TransformGroup tg = new TransformGroup(trans);
        tg.addChild(vp);
        // 创建视图分支
        BranchGroup bgView = new BranchGroup();
        bgView.addChild(tg);
        return bgView;
    }

    
    public BranchGroup createSceneGraph(){
        // 创建场景图分支
        BranchGroup group = new BranchGroup();
        
        // 几何变换组节点
        TransformGroup transGroup = new TransformGroup();
        // 几何变换
        Transform3D trans3d = new Transform3D();
        // 缩放变换
        trans3d.setScale(0.7);
        //将几何变换节点对象添加到节点组
        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 = new Background(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 = new DirectionalLight(lightColor, lightDirection);
        light.setInfluencingBounds(bound);
        //将光源添加到场景
        group.addChild(light);
        
        //几何变换组节点 - 加载外部模型
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        //加载Obj格式的模型文件
        objTrans.addChild(new ThreeDSFileReader("F:/rexue.3ds"));
        //将模型添加到变换组节点
        transGroup.addChild(objTrans);
        
        // 添加通过鼠标左键控制3D物体旋转的对象
        MouseRotate behavior = new MouseRotate();
        behavior.setTransformGroup(objTrans);
        objTrans.addChild(behavior);
        behavior.setSchedulingBounds(bound);
        // 添加鼠标右键的拖拉运动控制3D物体(X,Y)平移
        MouseTranslate tr = new MouseTranslate();
        tr.setTransformGroup(objTrans);
        objTrans.addChild(tr);
        tr.setSchedulingBounds(bound);
        // 添加鼠标滚轮控制3D物体沿Z轴
        MouseWheelZoom tr1 = new MouseWheelZoom();
        tr1.setTransformGroup(objTrans);
        objTrans.addChild(tr1);
        tr1.setSchedulingBounds(bound);
        
        group.compile();
        
        return group;
    }
    
}


转载请注明出处:[http://www.cnblogs.com/dennisit/archive/2013/05/07/3065479.html]

在线交谈

 

posted @ 2013-05-07 20:23  苏二  阅读(6476)  评论(1编辑  收藏  举报