Channels ▼
RSS

Mobile

Importing 3D Models into Mobile Java Devices

Source Code Accompanies This Article. Download It Now.


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()
Listing Two

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video