Java3D应用实例-加载Obj 三维模型

OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模型之间的互导,也可以通过Maya读写。比如你在3dsMaxLightWave中建了一个模型,想把它调到Maya里面渲染或动画,导出OBJ文件就是一种很好的选择。目前几乎所有知名的3D软件都支持OBJ文件的读写,不过其中很多需要通过插件才能实现。Java3D内置了OBJ文件的相关操作.

Java3D应用实例-加载Obj 三维模型Java3D应用实例-加载Obj 三维模型

实例比较简单,制作的效果是模型加载后绕Y轴旋转.我们可以通过模型动态展示观察3D效果

OBJ文件读取工具类代码如下:[在使用前别忘了添加Java3D驱动包,这块信息可以看这篇http://www.cnblogs.com/dennisit/archive/2013/05/06/3063042.html ]

废话不多说,直接上代码,说明见代码注释
实例一:加载obj文件到场景

package com.java3d.dennist.loader;



import javax.media.j3d.BranchGroup;

import com.sun.j3d.loaders.Scene;

import com.sun.j3d.loaders.objectfile.ObjectFile;



/**

 *

 *

 *  @version : 1.1

 *  

 *  @author  : 苏若年         <a href="mailto:[email protected]">发送邮件</a>

 *    

 *  @since   : 1.0      创建时间:    2013-5-7 下午02:13:43

 *     

 *  TODO     :  Java3D读取OBJ文件

 *

 */

public class ObjFileReader extends BranchGroup{



    private double creaseAngle = 60.0;

    

    /**

     * 

     * 读取ObjModel文件

     * 

     * @param filePath    obj文件路径

     * 

     */

    public ObjFileReader(String filePath){

        BranchGroup branchGroup = new BranchGroup();

        int flags = ObjectFile.RESIZE;

        ObjectFile objFile = new ObjectFile(flags, (float)(creaseAngle*Math.PI)/180);

        Scene scenen = null;

        try {

            scenen = objFile.load(filePath);

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println("OBJ模型加载失败" + e.getMessage());

        }

        branchGroup.addChild(scenen.getSceneGroup());

        this.addChild(branchGroup);

    }

    

}

Java加载OBJ模型到应用场景

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.Vector3f;



import com.sun.j3d.utils.applet.MainFrame;

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



/**

 *

 *

 *  @version : 1.1

 *  

 *  @author  : 苏若年         <a href="mailto:[email protected]">发送邮件</a>

 *    

 *  @since   : 1.0      创建时间:    2013-5-7 下午02:21:18

 *     

 *  TODO     : Java3D 实例 OBJ模型加载 展示

 *

 */

public class JavaModelObjLoaderApp extends Applet{



    /**

     * 

     */

    private static final long serialVersionUID = 5841679659336190804L;



    

    public BranchGroup createSceneGraph(){

        // 创建场景图分支

        BranchGroup group = new BranchGroup();

        

        // 几何变换组节点

        TransformGroup transGroup = new TransformGroup();

        // 几何变换

        Transform3D trans3d = new Transform3D();

        // 缩放变换

        trans3d.setScale(0.8);

        //将几何变换节点对象添加到节点组

        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 ObjFileReader("F:/guanyu.obj"));

        //将模型添加到变换组节点

        transGroup.addChild(objTrans);

        

        //设置几何变化,绕Y轴中心旋转

        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(bound);

        objTrans.addChild(rotator);

        

        group.compile();

        

        return group;

    }

    

    

    public JavaModelObjLoaderApp(){

        

        setLayout(new BorderLayout());

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

        Canvas3D canvas = new Canvas3D(null);

        add("Center",canvas);

        BranchGroup scene = createSceneGraph();

        

        SimpleUniverse universe = new SimpleUniverse(canvas);

        universe.getViewingPlatform().setNominalViewingTransform();

        universe.addBranchGraph(scene);

    }

    

    

    public static void main(String[] args) {

        new MainFrame(new JavaModelObjLoaderApp(), 360,360); 

    }

}

 

实例二:实现场景中多个模型

效果图:

Java3D应用实例-加载Obj 三维模型

实例代码:

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.Group;

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;



/**

 *

 *

 *  @version : 1.1

 *  

 *  @author  : 苏若年         <a href="mailto:[email protected]">发送邮件</a>

 *    

 *  @since   : 1.0      创建时间:    2013-5-7 下午03:46:59

 *     

 *  TODO     : 

 *

 */

public class JavaModelObjMultiLoaderApp extends Applet{

    



    /**

     * 

     */

    private static final long serialVersionUID = 3645520149291184985L;





