JAVA3D

  AND

 VRML

-R.S.Ramaswamy

(rs.ramaswamy@gmail.com)

-------------------------------

   JAVA3D and JMF are important topics in view of possible applications of similar concepts in mobile devices through J2ME. We had covered JMF, last month. In the present  introductory tutorial, we are covering Java3D and comparing it with VRML.

------------------------------------

 

     3D and animation are interesting areas of applications. Java3d enables us to create 3D objects and animate them. We can write applets as well as standalone frames using 3D. The terminology of Java3D is similar to an earlier technolgy named VRML ( Virtual Reality Modelling Language). Sometimes, it would appear that VRML is simpler but only for trivial applications. When the scene is more coplex, the advantage of Java3d becomes evident.

 

    Java3D  is not available within JDK.We must download  the required software from:

   http://java.sun.com/products/

          javamedia/3D/index.html

 

There are separate zip files for specifications and API documentation.For the moment, we can confine ourselves to the main software.

 

   The packages that we get are as follows:

a) javax.media.j3d

b) javax.vecmath

c) com.sun.j3d.utils

 

After installation,we begin with a simple demo for a coloured 3D cube.(cubedemo.java)

-------------------------------------------

// cubedemo.java

 

    

import java.awt.*;

import java.awt.event.*;

import javax.media.j3d.*;

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

import com.sun.j3d.utils.geometry.*;

 

public class  cubedemo                     extends Frame

{

public static void main(String args[])

    {

    cubedemo     app = new cubedemo();

    app.resize(300,300);

    app.move(300,100);

    app.show();

    }

 

   cubedemo()

     {

 

 Canvas3D   canvas  =new Canvas3D(null);

       add(canvas);

 

 SimpleUniverse   universe  =

           new SimpleUniverse(canvas);

 

universe.getViewingPlatform.

setNominalViewingTransform();

 

 BranchGroup       root  =

                  new BranchGroup();

 

 Transform3D  transform  =

                   new Transform3D();

 transform.rotX(Math.PI/6.0);

 Transform3D y60  =new Transform3D();

 

       y60.rotY(Math.PI/3.0);

       transform.mul(y60);

 

 TransformGroup group  =

        new TransformGroup(transform);

       root.addChild(group);

 ColorCube     cube=

                new ColorCube(.3);

       group.addChild(cube);

       universe.addBranchGraph(root);

 

       }

}

======================================

 

   All Java3d programs follow a pattern. First we create a canvas for drawing our 3d picture. This class is Canvas3D.This canvas is added to the screen.Next we create an instance of SimpleUniverse class.We then have a BranchGroup instance.

 

   In order to create the 3D effect, we want to tilt the cube slightly in both X-axis and Y-axis. For this we need Transform3D object.Having obtained it, we tilt by 30 degrees in X-axis and 60 degrees in Y-axis.This is specified as an instance of TransformGroup object.

 

   Now, we create an instance of the ColorCube.We have specified 0.3  as relative size.This is then added to the scene.

( screenshot shown  as    cubedemo.jpg)

-----------------------------------------

 

   Let us now consider a more complex example which shows four primitive shapes in different colours.

---

// fourobjects.java

 

 

import java.awt.*;

import java.awt.event.*;

import javax.media.j3d.*;

import javax.vecmath.*;

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

import com.sun.j3d.utils.geometry.*;

 

 

public class fourobjects extends Frame

{

   public static void main(String args[])

   {

    fourobjects  app  = new fourobjects();

    app.resize(300,300);

    app.move(300,100);

    app.show();

    }

 

     public Appearance    getAppearance

      (float red,float green,float blue)

        {

 Appearance appearance=new Appearance();

 Color3f     ambient=

            new Color3f(0.1f,0.1f,0.1f);

 Color3f color =

            new Color3f(red,green,blue);

 Color3f white = new Color3f(1,1,1);

 Color3f black = new Color3f(0,0,0);

 

 appearance. setMaterial(new  Material(ambient,black,color,white,10));

 

           return appearance;

        }

 

 

 

 

     public void setShape(

BranchGroup    parent,

 Vector3d      translate,

 Primitive     shape)

 

     {

 Transform3D transform = new Transform3D();

 Transform3D  t        = new Transform3D();

     t.rotX(Math.PI/6);

     transform.set(translate);

     transform.mul(t);

     t.rotY(Math.PI/6);

     transform.mul(t);

     transform.setScale(0.5);

 

     TransformGroup group  =

     new TransformGroup(transform);

     parent.addChild(group);

     group.addChild(shape);             

                   

 

     }

 

 

 

