简简单单java 3D入门

大家晚上好啊 我是寻觅!


以后会有文章给大家介绍一个java 3D 游戏引擎现在,先委屈大家看个简单例子了,

呵呵;

先给大家个游戏引擎下载地址大家可以先下去看看:在这里要感谢新朋友守护天使的

推荐


http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge

/j/ji/jirr/


呵呵,废话少说了 开始解析代码

大家先要下载些东西,说实话java就是这样,功能不错就是麻烦

下载:


java-media

http://java.sun.com/products/java-media/jmf/2.1.1/download.html


java-3d

http://www.downloadjava3d.com/windows.php

https://java3d.dev.java.net/binary-builds.html


接着,安装就不说了,easy very

当然希望大家把doc也下来看看。

装好后,给大家一个经过我修改的简单实验代码:(由于时间关系,解说会放在以后)

import  java.awt. * ;
import  javax.swing. * ;
import  javax.media.j3d. * ;
import  javax.vecmath. * ;
import  java.awt.event. * ;
import  com.sun.j3d.utils.geometry. * ;

 
public   class  MyJava3D  extends  JFrame
{
   
//  Virtual Universe object.
   private VirtualUniverse universe;

   
//  Locale of the scene graph.
   private Locale locale;
 

   
// BranchGroup for the Content Branch of the scene
   private BranchGroup contentBranch;

   
//  TransformGroup  node of the scene contents
   private TransformGroup contentsTransGr;
 

   
// BranchGroup for the View Branch of the scene
   private BranchGroup viewBranch;

   
// ViewPlatform node, defines from where the scene is viewed.
   private ViewPlatform viewPlatform;

   
//  Transform group for the ViewPlatform node
   private TransformGroup vpTransGr;

   
//  View node, defines the View parameters.
   private View view;

   
// A PhysicalBody object can specify the user's head
   PhysicalBody body;

   
// A PhysicalEnvironment object can specify the physical
   
// environment in which the view will be generated
   PhysicalEnvironment environment;

   
// Drawing canvas for 3D rendering
   private Canvas3D canvas;

   
// Screen3D Object contains screen's information
   private Screen3D screen;

   
private Bounds bounds;
 
//***********************MyJava3D******************************/
  public MyJava3D()
  
{
    
super("My First Java3D Example");

//****************************************************************************************/

GraphicsDevice dev 
=
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
GraphicsConfigTemplate3D template 
= new GraphicsConfigTemplate3D();
GraphicsConfiguration config 
=
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(template);
canvas 
= new Canvas3D(config);






    
// Creating and setting the Canvas3D
    
//canvas = new Canvas3D(null);                                   //改了这里
  
//****************************************************************************************/
  
  
    getContentPane().setLayout( 
new BorderLayout( ) );
    getContentPane().add(canvas, 
"Center");

    
// Setting the VirtualUniverse and the Locale nodes
    setUniverse();

    
// Setting the content branch
    setContent();

    
// Setting the view branch
    setViewing();

    
// To avoid problems between Java3D and Swing
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);

    
// enabling window closing
    addWindowListener(new WindowAdapter() {
                       
public void windowClosing(WindowEvent e)
                                           
{System.exit(0); }   }
);
    setSize(
600600);
    bounds 
= new BoundingSphere(new Point3d(0.0,0.0,0.0), Double.MAX_VALUE);
  }

//***********************MyJava3D******************************/ 

//***********************setUniverse******************************/
  private void setUniverse()
    
{
        
// Creating the VirtualUniverse and the Locale nodes
        universe = new VirtualUniverse();
        locale 
= new Locale(universe);
    }

//***********************setUniverse******************************/
 
//***********************setContent******************************/
  private void setContent()
    
