Building the Model and Scene
Now it was time to write the MIDlet code. The makeGeometry() method (Listing Two) assembles the data into a model. It invokes the built-in methods to load the positions, normals, and textures into VertexArrays va1, normArray1, and texArray, respectively. Next, it adds the VertexArrays to a VertexBuffer. Finally, the code makes a TriangleStripBuffer, idxBuf1, which references the triangle strips stored in va1.
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 these arrays. 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()
However, all I've done is set up the model's surfaces and texture map. Now I must describe the model's appearance, and the scene it resides in. Starting with the appearance, the method setAppearance() (Listing Three, available electronically) first loads the texture image into an instance of Texture2D. Next, it specifies the mapping characteristics that control how M3G applies the image to the model's surfaces. The code finishes up by making an instance of Appearance, and invokes setMatColours() to load the attributes generated by ObjView into it. This completes the model's assembly and appearance.
To build a scene, a POV must be established for a scene, and the lighting set and model added to it; see the createScene() method in Listing Four (available electronically). You can trace the steps that create a Camera and set its location in space. More M3G API calls create a Light, then set its type, color, and direction. (Rotating the Light to set its direction is only necessary for spotlights.) The last thing this method does is initialize the transform array, modelTrans(), which rotates or translates (moves) the model. With the scene illuminated and its POV established, the code invokes makeGeometry() and makeAppearance() to add the model to it. The last bit of code sets the background to a medium blue color.