Java3D應用例項-載入Obj 三維模型

weixin_34219944發表於2013-05-07

OBJ檔案是Alias|Wavefront公司為它的一套基於工作站的3D建模和動畫軟體"Advanced Visualizer"開發的一種標準3D模型檔案格式,很適合用於3D軟體模型之間的互導,也可以通過Maya讀寫。比如你在3dsMaxLightWave中建了一個模型,想把它調到Maya裡面渲染或動畫,匯出OBJ檔案就是一種很好的選擇。目前幾乎所有知名的3D軟體都支援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:DennisIT@163.com">傳送郵件</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:DennisIT@163.com">傳送郵件</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); 
    }
}

 

例項二:實現場景中多個模型

效果圖:

例項程式碼:

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:DennisIT@163.com">傳送郵件</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:DennisIT@163.com">傳送郵件</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]

線上交談

 

相關文章