Mobile Java & 3D Apps 

by Oscar Vivall and Tom Thompson



Listing One



(a)

// Mascot Capsule v3



public class CoolGameCanvas extends Canvas

{

// Get instance of 3D environment

   Graphics3D gameG3D = new Graphics3D();

// Paint method which handle 3D content display

   public void paint(Graphics g) {

   try {

      gameG3D.bind(g);

      // update the scene elements

      // render the scene

   } finally {

      gameG3D.release();

   }

} // end CoolGameCanvas



(b)

// JSR 184



public class CoolGameCanvas extends Canvas

{

// Get instance of 3D environment

   Graphics3D gameG3D = Graphics3D.getInstance();

// Paint method which handle 3D content display

   public void paint(Graphics g) {

   try {

      gameG3D.bindTarget(g);

      // update the scene elements

      // render the scene

   } finally {

      gameG3D.releaseTarget();

   }

} // end CoolGameCanvas





Listing Two



(a)

// Mascot Capsule v3



// Triangles that make up the cube's faces. Are vertices, not triangle strips

   private int[] vert = { 

    10, 10, 10, -10, 10, 10,  10,-10, 10,  -10, 10, 10, 10,-10, 10, 

                                                        -10,-10, 10, // front

    -10, 10,-10, 10, 10,-10,  -10,-10,-10, 10, 10,-10,  -10,-10,-10, 

                                                       10,-10,-10,   // back

    -10, 10, 10, -10, 10,-10, -10,-10, 10, -10, 10,-10, -10,-10, 10,   

                                                       -10,-10,-10,  // left

    10, 10,-10,  10, 10, 10,  10,-10,-10,  10, 10, 10,  10,-10,-10,  

                                                       10,-10, 10,   // right

    10, 10,-10, -10, 10,-10,  10, 10, 10,  -10, 10,-10, 10, 10, 10,  

                                                      -10, 10, 10,   // top

    10,-10, 10, -10,-10, 10,  10,-10,-10,  -10,-10, 10, 10,-10,-10,   

                                                      -10,-10,-10 }; // bottom

// Cube's normals -- Used in lighting calculations.

private int[] norm = {  

0, 0, 4096,  0, 0, 4096,  0, 0, 4096,  0, 0, 4096,  0, 0, 4096,  0, 0, 4096,

0, 0, -4096, 0, 0,-4096,  0, 0,-4096,  0, 0,-4096,  0, 0,-4096,  0, 0, -4096,

-4096, 0, 0, -4096, 0, 0, -4096, 0,  0,-4096, 0, 0, -4096, 0, 0, -4096, 0,  0,

4096, 0, 0,  4096, 0, 0,  4096, 0, 0,  4096, 0, 0,  4096, 0, 0,  4096, 0,  0,

0, 4096, 0,  0, 4096, 0,  0, 4096, 0,  0, 4096, 0,  0, 4096, 0,  0, 4096, 0,

0, -4096, 0, 0, -4096, 0, 0, -4096, 0, 0, -4096, 0, 0, -4096, 0, 0, -4096, 0};



(b)

// JSR 184



private VertexBuffer    iVb;  // Vertex positions, normals, colors, texcoords

private IndexBuffer     iIb;  // Indices to VertexBuffer, for tri-strips

// Triangles strips that make up the cube's faces

   short[] vert = { 

      10, 10, 10,   -10, 10, 10,    10,-10, 10,   -10,-10, 10,   // front

     -10, 10,-10,    10, 10,-10,   -10,-10,-10,    10,-10,-10,   // back

     -10, 10, 10,   -10, 10,-10,   -10,-10, 10,   -10,-10,-10,   // left

      10, 10,-10,    10, 10, 10,    10,-10,-10,    10,-10, 10,   // right

      10, 10,-10,   -10, 10,-10,    10, 10, 10,   -10, 10, 10,   // top

      10,-10, 10,   -10,-10, 10,    10,-10,-10,   -10,-10,-10 }; // bottom

 

// Create a VertexArray to hold the vertices for the object

