Java Reference
In-Depth Information
The first version works with 8-bit byte values, while the second one works with 16-bit short
values. In Listing 15-1, the 16 bit version is used to fill the vertexArray variable. The decision of
which version to use will depend on what you will be using the resulting VertexArray for. In the
next section, you will see that normals in M3G are defined in a VertexArray containing 8-bit
values.
Defining Normals
During 3D rendering, a normal at a vertex tells the 3D engine which side of a triangle gets color
and lighting effects. For example, consider Figure 15-6.
Figure 15-6. Using normals to indicate which side of a triangle gets lighting and effects
The code from Listing 15-1 that defines the normals is shown here:
byte[] normals = { 0, 0, 127, 0, 0, 127, 0, 0, 127 };
VertexArray normalsArray = new VertexArray(normals.length / 3, 3, 1);
normalsArray.set(0, normals.length/3, normals);
The normal of a surface is always perpendicular (at 90 degrees) to the surface itself. In
Figure 15-6, each of the normals specified corresponds to a vertex of the triangle, specified in
vertexArray . For example, corresponding to the vertex (0,0,0) , the normal is (0,0, 127) ; and
corresponding to the vertex (0,3,0) , the normal is (0,0,127) . The set of normals for the triangle
tells M3G that the side of the triangle facing you is the side to light and render.
Combining Vertex Information into a VertexBuffer
The M3G class VertexBuffer is used to combine the position of vertices, their normals, and
other associated information into one single data structure. This allows the input of most of the
information for the skeletal frame of a 3D object into one single data structure. In Listing 15-1,
a VertexBuffer called verbuf is created using this code:
Search WWH ::




Custom Search