    public BranchGroup createSceneGraph(){

        // 创建场景图分支

        BranchGroup group = new BranchGroup();

        

        // 几何变换组节点

        TransformGroup transGroup = new TransformGroup();

        // 几何变换

        Transform3D trans3d = new Transform3D();

        // 缩放变换

        trans3d.setScale(0.8);

        //将几何变换节点对象添加到节点组

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

        

        BranchGroup branchGroup1 = new ObjFileReader("F:/guanyu.obj");

        BranchGroup branchGroup2 = new ObjFileReader("F:/guanyu.obj");

        

        group.addChild(createObject(branchGroup1,bound, -1.3f, 0.0f, 0.0f,3000));

        group.addChild(createObject(branchGroup2,bound, 1.3f, 0.0f, 0.0f ,4000));

        

        group.compile();

        

        return group;

    }

    

    /**

     * 

     * Description:  创建模型行为  绕Y轴旋转

     * @param group       模型结点

     * @param bound       模型作用范围边界

     * @param xpos       模型展示左边X轴位置

     * @param ypos       模型展示左边y轴位置

     * @param zpos   模型展示左边z轴位置

     * @param time   模型转动速度

     * @return

     */

    private Group createObject(BranchGroup group,BoundingSphere bound, float xpos, float ypos,float zpos, int time){

        Transform3D trans3d = new Transform3D();

        trans3d.setTranslation(new Vector3f(xpos, ypos, zpos));

        TransformGroup objTrans = new TransformGroup(trans3d);

        TransformGroup sgroup = new TransformGroup();

        sgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        sgroup.addChild(group);

        

        //设置几何变化,绕Y轴中心旋转

        Transform3D yAxis = new Transform3D();

        Alpha rotationAlpha = new Alpha(-1,Alpha.INCREASING_ENABLE,

                0,0,

                time,0,0,

                0,0,0);

        RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, sgroup,yAxis,0.0f,(float)Math.PI*2.0f);

        rotator.setSchedulingBounds(bound);

        objTrans.addChild(rotator);

        objTrans.addChild(sgroup);

        return objTrans;

    }

    

    public JavaModelObjMultiLoaderApp(){

        

        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,3,6);

        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 JavaModelObjMultiLoaderApp(), 350,210); 

    }



}

 

实例二:模型行为变换 实现鼠标触控360度空间旋转

package com.java3d.dennist.loader;



import java.applet.Applet;

import java.awt.BorderLayout;



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.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;

/**

*

*

*  @version : 1.1

*  

*  @author  : 苏若年         <a href="mailto:[email protected]">发送邮件</a>

*    

*  @since   : 1.0      创建时间:    2013-5-7 下午02:21:18

*     

*  TODO     : Java3D 实例 OBJ模型加载 展示,360度空间旋转

*

*/

public class JavaModelObjLoader extends Applet {



    /**

     * 

     */

    private static final long serialVersionUID = 5841679659336190804L;



    public BranchGroup createSceneGraph() {

        // 创建场景图分支

        BranchGroup group = new BranchGroup();



        // 几何变换组节点

        TransformGroup transGroup = new TransformGroup();

        // 几何变换

        Transform3D trans3d = new Transform3D();

        // 缩放变换

        trans3d.setScale(0.8);

        // 将几何变换节点对象添加到节点组

        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 ObjFileReader("F:/a.obj"));

        // 将模型添加到变换组节点

        transGroup.addChild(objTrans);



        // 设置模型行为,鼠标触控360度空间旋转

        MouseRotate behavior = new MouseRotate();

        behavior.setTransformGroup(objTrans);

        objTrans.addChild(behavior);

        behavior.setSchedulingBounds(bound);

        MouseTranslate tr = new MouseTranslate();

        tr.setTransformGroup(objTrans);

        objTrans.addChild(tr);



        tr.setSchedulingBounds(bound);

        MouseWheelZoom tr1 = new MouseWheelZoom();

        tr1.setTransformGroup(objTrans);

        objTrans.addChild(tr1);

        tr1.setSchedulingBounds(bound);



        group.compile();

        return group;

    }



    public JavaModelObjLoader() {



        setLayout(new BorderLayout());

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

        Canvas3D canvas = new Canvas3D(null);

        add("Center", canvas);

        BranchGroup scene = createSceneGraph();



        SimpleUniverse universe = new SimpleUniverse(canvas);

        universe.getViewingPlatform().setNominalViewingTransform();

        universe.addBranchGraph(scene);

    }



    public static void main(String[] args) {

        new MainFrame(new JavaModelObjLoader(), 360, 360);

    }

    

}


说明:
sun
公司的Java3D市场当年没有火起来,最近要用才开始找资料学习,不然 哎!!哪来心思整这玩意儿网上搜了下 都是学院派的东东,什么毕业设计啦 什么虚拟校园的啦 老生长谈 看来学院派真的很out老跟不上技术发展 用这没人用的东西小小抱怨一下自己学习整理一下 以备后来者被逼用过时技术的时候直接copy, 省出些时间,泡妹纸的泡妹纸宅技术的宅技术(新技术 IT行业的潮流 嘿嘿)

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

在线交谈

 

你可能感兴趣的:(java)