{
        
// Creating the content branch

        contentsTransGr 
= new TransformGroup();
        contentsTransGr.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        setLighting();

        ColorCube cube1 
= new ColorCube(0.1);

        Appearance appearance 
= new Appearance();
        cube1.setAppearance(appearance);

        contentsTransGr.addChild(cube1);
 

        ColorCube cube2 
= new ColorCube(0.25);

        Transform3D t1 
= new Transform3D();
        t1.rotZ(
0.5);
        Transform3D t2 
= new Transform3D();
        t2.set(
new Vector3f(0.7f0.6f,-1.0f));
        t2.mul(t1);
        TransformGroup trans2 
= new TransformGroup(t2);
        trans2.addChild(cube2);
        contentsTransGr.addChild(trans2);
 

        Sphere sphere 
= new Sphere(0.2f);
        Transform3D t3 
= new Transform3D();
        t3.set(
new Vector3f(-0.2f0.5f,-0.2f));
        TransformGroup trans3 
= new TransformGroup(t3);

        Appearance appearance3 
= new Appearance();

        Material mat 
= new Material();
        mat.setEmissiveColor(
-0.2f1.5f0.1f);
        mat.setShininess(
5.0f);
        appearance3.setMaterial(mat);
        sphere.setAppearance(appearance3);
        trans3.addChild(sphere);
        contentsTransGr.addChild(trans3);
 

        contentBranch 
= new BranchGroup();
        contentBranch.addChild(contentsTransGr);
        
// Compiling the branch graph before making it live
        contentBranch .compile();

        
// Adding a branch graph into a locale makes its nodes live (drawable)
        locale.addBranchGraph(contentBranch);
    }

//***********************setContent******************************/

//***********************setLighting******************************/
   private void setLighting()
    
{
        AmbientLight ambientLight 
=  new AmbientLight();
        ambientLight.setEnable(
true);
        ambientLight.setColor(
new Color3f(0.10f0.1f1.0f) );
        ambientLight.setCapability(AmbientLight.ALLOW_STATE_READ);
        ambientLight.setCapability(AmbientLight.ALLOW_STATE_WRITE);
        ambientLight.setInfluencingBounds(bounds);
        contentsTransGr.addChild(ambientLight);

        DirectionalLight dirLight 
=  new DirectionalLight();
        dirLight.setEnable(
true);
        dirLight.setColor( 
new Color3f( 1.0f0.0f0.0f ) );
        dirLight.setDirection( 
new Vector3f( 1.0f-0.5f-0.5f ) );
        dirLight.setCapability( AmbientLight.ALLOW_STATE_WRITE );
        dirLight.setInfluencingBounds(bounds);
        contentsTransGr.addChild(dirLight);
    }


//***********************setLighting******************************/

//***********************setViewing******************************/
  private void setViewing()
     
{
        
// Creating the viewing branch

         viewBranch 
= new BranchGroup();

         
// Setting the viewPlatform
         viewPlatform = new ViewPlatform();
         viewPlatform.setActivationRadius(Float.MAX_VALUE);
         viewPlatform.setBounds(bounds);

         Transform3D t 
= new Transform3D();
         t.set(
new Vector3f(0.3f0.7f3.0f));
         vpTransGr 
= new TransformGroup(t);

     
// Node capabilities control (granding permission) read and write access
    
//  after a node is live or compiled
     
//  The number of capabilities small to allow more optimizations during compilation
         vpTransGr.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
         vpTransGr.setCapability( TransformGroup.ALLOW_TRANSFORM_READ);

         vpTransGr.addChild(viewPlatform);
         viewBranch.addChild(vpTransGr);

         
// Setting the view
          view = new View();
          view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION );
          view.addCanvas3D(canvas);

          body 
= new PhysicalBody();
          view.setPhysicalBody(body);
          environment 
= new PhysicalEnvironment();
          view.setPhysicalEnvironment(environment);

          view.attachViewPlatform(viewPlatform);

          view.setWindowResizePolicy(View.PHYSICAL_WORLD);

          locale.addBranchGraph(viewBranch);
     }


//***********************setViewing******************************/




 
//***********************************************************/
  public static void main(String[] args)
  
{
    JFrame frame 
= new MyJava3D();
    frame.setVisible(
true);

  }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值