Natural

           

          我的java3d學習(一)——顯示obj模型

          我的java3d學習(一)——顯示obj模型

          最近被3d漫游所吸引,所以嘗試學習一下java3d。

          類關系圖:

          最近對3d漫游感興趣,嘗試學習一下。Java3D入門資料收藏夾里有詳細介紹,此處不再啰嗦了。

          在網(wǎng)上下了個人物模型,寫了個類載入效果如下:


          模型下載鏈接:
          http://www.aygfsteel.com/Files/Man/csMANmodule.part2.rar
          http://www.aygfsteel.com/Files/Man/csMANmodule.part1.rar
          紋理鏈接:
          http://www.aygfsteel.com/images/blogjava_net/Man/42654/t_flooring.jpg

          源代碼:
          main類
          package com.zzl.j3d.viewer;

          import java.applet.Applet;
          import java.awt.BorderLayout;
          import java.awt.GraphicsConfiguration;
          import java.net.URL;

          import javax.media.j3d.Appearance;
          import javax.media.j3d.BoundingSphere;
          import javax.media.j3d.BranchGroup;
          import javax.media.j3d.Canvas3D;
          import javax.media.j3d.Group;
          import javax.media.j3d.Texture;
          import javax.media.j3d.TextureAttributes;
          import javax.media.j3d.Transform3D;
          import javax.media.j3d.TransformGroup;
          import javax.vecmath.Point3d;
          import javax.vecmath.Vector3d;
          import javax.vecmath.Vector3f;

          import com.sun.j3d.loaders.Scene;
          import com.sun.j3d.utils.applet.MainFrame;
          import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
          import com.sun.j3d.utils.geometry.Box;
          import com.sun.j3d.utils.geometry.Primitive;
          import com.sun.j3d.utils.image.TextureLoader;
          import com.sun.j3d.utils.universe.SimpleUniverse;
          import com.sun.j3d.utils.universe.ViewingPlatform;
          import com.zzl.j3d.loader.J3DLoader;

          public class Main extends Applet{
              
              
          private SimpleUniverse universe ; 
              
          private Canvas3D canvas; 
              
          private BoundingSphere bounds = new BoundingSphere(new Point3d(0.00.00.0), 1000.0);
              TransformGroup objTransG 
          = null;
              
              
          public void Main()
              {}
              
              
          public void setupView()
              {
                  
          /** Add some view related things to view branch side of scene graph */ 
                  
          // add mouse interaction to the ViewingPlatform    
                  OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL|OrbitBehavior.STOP_ZOOM); 
                  orbit.setSchedulingBounds(bounds);    
                  ViewingPlatform viewingPlatform 
          = universe.getViewingPlatform();   
                  
          // This will move the ViewPlatform back a bit so the       
                  
          // objects in the scene can be viewed.     
                  viewingPlatform.setNominalViewingTransform();     
                  viewingPlatform.setViewPlatformBehavior(orbit);    
              } 
              
              
          public void init()
              {
                  setLayout(
          new BorderLayout());
                  GraphicsConfiguration gc 
          = SimpleUniverse.getPreferredConfiguration();
                  canvas 
          = new Canvas3D(gc);
                  add(
          "Center",canvas);
                  
                  
          // Create a simple scene and attach it to the virtual universe
                  universe = new SimpleUniverse(canvas);    
                  
                  
          // 定義觀察點,可用鼠標進行縮放、旋轉(zhuǎn)
                  setupView();
                  
                  J3DLoader loader 
          = new J3DLoader();
                  
          // 定義場景的根結(jié)點
                  BranchGroup objRoot = new BranchGroup();
                  
                  
          // 載入人物模型
                  Scene scene = loader.loadObj("Liit.obj");
                  
                  
          // 定義載入位置
                  Transform3D pos1 = new Transform3D();
                  pos1.setTranslation(
          new Vector3f(0f,0.0f,0f)); 
                  
          // 因為此處所采用的模型載入時是平行于y=0面的,所以要繞x軸旋轉(zhuǎn)90度
                  pos1.rotX(-1.57);
                  objTransG 
          = new TransformGroup();
                  objTransG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                  objTransG.setTransform(pos1);

                  objTransG.addChild(scene.getSceneGroup());
                  
                  objRoot.addChild(objTransG);
                  
                  
          // 載入紋理
                  URL texImage = loader.loadTexture("resource/flooring.jpg");
                  
          // 畫了一個大矩形,作為地板
                  Group group = this.createSceneBackGround(texImage);
                  objRoot.addChild(group);
                  universe.addBranchGraph(objRoot);
              }
              
              
          public void destroy() 
              {    
                  universe.removeAllLocales();    
              } 
              
              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          new MainFrame(new Main(), 256256);
              }

              
          public Group createSceneBackGround(URL texImage)
              {
                  
          return createGeometry(Texture.MULTI_LEVEL_POINT , -1.1f, texImage);
              }
              
              
          public Group createGeometry(int filter, float y, URL texImage)
              {
                  Group topNode 
          = new Group();
                  Appearance appearance 
          = new Appearance(); 
                  TextureLoader tex 
          = new TextureLoader(texImage, TextureLoader.GENERATE_MIPMAP , this);
                  Texture texture 
          = tex.getTexture();
                  
                  texture.setMinFilter(filter);
                  appearance.setTexture(texture);   
                  TextureAttributes texAttr 
          = new TextureAttributes();  
                  texAttr.setTextureMode(TextureAttributes.MODULATE);  
                  appearance.setTextureAttributes(texAttr); 
                  
                  
          //TODO light
                  Transform3D pos2 = new Transform3D();
                  pos2.setTranslation(
          new Vector3f(0f,y,0f));   
                  objTransG 
          = new TransformGroup(); 
                  objTransG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                  objTransG.setTransform(pos2);
                  
                  
                  Box box 
          = new Box(6f,0.1f,8f,Primitive.GENERATE_NORMALS|Primitive.GENERATE_TEXTURE_COORDS,appearance);
                  objTransG.addChild(box);
                  topNode.addChild(objTransG);
                  
          return topNode;
              }
          }


          模型、紋理載入類:
          package com.zzl.j3d.loader;

          import java.io.File;
          import java.io.FileNotFoundException;
          import java.net.MalformedURLException;
          import java.net.URL;

          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;

          public class J3DLoader {

              
          private static java.net.URL texImage = null
              
              
          /**
               * 載入obj模型
               * 
          @param arg0 String 模型文件名
               * 
          @return Scene
               
          */
              
          public Scene loadObj(String arg0)
              {
                  
          int flags = ObjectFile.RESIZE;
                  ObjectFile f 
          = new ObjectFile(flags,(float)(49.0 * Math.PI / 180.0));
                  
          // 創(chuàng)建場景葉節(jié)點
                  Scene s = null;
                  
          try {
                      s 
          = f.load(arg0);
                  } 
          catch (FileNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (IncorrectFormatException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (ParsingErrorException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  
          return s;
              }
              
              
          /**
               * 載入紋理圖片
               * 
          @param args String 圖片文件名
               * 
          @return URL
               
          */
              
          public URL loadTexture(String args)
              {
                  File f 
          = new File(args);
                  
          try {
                      texImage 
          = f.toURL();
                  } 
          catch (MalformedURLException e) {
                      e.printStackTrace();
                  }
                  
          return texImage;
              }
          }




          posted on 2009-11-07 12:51 此號已被刪 閱讀(4244) 評論(2)  編輯  收藏 所屬分類: JAVA3D學習筆記

          評論

          # re: 我的java3d學習(一)——顯示obj模型 2014-11-19 00:32 劉鵬森

          為什么我載入之后,模型變黑呀  回復  更多評論   

          # re: 我的java3d學習(一)——顯示obj模型 2016-04-06 14:47 佳文

          @劉鵬森
          我的也是變黑了  回復  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          導航

          統(tǒng)計

          常用鏈接

          留言簿(8)

          隨筆分類(83)

          隨筆檔案(78)

          文章檔案(2)

          相冊

          收藏夾(7)

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 确山县| 图木舒克市| 巩义市| 柯坪县| 辽中县| 山阳县| 常山县| 清水县| 抚远县| 东海县| 民乐县| 台中县| 克山县| 兰西县| 赣榆县| 枞阳县| 潍坊市| 安图县| 额尔古纳市| 章丘市| 南汇区| 库尔勒市| 阿合奇县| 蒙山县| 当涂县| 蒙阴县| 枣阳市| 修水县| 峨边| 永昌县| 桐乡市| 彝良县| 阿城市| 泸定县| 文水县| 海阳市| 淮滨县| 石柱| 花莲县| 灌云县| 遂昌县|