Java Reference
In-Depth Information
visual trapezoidal object. The vertex format specified corresponds to the use of both co-
ordinates for vertex positions and per vertex color. Since joining two appropriate vertices
will produce a side and the visual object has 12 sides, a total of 24 indices for an index
count of 24 are needed to render the object.
A Point3f array named pts with eight elements is created to store the eight coordinates
of the vertices. This is followed by declaring an integer array named indices for storing the
vertices in pairs, with each pair forming a line of the visual object. Next, the codes segment
uses array.setCoordinates(0,pts) and array.setCoordinateIndices(0, indices) to set the coor-
dinates associated with the vertices and the index array for accessing these coordinates.
The colors of the vertices, which influence the colors of the lines drawn in the manner
as shown in the result in Figure 35, is set in the same way as that for the vertex coordinates.
Specifically, a Color3f array is created with each element initiated to the colors needed. Note
that this Color3f array need not have the same size as the Point3f array for the vertices. As
an example, if only three colors are needed, the Color3f array will only be of size three and
the colorIndices array declared subsequently will have values that are between 0 and 2.
Figure 36. First code segment of DemoSphere.java
1.
public class Axis
2.
{
3.
private BranchGroup axisBG;
4.
5.
public Axis()
6.
{
7.
axisBG = new BranchGroup();
8.
int n=200; float z=0.5f; Color3f color = new Color3f(0.2f, 0.3f, 0.2f);
9.
axisBG = drawSphere(n, z, color);
10.
}
11.
12.
public BranchGroup drawSphere(int n, float size, Color3f color)
13.
{
14.
BranchGroup axisBG = new BranchGroup();
15.
IndexedLineArray []array = new IndexedLineArray[n];
16.
for(int i=0; i<n; i++)
17.
{
18.
float radius = (float)(Math.sin(Math.PI*i/n));
19.
float dist = (float)(Math.cos(Math.PI*i/n));
20.
array[i] = drawCircle(100,(float)(radius*size),0f,0f,(float)(dist*size), color);
21.
axisBG.addChild(new Shape3D(array[i]));
22.
}
23.
return axisBG;
24.
}
Search WWH ::




Custom Search