objloader.java

来自「实现obj文件的加载和用鼠标旋转,可从多个角度查看用3d做的模型」· Java 代码 · 共 181 行

JAVA
181
字号
import   com.sun.j3d.loaders.objectfile.ObjectFile; 
import   com.sun.j3d.loaders.ParsingErrorException; 
import   com.sun.j3d.loaders.IncorrectFormatException; 
import   com.sun.j3d.loaders.Scene; 
import   java.awt.GraphicsConfiguration; 
import   java.applet.Applet; 
import   java.awt.BorderLayout; 
import   java.awt.event.*; 
import   com.sun.j3d.utils.image.TextureLoader; 
import   com.sun.j3d.utils.applet.MainFrame; 
import   com.sun.j3d.utils.universe.*; 
import   com.sun.j3d.utils.behaviors.mouse.*; 
import   com.sun.j3d.utils.image.*; 
import   java.io.*; 
import   javax.imageio.*; 
import   javax.media.j3d.*; 
import   javax.vecmath.*; 

public   class   objLoader   extends   Applet   { 
    public   BranchGroup   createSceneGraph(String   filename)   { 

                  BranchGroup   objRoot   =   new   BranchGroup(); 

                          BoundingSphere   bounds   =new   BoundingSphere(new   Point3d(0.0,0.0,0.0),   100.0);//设置涉及范围 

                          { 
                              Color3f   bgColor   =   new   Color3f(0.2f,   0.3f,   0.8f);//背景颜色 
        Background   bg   =   new   Background(bgColor); 
        bg.setApplicationBounds(bounds); 
        objRoot.addChild(bg); 
                          } 
                          { 
        //设置变换组 
          TransformGroup   objTrans1=new   TransformGroup(); 

        Transform3D   x   =   new   Transform3D(); 
        Transform3D   y   =   new   Transform3D(); 


        TransformGroup   xRot=new   TransformGroup(); 

        xRot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
        xRot.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 


        x.rotX(Math.PI/180);       //x轴旋转 
        xRot.setTransform(x); 
        objRoot.addChild(xRot); 
        y.rotY(Math.PI/180); 

        objTrans1.setTransform(y);       //Y轴旋转 
        objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
        objTrans1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 

      xRot.addChild(objTrans1); 

        //定义鼠标行为 
//         MouseRotate   behavior   =   new   MouseRotate(); 
//         behavior.setTransformGroup(objTrans1); 
//         objRoot.addChild(behavior); 
//         behavior.setSchedulingBounds(bounds); 

//         MouseZoom   behavior2   =   new   MouseZoom(); 
//         behavior2.setTransformGroup(objTrans1); 
//         objRoot.addChild(behavior2); 
//         behavior2.setSchedulingBounds(bounds); 

//         MouseTranslate   behavior3   =   new   MouseTranslate(); 
//         behavior3.setTransformGroup(objTrans1); 
//         objRoot.addChild(behavior3); 
//         behavior3.setSchedulingBounds(bounds); 
    } 


    { 


        //整体光设置 
        Color3f   light1Color   =   new   Color3f(0.0f,   1.0f,1.0f); 
        Vector3f   light1Direction     =   new   Vector3f(4.0f,   -7.0f,   -12.0f); 
        Color3f   light2Color   =   new   Color3f(0.3f,   0.3f,   0.4f); 
        Vector3f   light2Direction     =   new   Vector3f(-6.0f,   -2.0f,   -1.0f); 
        Color3f   ambientColor   =   new   Color3f(0.1f,   0.1f,   0.1f); 

        //   渐变光 
        AmbientLight   ambientLight   =   new   AmbientLight(ambientColor); 
        ambientLight.setInfluencingBounds(bounds); 
        objRoot.addChild(ambientLight); 

        //   定义方向光 
        DirectionalLight   light1=   new   DirectionalLight(light1Color,   light1Direction); 
        light1.setInfluencingBounds(bounds); 
        objRoot.addChild(light1); 

        DirectionalLight   light2   =   new   DirectionalLight(light2Color,   light2Direction); 
        light2.setInfluencingBounds(bounds); 
        objRoot.addChild(light2); 


        TransformGroup   objTrans2   =   new   TransformGroup(); 
        objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
        objRoot.addChild(objTrans2); 

        //   Create   an   Alpha   object   that   goes   from   0   to   1   in   8   seconds, 
        //   repeatedly. 
        Alpha   rotationAlpha   =   new   Alpha(-1,   Alpha.INCREASING_ENABLE, 
        0,   0,40000,   0,   0,       0,0,   0); 

        //   Create   a   new   Behavior   object   that   will   rotate   the   scene 
        //   add   it   into   the   scene   graph 

//         Transform3D   yAxis   =   new   Transform3D(); 
//         RotationInterpolator   rotator   =new   RotationInterpolator(rotationAlpha,   objTrans2,   yAxis,0.0f,   (float)   Math.PI*2.0f); 
//         rotator.setSchedulingBounds(bounds); 
//         objTrans2.addChild(rotator); 

          //准备导入 

        Transform3D   objTrans3d   =   new   Transform3D(); 
        objTrans2.getTransform(objTrans3d); 
        objTrans2.setTransform(objTrans3d); 
        //objTrans2.addChild(objTrans2); 

        int   flags   =   ObjectFile.RESIZE; 
        ObjectFile   f   =     new   ObjectFile(flags,   (float)(49.0   *   Math.PI   /   30.0)); 

        Scene   s   =   null; 
        try   { 
            s   =   f.load(filename); 

        } 
        catch   (FileNotFoundException   e)   { 
            System.err.println(e); 
            System.exit(1); 
        } 
        catch   (ParsingErrorException   e)   { 
            System.err.println(e); 
            System.exit(1); 
        } 
        catch   (IncorrectFormatException   e)   { 
            System.err.println(e); 
            System.exit(1); 
        } 

        //sceneTG.addChild(f); 

        MouseRotate   behavior   =   new   MouseRotate(); 
        behavior.setTransformGroup(objTrans2); 
        objRoot.addChild(behavior); 
        behavior.setSchedulingBounds(bounds); 


        objTrans2.addChild(s.getSceneGroup()); 

        return   objRoot; 
    } 
    } 
  public   objLoader(String   filename)   { 
        filename= "pao.obj";                                 //定义viewplatform 
    setLayout(new   BorderLayout()); 
    GraphicsConfiguration   config=SimpleUniverse.getPreferredConfiguration(); 
    Canvas3D   c=new   Canvas3D(config); 
    add( "Center",c); 
    Viewer   viewer=new   Viewer(c); 
    Vector3d   viewpoint=new   Vector3d(.0,.0,3.0); 
    Transform3D   t=new   Transform3D(); 
    t.set(viewpoint); 
    ViewingPlatform   v=new   ViewingPlatform(); 
    v.getViewPlatformTransform().setTransform(t); 

    BranchGroup   scene=createSceneGraph(filename); 
    SimpleUniverse   u=new   SimpleUniverse(v,viewer); 
    u.getViewingPlatform(); 
    u.addBranchGraph(scene); 
  } 


    public   static   void   main(String[]   args)   { 
            new   MainFrame(new   objLoader(null),   800,   600); 
    } 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?