     public fourobjects()

     {          

   Canvas3D canvas   = new Canvas3D(null);

   add(canvas,"Center");

 

   SimpleUniverse universe  =

               new SimpleUniverse(canvas);

 

      universe.getViewingPlatform().setNominalViewingTransform();

 

      BranchGroup root = new BranchGroup();

 

      BoundingSphere      bounds=

                 new BoundingSphere();

AmbientLight ambient = new AmbientLight();

      ambient.setInfluencingBounds(bounds);

 

DirectionalLight directional =

                    new DirectionalLight();

directional.setInfluencingBounds(bounds);

      root.addChild(ambient);

 

      root.addChild(directional);

 

                              

 

setShape(root, new Vector3d(-0.5,0.5,-1),

         new Box      (0.5f,0.5f,0.5f,

getAppearance(1,0,0)));//red

 

 setShape(root, new Vector3d(0.5,-0.5,-1),

        new Cylinder (0.5f,1.0f,     getAppearance(0,1,0)));// green

 

setShape(root, new Vector3d(0.5,0.5,-1),

  new Cone     (0.5f,1.0f,Cone.GENERATE_NORMALS,   getAppearance(0,0,1)));//blue

 

setShape(root, new Vector3d(-0.5,0.5,-1),

       new Sphere   (.7f,           getAppearance(0,1,1)));// cyan

 

      universe.addBranchGraph(root);

      }

 

}

===========================================

 

As before , we begin with the canvas3D in the constructor.We then create a SimpleUniverse object.After defining the required ambient light, we create the four objects.

 

The getAppearance  method is user-defined and takes three parameters defining RGB in float).

red color for the box.

green  color for the cylinder.

blue for the cone

cyan for the sphere.

-----

For translation of position, distance is reckoned as positive towards East for X-axis, towards  North for Y-axis and towards the observer for Z-axis.We specify (x,y,z) as floats.

 

-----

Box takes 3 parameters as (width,height,depth)

 

Cylinder has two parameters

(radius, height)

 

Cone  has radius as first and height as second parametr.

 

Sphere has just the radius as parameter.

----------------------

 

Therefore,

setShape(root, new Vector3d(-0.5,0.5,-1),

         new Box      (0.5f,0.7f,0.6f,

getAppearance(1,0,0)));//red

 

means a box having 0.5 as width,0.7 as height and 0.6 as depth. The box is placed at 0.5 to the left,0.5 to the north and -1 away from the observer.The color is red.

---

With this explanation, the code can be followed easily.

-------------------------------------------

   We will now see a similar example in VRML. The code is given below.

 

 

#VRML V2.0 utf8

 

 

Shape{

appearance Appearance {

material   Material {

 

diffuseColor   1  0  0  

}

}

geometry    Sphere { }

}

#-------------------------

Transform{

 

 translation   -3 0  0

 children

Shape{

appearance Appearance {

material   Material {

 

diffuseColor   1  1  0  

}

}

geometry    Cylinder { }

}

}

#---------------------------

 

Transform{

 

 translation   -3   3  0

 children

Shape{

appearance Appearance {

material   Material {

 

diffuseColor   0  1  0  

}

}

geometry    Cone { }

}

}

#---------------------------

 

Transform{

 

 translation   0   3   0

 children

Shape{

appearance Appearance {

material   Material {

 

diffuseColor   1  0   1

}

}

geometry    Box { }

}

}

#---------------------------

}

 

 

We can download

vrml plugin for internet explorer

====================================

http://www.parallelgraphics.com/products/cortona/download/iexplore/

============================================

file name: cortvrml.exe, file size: 1.58MB

==========================

We type the URL as :

"g:/fourshapes.wrl"

 

The resulting display in browser is given as fourshapes.jpg

-----

 

Suggested Further Reading:

 

1) Using 3D in your programs

   (page 520-523)

   Java2 Platform Unleashed-

   Jamie Jaworsky

2) Internet Programming with Java2-

   Art Gittleman

   (Scott Jones publishing)

   (DreamTech)

3) Teach yourself VRML2

    Chris Marrin of Silicon Graphics

   Techmedia

4) A number of examples have been given in Sun demo package.

-------------------------------------------------

 

-----------------------------------------------

 

 

1