   VertexArray vertArray = new VertexArray( vert.length / 3, 3, 2 );

   vertArray.set( 0, vert.length/3, vert );

 

// The per-vertex normals for the cube 

   byte[] norm = {0, 0, 127,      0, 0, 127,     0, 0, 127,     0, 0, 127,

                 0, 0, -127,      0, 0,-127,     0, 0,-127,    0, 0, -127,

                 -127, 0, 0,     -127, 0, 0,   -127, 0,  0,   -127, 0,  0,

                  127, 0, 0,      127, 0, 0,     127, 0, 0,    127, 0,  0,

                  0, 127, 0,      0, 127, 0,     0, 127, 0,     0, 127, 0,

                 0, -127, 0,     0, -127, 0,    0, -127, 0,   0, -127, 0};

// Create a vertex array for the normals of the object.

   VertexArray normArray = new VertexArray( norm.length / 3, 3, 1 );

   normArray.set( 0, norm.length/3, norm );

   int[] stripLen = { 4, 4, 4, 4, 4, 4 };  // Length of each triangle strip

// Create the VertexBuffer for our object

   VertexBuffer vb = iVb = new VertexBuffer();

   vb.setPositions(vertArray, 1.0f, null);       // Unit scale, zero bias

   vb.setNormals(normArray);

 

// Create the index buffer for the object (this tells how to create triangle

// strips from the contents of the vertex buffer).

   iIb = new TriangleStripArray( 0, stripLen );





Listing Three 



(a)

// Mascot Capsule v3



private static Texture d1_texture;

   d1_texture = new Texture("tex_skin.bmp", true);

// Coordinates on 2D BMP image to be mapped onto the cube's faces.

   private int[] tex = {  

     96, 32,      64, 32,      96, 64,       64, 32,      96, 64,    64, 64,

     64, 32,      32, 32,      64, 64,       32, 32,      64, 64,    32, 64,

     64, 0,       32, 0,       64, 32,       32, 0,       64, 32,    32, 32,

     32, 0,        0, 0,       32, 32,        0, 0,       32, 32,     0, 32,

     32, 32,       0, 32,      32, 64,        0, 32,      32, 64,     0, 64, 

     96, 0,       64, 0,       96, 32,       64, 0,       96, 32,    64, 32 };

   private int [] kolor = {  0xFF00FF }; // Placeholder



(b)

// JSR 184



private Image           iImage;

private Appearance      iAppearance;  // For material, texture, compositing

// Vertex texture coordinates. These specify the coordinates on a 2-D image

//   that will be painted onto a surface.

   short[] tex = {  

     96, 32,       64, 32,       96, 64,       64, 64,

     64, 32,       32, 32,       64, 64,       32, 64,

     64, 0,        32, 0,        64, 32,       32, 32,

     32, 0,         0, 0,        32, 32,        0, 32,

     32, 32,        0, 32,       32, 64,        0, 64,

     96, 0,        64, 0,        96, 32,       64, 32 };

// Create a vertex array for the texture coordinates of the object

     VertexArray texArray = new VertexArray( tex.length / 2, 2, 2 );

     texArray.set(0, tex.length/2, tex);

// Place texture coords into instance of VertexBuffer made in Listing 2 

     vb.setTexCoords(0, texArray, (1.0f/128.0f), null);  

                                         // 128-pixel scale, zero bias

// Load the image for the texture

     iImage = Image.createImage("/cubeface.png");

// Create the Image2D (we need this so we can make a Texture2D)

     Image2D image2D = new Image2D( Image2D.RGB, iImage );

// Create the Texture2D and enable mipmapping

    Texture2D texture = new Texture2D( image2D );

    texture.setFiltering( Texture2D.FILTER_NEAREST,Texture2D.FILTER_NEAREST );

    texture.setWrapping( Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP );

    texture.setBlending( Texture2D.FUNC_MODULATE );

// Create the appearance

