Java Reference
In-Depth Information
Since the texture is only in 2D, there is only an (x,y) coordinate associated with each vertex.
In TriangleCanvas , the vertices and normals are combined into a VertexBuffer for
rendering. Here in CornerCanvas , the texture coordinate is added into the same VertexBuffer :
VertexBuffer verbuf = mVertexBuffer = new VertexBuffer();
verbuf.setPositions(vertexArray, 1.0f, null);
verbuf.setNormals(normalsArray);
verbuf.setTexCoords(0, textureArray, 1.0f, null);
A TriangleStripArray is then built to tell M3G where each of the four strips are within the
VertexBuffer :
int[] stripLength = { 4, 4, 4};
mIndexBuffer = new TriangleStripArray( 0, stripLength );
Here, the first triangle strip starts at index 0 and has a length of 4. Subsequently, there are
two more triangle strips of length 4.
The last piece of code that is required creates the texture from the Image2D and adds it to
Appearance . Texture2D is a rendering attribute that is grouped by an Appearance object, just like
Material and PolygonMode . The code from Listing 15-3 that is responsible for adding Texture2D
to Appearance is shown here:
Texture2D texture = new Texture2D( image2D );
mAppearance.setTexture(0, texture);
That is all there is to the texture-mapping code. You should be able to see how you can
build complex 3D scenes out of submeshes, applying texture and material to them, lighting
them up, and then transforming them.
In reality, to create even a moderately complex 3D object—say, a jumping kangaroo—you
may have to define hundreds of triangles. Even a simple 3D scene usually requires a few 3D
objects, which means many vertices and a lot of M3G code! There must be a simpler solution.
Immediate Mode vs. Retained Mode
The simpler solution is to use the retained mode of the M3G API.
Thus far, all the examples have used the M3G API in immediate mode. Immediate mode
provides low-level access to the rendering process, very close to the rendering engine. This
level of access is similar to access provided by other industry-standard embedded 3D APIs such
as OpenGL ES. Immediate mode works with the 3D scenes in terms of vertices, normals, trans-
forms, appearance, etc.
Retained mode provides a significantly higher level of access to a 3D world (often called a
scene graph ).
High-Level Access to a Scene Graph via Retained Mode
A 3D world in retained mode can contain all the 3D objects in a scene prebuilt (all vertices,
texture, normals, etc., specified) and in position; lights and cameras are predefined and in
position, transformation can also be predefined, and animation can also be created beforehand.
Search WWH ::




Custom Search