Importing 3D Models into Mobile Java Devices
by Tom Thompson

Listing One

// Methods for model in brocket.obj
  private short[] getVerts()
  // return an array holding Vertexes [2457 values / 3 = 819 points] 
  {
    short[] vals = {
          15,-3,-56,     16,-3,-56,     16,-4,-60,
     16,-4,-60,     26,-8,-70,     15,-3,-56,  
        ...  
     -11,-2,-41,    -11,-5,-40,    -14,-4,-26      };
    return vals;
  }  // end of getVerts()
  private byte[] getNormals()
  // return an array holding Normals [2457 values / 3 = 819 points] 
  {
    byte[] vals = {
          -110,-66,-6,   -70,105,-23,   -78,70,-73,  
     -78,70,-73,    -89,-77,-51,   -110,-66,-6,  
        ...  
     92,85,23,      120,39,21,     99,79,19      };
    return vals;
  }  // end of getNormals()
  private short[] getTexCoords()
  // return an array holding TexCoords [1638 values / 2 = 819 points] 
  {
    short[] vals = {
          160,72,   163,72,   164,68,
     164,68,   191,58,   160,72,
        ...  
     86,87,    86,88,    78,102      };
    return vals;
  }  // end of getTexCoords()
  private int[] getStripLengths()
  // return an array holding the lengths of each triangle strip
  {
    int[] lens = {
      44, 28, 40, 76, 31, 69, 15, 85, 36, 8, 21, 22, 156, 44, 28, 40, 76
    };
    return lens;
  }  // end of getStripLengths()
  private Material setMatColours()
  // set the material's colour and shininess values
  {
    Material mat = new Material();
    mat.setColor(Material.AMBIENT, 0x00000000);
    mat.setColor(Material.EMISSIVE, 0x00000000);
    mat.setColor(Material.DIFFUSE, 0xFFCDCDCD);
    mat.setColor(Material.SPECULAR, 0x00000000);
    mat.setShininess(128.0f);
    return mat;
  } // end of setMatColours()


Listing Two

    private IndexBuffer idxBuf1;    // Indices to the model's triangle strips
    private VertexBuffer vertBuf1;  
    private void makeGeometry()
    {
/*  Read in the values for the vertices, normals, and texture coords.
    Create a VertexBuffer object for each array. Next read in
    the strip lengths array and place the indexes in the IndexBuffer.
    These point to the triangle strips stored in the vertex buffer.
    Depending on the model, there may not be any texture coordinates,
    and so the calls related to them should be commented out.
*/
// Make vertices from coordinate data
        short[] verts = getVerts();
        VertexArray va1 = new VertexArray(verts.length/3, 3, 2);
        va1.set(0, verts.length/3, verts);
// Make normals from data
        byte[] norms = getNormals();
        VertexArray normArray1 = new VertexArray(norms.length/3, 3, 1);
        normArray1.set(0, norms.length/3, norms);
// Make texture coords -- uncomment this code to use textures
        short[]tcs = getTexCoords();
        VertexArray texArray = new VertexArray(tcs.length/2, 2, 2);
        texArray.set(0, tcs.length/2, tcs);
// ------ Make the VertexBuffer for this model -------
        vertBuf1 = new VertexBuffer();
// fix the scale and bias to create points in range [-1 to 1]
        float[] pbias = {(1.0f/255.0f), (1.0f/255.0f), (1.0f/255.0f)}; 
                                                          // Set scaling
        vertBuf1.setPositions(va1, (2.0f/255.0f), pbias); // set up geometry
        vertBuf1.setNormals(normArray1);                  // Set up normals
// Uncomment following line to use textures
       vertBuf1.setTexCoords(0, texArray, (1.0f/255.0f), null);
// Create the index buffer for the model. This info tells M3G how to
// parse triangle strips from the contents of the vertex buffer.
        idxBuf1 = new TriangleStripArray(0, getStripLengths() );
    }  // end makeGeometry()




2