     iAppearance = new Appearance();

     iAppearance.setTexture( 0, texture ); // Add Texture2D to the Appearance

     iAppearance.setMaterial(iMaterial);

     iMaterial.setVertexColorTrackingEnable( true ); // Track per-vertex color

     iMaterial.setColor(Material.SPECULAR, 0xFFFFFFFF);  // Specular = white

     iMaterial.setShininess(100.0f);



Listing Four



(a)

//Mascot Capsule v3



// Vectors for point of camera point of view

private Vector3D D1_POS = new Vector3D(0,30,50);

private Vector3D D1_LOOK = new Vector3D(0,-2048,-4096);

private Vector3D D1_UP = new Vector3D(0,4096,0);



// Set up camera and rotation transform

     AffineTrans d1_vtrans = new AffineTrans();

     d1_vtrans.setIdentity();

     d1_vtrans.lookAt(D1_POS, D1_LOOK, D1_UP);



(b)

// JSR 184



private Camera iCamera;

// Create a camera

     iCamera = new Camera();

     iCamera.setPerspective(80.0f,              // field of view

         (float)getWidth()/ (float)getHeight(),  // aspectRatio

         1.0f,      // near clipping plane

         1000.0f);  // far clipping plane



Listing Five



(a)

// Mascot Capsule v3



Effect3D effect;

// Lights settings. Only two lights: directional and ambient

// Directional light, vector and intesity

     private Vector3D lightDirVec = new Vector3D(-146,293,439);

     private int lightDir = 3730;

// Ambient light intensity

     private int lightAmbi = 1626;

//  Make the lights

     private Light d1_light = new Light(lightDirVec,lightDir,lightAmbi);

//  Effects set up

     effect = new Effect3D();

     effect.setShadingType(Effect3D.NORMAL_SHADING);

     effect.setLight(d1_light);



(b)

// JSR 184



private Light  iLight;

// Create a light

     iLight = new Light();

     iLight.setColor( 0xffffff );         // White

     iLight.setIntensity(1.25f);          // Over bright



Listing Six



(a)

// Mascot Capsule v3



private int D1_pers = 512;    // Equivalent to 45 degrees

FigureLayout d1_layout;

// Set up Figure's (cube) layout

     d1_layout = new FigureLayout();

     d1_layout.setPerspective(10, 4096, D1_pers);

     d1_layout.setCenter(getWidth()/2, getHeight()/2); // Center on screen





(b)

// Accomplished in Listing 4.



Listing Seven



(a)

// Mascot Capsule v3



// Constants for graphics commands

private static final int COMMAND = Graphics3D.PRIMITVE_TRIANGLES |

     Graphics3D.PDATA_NORMAL_PER_VERTEX |

     Graphics3D.PDATA_TEXURE_COORD |

     Graphics3D.PDATA_COLOR_NONE |

     Graphics3D.ENV_ATTR_LIGHTING | Graphics3D.PATTR_BLEND_HALF;

// Instantiate 3D graphics object       

     Graphics3D g3d = new Graphics3D();

// Draw the scene           

     try{

        g3d.bind(g);

        g3d.renderPrimitives(d1_texture, 0, 0, d1_layout, effect, COMMAND, 12,

                             vert, norm, tex, kolor);

        g3d.flush();

        g3d.release( g );           

        } catch (Throwable h){

            h.printStackTrace();

        } // end catch



(b)

// JSR 184



private Graphics3D iG3D;

// Set up the camera in the desired position

     Transform transform3D = new Transform();

     transform3D.postTranslate(0.0f, 0.0f, 30.0f);

// Get the singleton Graphics3D instance

     iG3D = Graphics3D.getInstance();

     iG3D.setCamera(iCamera, transform3D);

     iG3D.resetLights();

     iG3D.addLight(iLight, transform3D);

// Render our cube

     iG3D.render(iVb, iIb, iAppearance, null);

// Free the graphics object

     iG3D.releaseTarget();











5



