Java Reference
In-Depth Information
Figure 24. Code segment and result of LineStripArraySimpleMaze.java
1.
private BranchGroup axisBG;
2.
private int layer = 20;
3.
private float m = 0.05f;
4.
private float dz = 0.02f;
5.
private int[] counts = {layer*2+2};
6.
7.
public LineStripArray draw(int layer, float m, LineStripArray lineArr){
8.
if (layer==1) {
9.
lineArr.setCoordinate(0, new Point3f(0.0f, 0.0f, 0.0f));
10.
lineArr.setCoordinate(1, new Point3f(-m, 0.0f, dz));
11.
lineArr.setCoordinate(2, new Point3f(-m, -m, 2*dz));
12.
lineArr.setCoordinate(3, new Point3f(0.0f, -m, 3*dz));
13.
}
14.
else {
15.
lineArr = draw(layer-1, m, lineArr);
16.
int i=(layer-1)%2;
17.
float p1, p2, p3;
18.
p1 = (float)-Math.pow(-1,i)*m*(layer/2+(1-i));
19.
p2 = (float)Math.pow(-1,i)*m*(layer/2);
20.
p3 = dz*layer;
21.
lineArr.setCoordinate((layer-1)*2+1, new Point3f(p1, p2, p3));
22.
lineArr.setCoordinate((layer-1)*2+2, new Point3f(p1, p1, p3+dz));
23.
lineArr.setCoordinate((layer-1)*2+3, new Point3f(p2, p1, p3+2*dz));
24. }
25. return lineArr;
26. }
27.
With five points and points 3 to 4 lying on a straight line, TriangleStripArray will give
rise to two non-trivial triangles formed by points 1, 2, 3 and 3, 4, 5. However, the two
triangles have a different sense in terms of the order of the coordinates, with the former
being clockwise and the latter being anticlockwise in directions.
This difference in directions will result in the output as shown in Figure 25, where
only one of the triangles will be visible at any instant. This is because the triangle with
clockwise coordinates has a normal or face that points in the negative z direction, while
the other with anticlockwise coordinates has a face in the opposite direction or points
towards the positive z axis.
To overcome this difference in directions due to the ordering of the coordinators, the
setNormal method can be used to set all the triangles to face the same direction. It is also
possible to insert a dummy coordinate or vertice so that all the non-trivial triangles face
the same direction. In this example, we can repeat point 3 and swap point 4 with point 5.
Doing so will increase the number of vertices and rendering time slightly.
Search WWH ::

Custom Search